-
Notifications
You must be signed in to change notification settings - Fork 3
Fixfavourites #13
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
Open
pkol92
wants to merge
7
commits into
main
Choose a base branch
from
Fixfavourites
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fixfavourites #13
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0885d67
Merge branch 'main' into feature/favouritesAndUsers
pkol92 4cea2cf
Merge branch 'main' into feature/commentsSchema
pkol92 988bfc7
comments
pkol92 84f0144
fix add comments, add remove and edit
pkol92 b2f16af
add middleware and exceptions
pkol92 5ea7be4
fix middleware
pkol92 dadadef
edit code after reviewed
pkol92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { Router, Request, Response, NextFunction } from 'express'; | ||
| import Controller from '../interfaces/controller.interface'; | ||
| import authMiddleware from '../middleware/auth.middleware'; | ||
| import restaurantModel from '../models/restaurant.model'; | ||
| import RestaurantNotFoundException from '../exceptions/RestaurantNotFoundException'; | ||
| import validationMiddleware from '../middleware/validation.middleware'; | ||
| import WrongCredentialsException from '../exceptions/WrongCredentialsException'; | ||
| import commentsModel from '../models/comments.model'; | ||
| import CreateCommentDto from '../dto/comment.dto'; | ||
| import userModel from '../models/user.model'; | ||
| import RequestWithUser from '../interfaces/requestWithUser.interface'; | ||
| import CommentIsNotExist from '../exceptions/CommentIsNotExist'; | ||
| import CommentTooShort from '../exceptions/CommentTooShort'; | ||
| import permissionMiddleware from '../middleware/permission.middleware'; | ||
| import commentDeleteMiddleware from '../middleware/commentDelete.middleware'; | ||
| import commentEditMiddleware from '../middleware/editComment.middleware'; | ||
|
|
||
| class CommentsController implements Controller { | ||
| public path = '/comments'; | ||
| public router = Router(); | ||
| private restaurant = restaurantModel; | ||
| private comment = commentsModel; | ||
| private user = userModel; | ||
|
|
||
| constructor() { | ||
| this.initializeRoutes(); | ||
| } | ||
|
|
||
| private initializeRoutes() { | ||
| this.router.get(`${this.path}`, authMiddleware, permissionMiddleware, this.getComments); | ||
| this.router.get(`${this.path}/:id`, authMiddleware, permissionMiddleware, this.getCommentsById); | ||
| this.router.put( | ||
| `${this.path}/:restaurantId`, | ||
| authMiddleware, | ||
| validationMiddleware(CreateCommentDto), | ||
| this.createComment | ||
| ); | ||
| this.router.patch(`${this.path}/:id`, authMiddleware, commentEditMiddleware, this.editComment); | ||
| this.router.delete(`${this.path}/:id`, authMiddleware, commentDeleteMiddleware, this.deleteComment); | ||
| } | ||
|
|
||
| private getComments = async (request: Request, response: Response, next: NextFunction) => { | ||
| const comments = await this.comment.find(); | ||
| comments ? response.send(comments) : next(new CommentIsNotExist()); | ||
| }; | ||
|
|
||
| private getCommentsById = async (request: Request, response: Response, next: NextFunction) => { | ||
| const id = request.params.id; | ||
| try { | ||
| const comment = await this.comment.findById(id).populate('restaurant', 'name').populate('user', 'lastName'); | ||
| comment ? response.send(comment) : next(new CommentIsNotExist()); | ||
| } catch { | ||
| next(new CommentIsNotExist()); | ||
| } | ||
| }; | ||
|
|
||
| private createComment = async (request: RequestWithUser, response: Response, next: NextFunction) => { | ||
| try { | ||
| const restaurantId: string = request.params.restaurantId; | ||
| if (!restaurantId) { | ||
| return next(new RestaurantNotFoundException(restaurantId)); | ||
| } | ||
|
|
||
| const restaurant = await this.restaurant.findById(restaurantId); | ||
| if (!restaurant) { | ||
| return next(new RestaurantNotFoundException(restaurantId)); | ||
| } | ||
| const user = await this.user.findById(request.user._id); | ||
| const postComment: CreateCommentDto = request.body; | ||
| if (postComment.comment.length <2) { | ||
| return next(new CommentTooShort()); | ||
| } | ||
| const createdComment = await this.comment.create({ | ||
| ...postComment, | ||
| user: [request.user._id], | ||
| restaurant: [request.params.restaurantId], | ||
| }); | ||
|
|
||
| const restaurantsComments = restaurant.get('comments', null, {getters: false}); | ||
| const userComments = user.get('comments', null, {getters: false}); | ||
| userComments.push(createdComment); | ||
| restaurantsComments.push(createdComment); | ||
| const savedComment = await createdComment.save(); | ||
| await savedComment.populate('restaurant','name').execPopulate(); | ||
| await savedComment.populate('user', 'lastName').execPopulate(); | ||
| await this.restaurant.findByIdAndUpdate(restaurantId, { | ||
| comments: restaurant.comments | ||
| }, { new: true }); | ||
| await this.user.findByIdAndUpdate(request.user._id, { | ||
| comments: user.comments | ||
| }, { new: true }); | ||
| response.send(savedComment); | ||
| } catch { | ||
| next(new CommentIsNotExist()); | ||
| } | ||
| }; | ||
|
|
||
| private deleteComment = async (request: RequestWithUser, response: Response, next: NextFunction) => { | ||
| const id = request.params.id; | ||
| try { | ||
| await this.comment.findByIdAndDelete(id, { ...request.body }, (err, comment) => { | ||
| !err ? response.send(comment) : next(new CommentIsNotExist()); | ||
| }); | ||
| } catch { | ||
| next(new WrongCredentialsException()); | ||
| } | ||
| }; | ||
|
|
||
| private editComment = async (request: RequestWithUser, response: Response, next: NextFunction) => { | ||
| const id = request.params.id; | ||
| const dataToEdit = request.body; | ||
| try { | ||
| if (dataToEdit.comment.length < 2) { | ||
| return next(new CommentTooShort()); | ||
| } | ||
| const comment = await this.comment.findByIdAndUpdate(id, { ...dataToEdit }, { new: true }) | ||
| response.send({comment}); | ||
| } catch { | ||
| next(new CommentIsNotExist()); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| export default CommentsController; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // @ts-ignore | ||
| import { IsString } from 'class-validator'; | ||
|
|
||
| class CreateCommentDto { | ||
| @IsString() | ||
| public comment: string; | ||
| } | ||
|
|
||
| export default CreateCommentDto; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import HttpException from './HttpException'; | ||
|
|
||
| class CommentIsNotExist extends HttpException { | ||
| constructor() { | ||
| const idErrorMessage = `There is no such comment`; | ||
| super(400, idErrorMessage); | ||
| } | ||
| } | ||
|
|
||
| export default CommentIsNotExist; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import HttpException from './HttpException'; | ||
|
|
||
| class CommentTooShort extends HttpException { | ||
| constructor() { | ||
| const idErrorMessage = `The comment must have more than one letter`; | ||
| super(400, idErrorMessage); | ||
| } | ||
| } | ||
|
|
||
| export default CommentTooShort; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import HttpException from './HttpException'; | ||
|
|
||
| class UserDoesNotHavePermissionToDeleteThisComment extends HttpException { | ||
| constructor() { | ||
| const idErrorMessage = `User does not have permission to delete this comment`; | ||
| super(403, idErrorMessage); | ||
| } | ||
| } | ||
|
|
||
| export default UserDoesNotHavePermissionToDeleteThisComment; |
10 changes: 10 additions & 0 deletions
10
server/src/exceptions/UserDoesNotPermissiontoEditThisComment.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import HttpException from './HttpException'; | ||
|
|
||
| class UserDoesNotHavePermissionToEditThisComment extends HttpException { | ||
| constructor() { | ||
| const idErrorMessage = `User does not have permission to edit this comment`; | ||
| super(403, idErrorMessage); | ||
| } | ||
| } | ||
|
|
||
| export default UserDoesNotHavePermissionToEditThisComment; |
10 changes: 10 additions & 0 deletions
10
server/src/exceptions/UserDoesNotPerrmissionToEditFavourites.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import HttpException from './HttpException'; | ||
|
|
||
| class UserDoesNotHavePermissionToEditThisFavourtie extends HttpException { | ||
| constructor() { | ||
| const idErrorMessage = `User does not have permission to edit this favourites`; | ||
| super(403, idErrorMessage); | ||
| } | ||
| } | ||
|
|
||
| export default UserDoesNotHavePermissionToEditThisFavourtie; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| interface Comments { | ||
| _id: string, | ||
| comment: string, | ||
| user: string | ||
| restaurant: string | ||
| } | ||
|
|
||
| export default Comments; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import RequestWithUser from '../interfaces/requestWithUser.interface'; | ||
| import { NextFunction, Response } from 'express'; | ||
| import UserDoesNotHavePermissionToExecutedRequestedData from '../exceptions/UserDoesNotHavePermissionToExecutedRequestedData'; | ||
| import commentsModel from '../models/comments.model'; | ||
| import CommentIsNotExist from '../exceptions/CommentIsNotExist'; | ||
| import UserDoesNotHavePermissionToDeleteThisComment from '../exceptions/UserDoesNotDeleteThisComment'; | ||
|
|
||
| async function commentDeleteMiddleware(request: RequestWithUser, response: Response, next: NextFunction) { | ||
| const commentId = request.params.id; | ||
| let selectedComment = null; | ||
| const userRole = request.user.userRole; | ||
| const userId = request.user._id; | ||
| const comment = commentsModel; | ||
|
|
||
| try { | ||
| selectedComment = await comment.findById(commentId); | ||
| const commentOwner = selectedComment.user !== null && selectedComment.user; | ||
| if ( userRole === 2) { | ||
| commentOwner.toString() === userId.toString() | ||
| ? next() | ||
| : next(new UserDoesNotHavePermissionToDeleteThisComment()); | ||
| } else if ( userRole === 0) { | ||
| next(); | ||
| } else { | ||
| next(new UserDoesNotHavePermissionToExecutedRequestedData()); | ||
| } | ||
| } catch (error) { | ||
| next(new CommentIsNotExist()); | ||
| } | ||
| } | ||
|
|
||
| export default commentDeleteMiddleware; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,30 @@ | ||||||
| import RequestWithUser from '../interfaces/requestWithUser.interface'; | ||||||
| import { NextFunction, Response } from 'express'; | ||||||
| import UserDoesNotHavePermissionToExecutedRequestedData from '../exceptions/UserDoesNotHavePermissionToExecutedRequestedData'; | ||||||
| import commentsModel from '../models/comments.model'; | ||||||
| import CommentIsNotExist from '../exceptions/CommentIsNotExist'; | ||||||
| import UserDoesNotHavePermissionToEditThisComment from '../exceptions/UserDoesNotPermissiontoEditThisComment'; | ||||||
|
|
||||||
| async function commentEditMiddleware(request: RequestWithUser, response: Response, next: NextFunction) { | ||||||
| const commentId = request.params.id; | ||||||
| let selectedComment = null; | ||||||
| const userRole = request.user.userRole; | ||||||
| const userId = request.user._id; | ||||||
| const comment = commentsModel; | ||||||
|
|
||||||
| try { | ||||||
| selectedComment = await comment.findById(commentId); | ||||||
| const commentOwner = selectedComment.user !== null && selectedComment.user; | ||||||
| if ( userRole === 2) { | ||||||
|
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
We have to make a map that will hold these numeric values |
||||||
| commentOwner.toString() === userId.toString() | ||||||
| ? next() | ||||||
| : next (new UserDoesNotHavePermissionToEditThisComment()); | ||||||
| } else { | ||||||
| next (new UserDoesNotHavePermissionToExecutedRequestedData()); | ||||||
| } | ||||||
| } catch (error) { | ||||||
| next(new CommentIsNotExist()); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export default commentEditMiddleware; | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There is a lot of code that we have to test.