-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreviews.js
93 lines (82 loc) · 2 KB
/
reviews.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'use strict'
const epilogue = require('./epilogue');
const db = require('APP/db');
const { Review, Product, User } = require('APP/db/models');
const customReviewsRoutes = require('express').Router();
// Reviews GET
customReviewsRoutes.get('/', function (request, response, next) {
Review.findAll({
include: [Product, User]
})
.then((reviewArray) => {
response.json(reviewArray);
})
.catch(next);
});
customReviewsRoutes.get('/:reviewId', function (request, response, next) {
Review.findOne({
include: [Product, User],
where: {
id: request.params.reviewId
}
})
.then((review) => {
response.json(review);
})
.catch(next);
});
// Reviews POST
// Request.body must have
// - text: text with minimum of 10 characters
// - rating: number (which is a string)
// - user_id: is an id (id is a string)
// - product_id: is an id (id is a string)
customReviewsRoutes.post('/', function (request, response, next) {
Review.create({
text: request.body.text,
rating: request.body.rating,
user_id: request.body.user_id,
product_id: request.body.product_id
})
.then((review) => {
response.sendStatus(200);
})
.catch(next);
});
// Reviews Update
customReviewsRoutes.put('/:reviewId', function (request, response, next) {
Review.update(request.body, {
where: {
id: request.params.reviewId
}
})
.then((updatedReview) => {
response.sendStatus(200);
})
.catch(next);
});
// Review Delete
customReviewsRoutes.delete('/:reviewId', function (request, response, next) {
Review.destroy({
where: {
id: request.params.reviewId
}
})
.then((deleted) => {
response.sendStatus(200);
})
.catch(next);
});
// route to show reviews on user account page
customReviewsRoutes.get('/users/:userId', function (request, response, next) {
Review.findAll({
where :{
user_id:request.params.userId
}
})
.then((reviewArray) => {
response.json(reviewArray);
})
.catch(next);
});
module.exports = customReviewsRoutes;