-
Notifications
You must be signed in to change notification settings - Fork 1
Togglefavorite #49
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
base: main
Are you sure you want to change the base?
Togglefavorite #49
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,83 @@ | ||||||
| const { Favorite } = require('./models'); | ||||||
| const { User } = require('./models'); | ||||||
|
|
||||||
| module.exports = { | ||||||
| addFavorite(req, res) { | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May want to consider making this an async method due to all of the nesting of promises with |
||||||
| const { username } = req.body; | ||||||
| const { userId } = req.user; | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The userId is not provided by default in the request; you'd have to find a way to get the userId from the JWT subject (sub) claim from the request. A good place to do this would be in authentication.js |
||||||
|
|
||||||
| // Validate both user IDs exist | ||||||
| try { | ||||||
| User.findOne({ where: { username } }) | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. username is also not provided by default in the request; |
||||||
| .then(user => { | ||||||
| if (!user || !userId) { | ||||||
| return res.status(400).json({ error: 'Invalid user ID' }); | ||||||
| } | ||||||
|
|
||||||
| // Check if an entry in the Favorites table already exists | ||||||
| Favorite.findOne({ where: { user: userId, favorite: username } }) | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, you try to access a favorite with |
||||||
| .then(existingFavorite => { | ||||||
| if (existingFavorite) { | ||||||
| return res.status(400).json({ error: 'Favorite already exists' }); | ||||||
| } | ||||||
|
|
||||||
| // Create a Favorites entry with user as the userId from the JWT, and the favorite as the request body | ||||||
| Favorite.create({ user: userId, favorite: username }) | ||||||
| .then(() => { | ||||||
| // Return a 200 OK response | ||||||
| res.status(200).json({ message: 'Favorite created successfully' }); | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| }) | ||||||
| .catch(error => { | ||||||
| console.error(error); | ||||||
| res.status(500).json({ error: 'Internal Server Error' }); | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Too much nesting of try/catch. Will need to be more specific with status codes. |
||||||
| }); | ||||||
| }) | ||||||
| .catch(error => { | ||||||
| console.error(error); | ||||||
| res.status(500).json({ error: 'Internal Server Error' }); | ||||||
| }); | ||||||
| }) | ||||||
| .catch(error => { | ||||||
| console.error(error); | ||||||
| res.status(500).json({ error: 'Internal Server Error' }); | ||||||
| }); | ||||||
| } catch (error) { | ||||||
| console.error(error); | ||||||
| res.status(500).json({ error: 'Internal Server Error' }); | ||||||
| } | ||||||
| }, | ||||||
|
|
||||||
| removeFavorite(req, res) { | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same problems as above with |
||||||
| const { username } = req.body; | ||||||
| const { userId } = req.user; | ||||||
|
|
||||||
| // Validate both user IDs exist | ||||||
| try { | ||||||
| Favorite.findOne({ where: { user: userId, favorite: username } }) | ||||||
| .then(favorite => { | ||||||
| if (!favorite) { | ||||||
| return res.status(400).json({ error: 'Favorite does not exist' }); | ||||||
| } | ||||||
|
|
||||||
| // Remove the entry from the database | ||||||
| favorite.destroy() | ||||||
| .then(() => { | ||||||
| // Return a 200 OK response | ||||||
| res.status(200).json({ message: 'Favorite removed successfully' }); | ||||||
| }) | ||||||
| .catch(error => { | ||||||
| console.error(error); | ||||||
| res.status(500).json({ error: 'Internal Server Error' }); | ||||||
| }); | ||||||
| }) | ||||||
| .catch(error => { | ||||||
| console.error(error); | ||||||
| res.status(500).json({ error: 'Internal Server Error' }); | ||||||
| }); | ||||||
| } catch (error) { | ||||||
| console.error(error); | ||||||
| res.status(500).json({ error: 'Internal Server Error' }); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| const jwt = require('jsonwebtoken'); | ||
|
|
||
| function authenticateToken(res,req,next) { | ||
| const authHeader = req.headers['authorization']; | ||
| const token = authHeader && authHeader.split(' ')[1]; | ||
|
|
||
| if(!token) { | ||
| return res.sendStatus(401); // Unauthorized | ||
| } | ||
|
|
||
| jwt.verify(token, 'YOUR_SECRET_KEY', (err, user) => { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace |
||
| if (err) { | ||
| return res.sendStatus(401); // Unauthorized | ||
| } | ||
| req.user = user; | ||
| next(); | ||
| }); | ||
| } | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will need to add a method to create a JWT token |
||
| module.exports = { | ||
| authenticateToken | ||
|
|
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| module.exports = (sequelize, Sequelize) => { | ||
| const Favorite = sequelize.define('Favorite', { | ||
| id: { | ||
| primaryKey: true, | ||
| type: Sequelize.DataTypes.UUID, | ||
| defaultValue: Sequelize.DataTypes.UUIDV4 | ||
| }, | ||
| user: { | ||
| type: Sequelize.DataTypes.UUID | ||
| }, | ||
| favorite: { | ||
| type: Sequelize.DataTypes.UUID | ||
| } | ||
|
|
||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| const express = require('express'); | ||
| const router = express.Router(); | ||
| const { addFavorite, removeFavorite } = require('../controllers/favorites_controllers.js'); | ||
|
|
||
| router.post('/',addFavorite); | ||
| router.delete('/',removeFavorite); | ||
|
|
||
| module.exports = router; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| 'use strict'; | ||
|
|
||
| /** @type {import('sequelize-cli').Migration} */ | ||
| module.exports = { | ||
| async up (queryInterface, Sequelize) { | ||
| /** | ||
| * Add altering commands here. | ||
| * | ||
| * Example: | ||
| * await queryInterface.createTable('users', { id: Sequelize.INTEGER }); | ||
| */ | ||
| await queryInterface.createTable('Favorites', { | ||
| id: { | ||
| primaryKey: true, | ||
| type: Sequelize.DataTypes.UUID, | ||
| defaultValue: Sequelize.DataTypes.UUIDV4, | ||
| allowNull: false | ||
| }, | ||
| user: { | ||
| type: Sequelize.DataTypes.UUID, | ||
| allowNull: false | ||
| }, | ||
| favorite: { | ||
| type: Sequelize.DataTypes.UUID, | ||
| allowNull: false | ||
| } | ||
| }); | ||
| }, | ||
|
|
||
| async down (queryInterface, Sequelize) { | ||
| /** | ||
| * Add reverting commands here. | ||
| * | ||
| * Example: | ||
| * await queryInterface.dropTable('users'); | ||
| */ | ||
| await queryInterface.dropTable('Favorites'); | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure to call the authentication method in authentication.js