Skip to content

Commit ff9713a

Browse files
committed
comment endpoints fixed
1 parent b66e4c5 commit ff9713a

File tree

6 files changed

+61
-25
lines changed

6 files changed

+61
-25
lines changed
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
1+
import Comment from '../Models/commentModel.js';
12
import commentService from '../Service/commentService.js';
23
// function to check ownership
3-
import Comment from '../Models/commentModel.js';
44
import isResourceOwner from '../helpers/isOwner.js';
55

66
const commentController = {
77
createComment,
8-
deleteComment,
98
getAllComment,
9+
deleteComment,
1010
};
1111

1212
function createComment(req, res, next) {
1313
commentService
14-
.createComment(req.user.id, req.params.id, req.body)
14+
.createComment(
15+
req.user.id,
16+
req.params.id,
17+
req.body.description,
18+
req.body.calification,
19+
)
1520
.then((comment) => res.json(comment))
1621
.catch((error) => next(error));
1722
}
1823

24+
function getAllComment(req, res, next) {
25+
commentService
26+
.getAllComment(req.query.page, req.query.size, req.params.id)
27+
.then((product) => res.json(product))
28+
.catch((error) => next(error));
29+
}
30+
1931
function deleteComment(req, res, next) {
20-
const isOwner = isResourceOwner(Comment, req.params.commentId, req.user.id);
32+
console.log(req.params.id);
33+
const isOwner = isResourceOwner(Comment, req.params.id, req.user.id);
2134
if (isOwner) {
2235
commentService
2336
.deleteComment(req.params.id)
@@ -28,10 +41,4 @@ function deleteComment(req, res, next) {
2841
}
2942
}
3043

31-
function getAllComment(req, res, next) {
32-
commentService
33-
.getAllComment(req.query.page, req.query.size)
34-
.then((product) => res.json(product))
35-
.catch((error) => next(error));
36-
}
3744
export default commentController;

server/Models/commentModel.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ const CommentSchema = new mongoose.Schema(
77
ref: 'User',
88
required: true,
99
},
10-
productId: {
11-
type: mongoose.Schema.Types.ObjectId,
12-
ref: 'Product',
13-
required: true,
14-
},
1510
calification: {
1611
type: Number,
1712
default: 5,

server/Models/productModel.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ const ProductSchema = new mongoose.Schema(
1717
maxLength: 400,
1818
required: true,
1919
},
20+
commentId: {
21+
type: mongoose.Schema.Types.ObjectId,
22+
ref: 'Comment',
23+
required: true,
24+
},
2025
size: {
2126
type: Array,
2227
required: true,

server/Routes/commentsRoutes.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import authMiddleware from '../helpers/jwt.js';
55
const router = express.Router();
66

77
router.post(
8-
'/comments/create',
8+
'/comments/create/:productId',
99
authMiddleware,
1010
cleanBody,
1111
commentController.createComment,
1212
);
13-
router.delete('/comments/delete', authMiddleware, commentController.deleteComment);
14-
router.get('/comments/getAll', authMiddleware, commentController.getAllComment);
13+
router.get('/comments/getAll/:productId', authMiddleware, commentController.getAllComment);
14+
15+
router.delete('/comments/delete/:id', authMiddleware, commentController.deleteComment);
16+
1517

1618
export default router;

server/Service/commentService.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { NotFoundError } from '../helpers/errorHandler.js';
12
import Comment from '../Models/commentModel.js';
23

34
const commentService = {
@@ -6,6 +7,29 @@ const commentService = {
67
createComment,
78
};
89

10+
// get comment by id
11+
async function getCommentById(id) {
12+
const comment = await Comment.findById(id);
13+
if (comment) {
14+
return comment;
15+
} else {
16+
return new NotFoundError(`Comment Not Found`);
17+
}
18+
}
19+
20+
// create comment
21+
async function createComment(userId, calification, description) {
22+
const newComment = new Comment({
23+
userId,
24+
calification,
25+
description,
26+
});
27+
const res = await newComment.save();
28+
29+
return res;
30+
}
31+
32+
// get products comment paginated
933
async function getAllComment(page, size) {
1034
//pagination for comments instead get all comments
1135
const pageNumber = parseInt(page) || 1;
@@ -14,14 +38,11 @@ async function getAllComment(page, size) {
1438
return await Comment.find().skip(skipCount).limit(pageSize);
1539
}
1640

41+
// delete comment by id
1742
async function deleteComment(commentId) {
18-
return await Comment.findByIdAndDelete(commentId);
19-
}
20-
21-
async function createComment(userId, body) {
22-
const newComment = new Comment({ ...body, userId });
23-
24-
return await newComment.save();
43+
await getCommentById(commentId);
44+
const res = await Comment.findByIdAndDelete(commentId);
45+
return res;
2546
}
2647

2748
export default commentService;

server/Service/shopingCartService.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const cartService = {
99
getAll,
1010
};
1111

12+
// get cart by id
1213
async function findCartById(id) {
1314
const cart = await ShoppingCart.findById(id);
1415
if (cart) {
@@ -18,11 +19,13 @@ async function findCartById(id) {
1819
}
1920
}
2021

22+
// create cart
2123
async function createCart(products, userId) {
2224
const newCart = new ShoppingCart({ products, userId });
2325
return await newCart.save();
2426
}
2527

28+
// get all carts paginated
2629
async function getAll(page, size) {
2730
// Pagination for products
2831
const actualPage = parseInt(page) || 1;
@@ -41,11 +44,13 @@ async function getAll(page, size) {
4144
return { carts, totalPages };
4245
}
4346

47+
// get user carts by user id
4448
async function getUserCart(userId) {
4549
await userService.getUser(userId);
4650
return await ShoppingCart.findOne({ userId });
4751
}
4852

53+
// edit user cart by id
4954
async function editCart(body, cartId) {
5055
await findCartById(cartId);
5156
return await ShoppingCart.findByIdAndUpdate(
@@ -57,6 +62,7 @@ async function editCart(body, cartId) {
5762
);
5863
}
5964

65+
//delete user cart by cart id
6066
async function deleteCart(cartId) {
6167
await findCartById(cartId);
6268
return await ShoppingCart.findByIdAndDelete(cartId);

0 commit comments

Comments
 (0)