From 34885be326b3e62e451ed944e7d63ab2b539730d Mon Sep 17 00:00:00 2001 From: rpkenney Date: Wed, 15 Nov 2023 15:22:44 -0500 Subject: [PATCH] connected the wall to the db --- src/recommenderapp/app.py | 8 +-- src/recommenderapp/static/wall.js | 67 ++++++++++++++++---------- src/recommenderapp/templates/wall.html | 12 ++--- src/recommenderapp/utils.py | 11 +++++ 4 files changed, 63 insertions(+), 35 deletions(-) diff --git a/src/recommenderapp/app.py b/src/recommenderapp/app.py index 2da1dc44e..4b75e0ff4 100644 --- a/src/recommenderapp/app.py +++ b/src/recommenderapp/app.py @@ -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 +from utils import beautify_feedback_data, send_email_to_user, createAccount, logintoAccount, submitReview, getWallPosts import mysql.connector import os from dotenv import load_dotenv @@ -137,6 +137,10 @@ def review(): submitReview(g.db, 1, data["movie"], data["score"], data["review"], timestamp) return request.data +@app.route("/getWallData", methods=["GET"]) +def wallPosts(): + return getWallPosts(g.db) + @app.route("/feedback", methods=["POST"]) def feedback(): @@ -166,7 +170,6 @@ def success(): @app.before_request def before_request(): - print('opening db connection') load_dotenv() g.db = mysql.connector.connect(user='root', password=os.getenv('DB_PASSWORD'), host='127.0.0.1', @@ -174,7 +177,6 @@ def before_request(): @app.after_request def after_request(response): - print('closing db connection') g.db.close() return response diff --git a/src/recommenderapp/static/wall.js b/src/recommenderapp/static/wall.js index 076593634..292f5e9eb 100644 --- a/src/recommenderapp/static/wall.js +++ b/src/recommenderapp/static/wall.js @@ -1,26 +1,29 @@ -const posts = [ - { username: 'User1', movie: 'Movie1', review: 4, comment: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', imdbID:'tt0086190' }, - { username: 'User2', movie: 'Movie1', review: 3.5, comment: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', imdbID:'tt0371746' }, - { username: 'User3', movie: 'Movie1', review: 2, comment:'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', imdbID:'tt0126029' }, - { username: 'User1', movie: 'Movie1', review: 4, comment: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', imdbID:'tt0415306' }, - { username: 'User2', movie: 'Movie1', review: 3.5, comment: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', imdbID:'tt0120484' }, - { username: 'User3', movie: 'Movie1', review: 2, comment:'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', imdbID:'tt0317219' }, - ]; +posts = [] +function loadPosts(){ - $(document).ready(function () { - renderPosts(); - }); - - var result = ''; + return new Promise(function(resolve, reject){ + $.ajax({ + type: 'GET', + url: '/getWallData', + contentType: "application/json;charset=UTF-8", + success: function(response) { + console.log(response) + resolve(response) + }, + error: function(error) { + reject(error); + } + }); + }); +} - function fetchMovieData(imdbID){ +function fetchMovieData(imdbID){ var apikey = '77da67f1'; - - return new Promise(function(resolve, reject){ + return new Promise(function(resolve, reject){ $.ajax({ type: 'GET', url: 'http://www.omdbapi.com/', @@ -35,15 +38,15 @@ const posts = [ error: function(error) { reject(error); } + }); }); - }); - } +} - async function renderPosts() { +async function renderPosts() { const postContainer = $('#post-container'); posts.forEach(post => buildPost(post, postContainer)); - } +} async function buildPost(post, postContainer){ @@ -54,7 +57,7 @@ async function buildPost(post, postContainer){ var movieData; try{ - movieData = await fetchMovieData(post.imdbID); + movieData = await fetchMovieData(post.imdb_id); } catch(error){ console.error(error); } @@ -73,17 +76,17 @@ async function buildPost(post, postContainer){ var reviewDiv = $('
').addClass('review'); - for(let i = 0; i < post.review; i++){ + for(let i = 0; i < post.score; i++){ var star = $(''); reviewDiv.append(star); } var halfStar = $(''); - if((post.review * 10) % 10 > 0){ + if((post.score * 10) % 10 > 0){ reviewDiv.append(halfStar); } - var commentDiv = $('
').addClass('comment').text(post.comment); + var commentDiv = $('
').addClass('comment').text(post.review); var dataDiv = $('
').addClass('movie-data'); @@ -99,4 +102,18 @@ async function buildPost(post, postContainer){ userDiv.append(titleDiv, reviewDiv, commentDiv); postDiv.append(imageDiv, userDiv, dataDiv); postContainer.append(postDiv); -} \ No newline at end of file +} + +var loaded = false; + +$(document).ready(async function () { + if(!loaded){ + loaded = true; + try{ + posts = await loadPosts(); + } catch(error){ + console.error(error); + } + renderPosts(); + } +}); \ No newline at end of file diff --git a/src/recommenderapp/templates/wall.html b/src/recommenderapp/templates/wall.html index e97ead34e..26821227e 100644 --- a/src/recommenderapp/templates/wall.html +++ b/src/recommenderapp/templates/wall.html @@ -13,7 +13,11 @@ - + + + + +