Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Favorite #19

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.pdf
/node_modules/
*.db
*.db
knexfile.js
.eslintrc.js
12 changes: 11 additions & 1 deletion controllers/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ const displayAccount = (knex, form) => (req, res) => {
});
}
data.semesters = semesters;
res.render('pages/account', data);
knex('users_favorites').where({ username: username }).select('courses')
.then(result => {
let favCourses = [];
console.log('favCourses');
console.log(favCourses);
if (!result == null) {
favCourses = result[0].course;
}
data.favCourses = result;
res.render('pages/account', data);
});
})
.catch(err => {
console.log('error occurred while querying users_courses: ', err.stack);
Expand Down
128 changes: 95 additions & 33 deletions controllers/course.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,95 @@ const handleCoursePost = async (req, res, next) => {
}
if (req.session.user) { // logged in
data.user = req.session.user;
const { newComment: commentText, newRating: rating } = req.body;
const user = data.user;
const course = req.query.id;
const date = new Date();

// check if user has commented/rated before
const oldComment = await Comment.query().where('course', course).where('user', user);
// if favoriting
if (req.body.favorite != null) {
const favorite = req.body.favorite;
let favCourses = await knex('users_favorites').where('username', data.user).select('courses');
let exists;
if (favCourses === undefined || favCourses.length == 0) {
exits = false;
} else {
exists = true;
favCourses = favCourses[0].courses;
}
const course = req.query.id;
console.log(favorite);
if (favorite === 'add') {
// sections are currently unused
if (!favCourses.includes(course)) {
favCourses.push(course);
}
if (exists) {
const updateFav = await knex('users_favorites').where('username', data.user).update({courses: JSON.stringify(favCourses)});
} else {
const addFav = await knex('users_favorites').where('username', data.user).insert({username: data.user, courses: JSON.stringify(favCourses)});
}
console.log('favoriting: ' + course)
req.session.notification = {
type: 'success',
message: 'Added course to favorites!'
};
} else if (favorite === 'delete') {
if (favCourses.includes(course)) {
favCourses.splice(favCourses.indexOf('course'), 1);
}
// sections are currently unused
const delFav = await knex('users_favorites').where('username', data.user).update({courses: JSON.stringify(favCourses)});
req.session.notification = {
type: 'success',
message: 'Removed course to favorites!'
};
console.log('unfavoriting: ' + course)
} else {
req.session.notification = {
type: 'error',
message: 'Error favoriting course! Something went wrong on our end :('
};
}
// res.redirect('/course?id=' + course);
} else {
const { newComment: commentText, newRating: rating } = req.body;
const user = data.user;
const course = req.query.id;
const date = new Date();

let isNewComment = false;
// check if user has commented/rated before
const oldComment = await Comment.query().where('course', course).where('user', user);

if (oldComment === undefined || oldComment.length == 0) { // new comment
isNewComment = true;
const newComment = await knex('comments').insert({comment: commentText, rating: rating, date: date, course: course, user: user});
} else { // update comment
const updatedComment = await knex('comments').where('course', course).where('user', user).update({comment: commentText, rating: rating, date: date});
}
let isNewComment = false;

console.log('COMMENT POSTING DEBUGGING ------------------------------');
console.log('commentText: ' + commentText);
console.log('rating: ' + rating);
console.log('user: ' + user);
console.log('course: ' + course);
console.log('date: ' + date);
console.log('oldComment:')
console.log(oldComment);
console.log('--------------------------------------------------------');
if (isNewComment) {
req.session.notification = {
type: 'success',
message: 'Successfully added comment!'
};
} else {
req.session.notification = {
type: 'success',
message: 'Successfully updated comment!'
};
if (oldComment === undefined || oldComment.length == 0) { // new comment
isNewComment = true;
const newComment = await knex('comments').insert({comment: commentText, rating: rating, date: date, course: course, user: user});
} else { // update comment
const updatedComment = await knex('comments').where('course', course).where('user', user).update({comment: commentText, rating: rating, date: date});
}

console.log('COMMENT POSTING DEBUGGING ------------------------------');
console.log('commentText: ' + commentText);
console.log('rating: ' + rating);
console.log('user: ' + user);
console.log('course: ' + course);
console.log('date: ' + date);
console.log('oldComment:')
console.log(oldComment);
console.log('--------------------------------------------------------');
if (isNewComment) {
req.session.notification = {
type: 'success',
message: 'Successfully added comment!'
};
} else {
req.session.notification = {
type: 'success',
message: 'Successfully updated comment!'
};
}

res.redirect('/course?id=' + course);
}

res.redirect('/course?id=' + course);
} else { // not logged in
console.log('non-logged in user tried to rate/comment');
req.session.notification = {
Expand Down Expand Up @@ -262,6 +312,18 @@ const handleCourseGet = async (req, res, next) => {
console.log(comments);
console.log('-------------------------------------------------')

console.log('FAVORITE DEBUGGIG INFO ------------------------------------------')
// handle user favorited courses/sections
if (req.session.user) { // if logged in
const favCourses = await knex('users_favorites').select('courses').where('username', data.user);
const favSections = await knex('users_favorites').select('sections').where('username', data.user);
data.favCourses = (favCourses);
data.favSections = (favSections);
console.log('favCourses:');
console.log(favCourses);
console.log('favSections:');
console.log(favSections);
}
res.render('pages/course', data);
}
} catch (error) {
Expand Down
1 change: 0 additions & 1 deletion migrations/20180810004232_create_courses_requirements.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

exports.up = function(knex, Promise) {
return knex.schema.createTable('courses_requirements', function(t) {
t.text('course').notNullable();
Expand Down
12 changes: 12 additions & 0 deletions migrations/20190405133903_create_users_favorite_courses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

exports.up = function(knex, Promise) {
return knex.schema.createTable('users_favorite_courses', function(t) {
t.text('username').notNullable().references('username').inTable('users');
t.jsonb('course').notNullable().references('course_full_number').inTable('courses');
t.unique(['username', 'course']);
});
};

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('users_favorite_courses');
};
12 changes: 12 additions & 0 deletions migrations/20190405133915_create_users_favorite_sections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

exports.up = function(knex, Promise) {
return knex.schema.createTable('users_favorite_sections', function(t) {
t.text('username').notNullable().references('username').inTable('users');
t.jsonb('section').notNullable().references('section_index').inTable('sections');
t.unique(['username', 'section']);
});
};

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('users_favorite_sections');
};
Loading