Skip to content

Commit

Permalink
Added friends to the database and allowed the user to add and see all…
Browse files Browse the repository at this point in the history
… friends on profile page (Issue #12)
  • Loading branch information
brwali committed Nov 24, 2023
1 parent 756179e commit 3138814
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 8 deletions.
20 changes: 19 additions & 1 deletion src/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,22 @@ CREATE TABLE IF NOT EXISTS Ratings (
REFERENCES Movies (idMovies)
ON DELETE NO ACTION
ON UPDATE NO ACTION
) ENGINE = InnoDB;
) ENGINE = InnoDB;

-- Create the Friends table
CREATE TABLE IF NOT EXISTS Friends (
idFriendship INT NOT NULL AUTO_INCREMENT,
idUsers INT NOT NULL,
idFriend INT NOT NULL,
PRIMARY KEY (idFriendship),
CONSTRAINT idUsers
FOREIGN KEY (idUsers)
REFERENCES Users (idUsers)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT idFriend
FOREIGN KEY (idFriend)
REFERENCES Users (idUsers)
ON DELETE NO ACTION
ON UPDATE NO ACTION
) ENGINE = InnoDB;
17 changes: 14 additions & 3 deletions src/recommenderapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from flask import Flask, jsonify, render_template, request, g
from flask_cors import CORS
from search import Search
from utils import beautify_feedback_data, send_email_to_user, createAccount, logintoAccount, submitReview, getWallPosts, getRecentMovies, getUserName
from utils import beautify_feedback_data, send_email_to_user, createAccount, logintoAccount, submitReview, getWallPosts, getRecentMovies, getUserName, addFriend, getFriends
import mysql.connector
import os
from dotenv import load_dotenv
Expand Down Expand Up @@ -44,7 +44,9 @@ def profile_page():
"""
Renders the login page.
"""
return render_template("profile.html")
if (user[1] != None):
return render_template("profile.html")
return render_template("login.html")

@app.route("/wall")
def wall_page():
Expand Down Expand Up @@ -129,12 +131,17 @@ def signout():
def logIn():
data = json.loads(request.data)
resp = logintoAccount(g.db, data["username"], data["password"])
print(resp)
if (resp == None):
return 400
user[1] = resp
return request.data

@app.route("/friend", methods=["POST"])
def friend():
data = json.loads(request.data)
addFriend(g.db, data["username"], user[1])
return request.data

@app.route("/guest", methods=["POST"])
def guest():
data = json.loads(request.data)
Expand All @@ -161,6 +168,10 @@ def recentMovies():
def username():
return getUserName(g.db, user[1])

@app.route("/getFriends", methods=["GET"])
def getFriend():
return getFriends(g.db, user[1])


@app.route("/feedback", methods=["POST"])
def feedback():
Expand Down
21 changes: 20 additions & 1 deletion src/recommenderapp/static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,26 @@ $(document).ready(function () {
}

function friend(username) {

data = {
username:username
}
$.ajax({
type: "POST",
url: "/friend",
dataType: "json",
contentType: "application/json;charset=UTF-8",
traditional: "true",
cache: false,
data: JSON.stringify(data),
success: function (response) {
console.log(response)
$("#friendsList").append(response.username);
$("#addFriend").val("")
},
error: function (error) {

},
})
}

$("#friendButton").click(function () {
Expand Down
19 changes: 17 additions & 2 deletions src/recommenderapp/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ <h3 style="text-align: center;">Recently Rated Movies:</h2>
<center><button id="friendButton" onclick="friend()" class="btn btn-primary mx-auto">Add
Friend</button></center>
<h3 style="text-align: center;">Friends List:</h2>
<ul class="list-group" id="friendsList"></ul>
<ul class="list-group" style="column-count: 1; flex-direction: column; orientation: portrait;"
id="friendsList"></ul>
</div>
</div>
</div>
Expand All @@ -61,6 +62,7 @@ <h3 style="text-align: center;">Friends List:</h2>
window.onload = function () {
getRecentMoviesProfile();
getUserName();
getFriends();
}
function getRecentMoviesProfile() {
$.ajax({
Expand All @@ -83,14 +85,27 @@ <h3 style="text-align: center;">Friends List:</h2>
url: '/getUserName',
contentType: "application/json;charset=UTF-8",
success: function (response) {
console.log(response)
const resp = "Welcome " + response + "!";
$("#profBanner").append(resp);
},
error: function (error) {
}
});
}
function getFriends() {
$.ajax({
type: 'GET',
url: '/getFriends',
contentType: "application/json;charset=UTF-8",
success: function (response) {
response.forEach(element => {
$("#friendsList").append(element);
});
},
error: function (error) {
}
});
}
</script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" crossorigin="anonymous"></script>
Expand Down
15 changes: 14 additions & 1 deletion src/recommenderapp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ def createAccount(db, email, username, password):
executor.execute("INSERT INTO users(username, email, password) VALUES (%s, %s, %s);", (username, email, h.hexdigest()))
db.commit()

def addFriend(db, username, userId):
executor = db.cursor()
executor.execute("SELECT idUsers FROM users WHERE username = %s;", [username])
friendId = executor.fetchall()[0][0]
executor.execute("INSERT INTO friends(idUsers, idFriend) VALUES (%s, %s);", (int(userId), int(friendId)))
db.commit()

def logintoAccount(db, username, password):
executor = db.cursor()
newPass = (password + os.getenv("SALT") + username).encode()
Expand Down Expand Up @@ -237,4 +244,10 @@ def getUserName(db, user):
executor = db.cursor()
executor.execute("SELECT username FROM users WHERE idUsers = %s;", [int(user)])
result = executor.fetchall()
return jsonify(result[0][0])
return jsonify(result[0][0])

def getFriends(db, user):
executor = db.cursor()
executor.execute("SELECT username FROM users AS u JOIN friends AS f ON u.idUsers = f.idFriend WHERE f.idUsers = %s;", [int(user)])
result = executor.fetchall()
return jsonify(result)

0 comments on commit 3138814

Please sign in to comment.