1
1
const ClothingItem = require ( "../models/clothingItem" ) ;
2
- const {
3
- invalidDataError,
4
- notFoundError,
5
- defaultError,
6
- forbiddenError,
7
- } = require ( "../utils/errors" ) ;
2
+ // const {
3
+ // invalidDataError,
4
+ // notFoundError,
5
+ // defaultError,
6
+ // forbiddenError,
7
+ // } = require("../utils/errors");
8
+ const BadRequestError = require ( "../errors/bad-request-err" ) ;
9
+ const NotFoundError = require ( "../errors/not-found-err" ) ;
10
+ const ForbiddenError = require ( "../errors/forbidden-err" ) ;
8
11
9
- const getClothingItem = ( req , res ) => {
12
+ const getClothingItem = ( req , res , next ) => {
10
13
ClothingItem . find ( { } )
11
14
. orFail ( )
12
15
. then ( ( item ) => res . status ( 200 ) . send ( item ) )
13
16
. catch ( ( err ) => {
14
17
console . log ( err . name ) ;
15
- res
16
- . status ( defaultError )
17
- . send ( { message : "An error has occurred on the server" } ) ;
18
+ next ( err ) ;
19
+ // res
20
+ // .status(defaultError)
21
+ // .send({ message: "An error has occurred on the server" });
18
22
} ) ;
19
23
} ;
20
24
21
- const createClothingItem = ( req , res ) => {
25
+ const createClothingItem = ( req , res , next ) => {
22
26
const { _id } = req . user ;
23
27
const { name, weather, imageUrl } = req . body ;
24
28
ClothingItem . create ( { name, weather, imageUrl, owner : _id } )
25
29
. then ( ( item ) => res . status ( 201 ) . send ( item ) )
26
30
. catch ( ( err ) => {
27
31
console . log ( err . name ) ;
28
32
if ( err . name === "ValidationError" ) {
29
- return res
30
- . status ( invalidDataError )
31
- . send ( { message : "Bad Request! Invalid data passed" } ) ;
33
+ next ( new BadRequestError ( "Bad Request! Invalid data passed" ) ) ;
34
+ // return res
35
+ // .status(invalidDataError)
36
+ // .send({ message: "Bad Request! Invalid data passed" });
37
+ } else {
38
+ next ( err ) ;
32
39
}
33
- return res
34
- . status ( defaultError )
35
- . send ( { message : "An error has occurred on the server" } ) ;
40
+ // return res
41
+ // .status(defaultError)
42
+ // .send({ message: "An error has occurred on the server" });
36
43
} ) ;
37
44
} ;
38
45
39
- const deleteClothingItem = ( req , res ) => {
46
+ const deleteClothingItem = ( req , res , next ) => {
40
47
const { _id } = req . user ;
41
48
42
49
ClothingItem . findById ( req . params . itemId )
43
50
. orFail ( )
44
51
. then ( ( item ) => {
45
52
if ( item . owner . toString ( ) !== _id ) {
46
- return res
47
- . status ( forbiddenError )
48
- . send ( { message : "Request Was Forbidden" } ) ;
53
+ next ( new ForbiddenError ( "Request Was Forbidden" ) ) ;
54
+ // return res
55
+ // .status(forbiddenError)
56
+ // .send({ message: "Request Was Forbidden" });
49
57
}
50
58
return ClothingItem . findByIdAndRemove ( req . params . itemId ) . then ( ( ) =>
51
59
res . send ( { message : "Item successfully deleted" , item } )
@@ -54,22 +62,27 @@ const deleteClothingItem = (req, res) => {
54
62
. catch ( ( err ) => {
55
63
console . log ( err . name ) ;
56
64
if ( err . name === "CastError" ) {
57
- return res
58
- . status ( invalidDataError )
59
- . send ( { message : "Bad Request! Invalid data passed" } ) ;
65
+ next ( new BadRequestError ( "Bad Request! Invalid data passed" ) ) ;
66
+ // return res
67
+ // .status(invalidDataError)
68
+ // .send({ message: "Bad Request! Invalid data passed" });
69
+ } else if ( err . name === "DocumentNotFoundError" ) {
70
+ next (
71
+ new NotFoundError ( " The request was sent to a non-existent address" )
72
+ ) ;
73
+ // return res
74
+ // .status(notFoundError)
75
+ // .send({ message: " The request was sent to a non-existent address" });
76
+ } else {
77
+ next ( err ) ;
60
78
}
61
- if ( err . name === "DocumentNotFoundError" ) {
62
- return res
63
- . status ( notFoundError )
64
- . send ( { message : " The request was sent to a non-existent address" } ) ;
65
- }
66
- return res
67
- . status ( defaultError )
68
- . send ( { message : "An error has occurred on the server" } ) ;
79
+ // return res
80
+ // .status(defaultError)
81
+ // .send({ message: "An error has occurred on the server" });
69
82
} ) ;
70
83
} ;
71
84
72
- const likeItem = ( req , res ) => {
85
+ const likeItem = ( req , res , next ) => {
73
86
ClothingItem . findByIdAndUpdate (
74
87
req . params . itemId ,
75
88
{ $addToSet : { likes : req . user . _id } } ,
@@ -80,22 +93,27 @@ const likeItem = (req, res) => {
80
93
. catch ( ( err ) => {
81
94
console . error ( err ) ;
82
95
if ( err . name === "CastError" ) {
83
- return res
84
- . status ( invalidDataError )
85
- . send ( { message : "Bad Request! Invalid data passed" } ) ;
86
- }
87
- if ( err . name === "DocumentNotFoundError" ) {
88
- return res
89
- . status ( notFoundError )
90
- . send ( { message : " The request was sent to a non-existent address" } ) ;
96
+ next ( new BadRequestError ( "Bad Request! Invalid data passed" ) ) ;
97
+ // return res
98
+ // .status(invalidDataError)
99
+ // .send({ message: "Bad Request! Invalid data passed" });
100
+ } else if ( err . name === "DocumentNotFoundError" ) {
101
+ next (
102
+ new NotFoundError ( " The request was sent to a non-existent address" )
103
+ ) ;
104
+ // return res
105
+ // .status(notFoundError)
106
+ // .send({ message: " The request was sent to a non-existent address" });
107
+ } else {
108
+ next ( err ) ;
91
109
}
92
- return res
93
- . status ( defaultError )
94
- . send ( { message : "An error has occurred on the server" } ) ;
110
+ // return res
111
+ // .status(defaultError)
112
+ // .send({ message: "An error has occurred on the server" });
95
113
} ) ;
96
114
} ;
97
115
98
- const dislikeItem = ( req , res ) => {
116
+ const dislikeItem = ( req , res , next ) => {
99
117
ClothingItem . findByIdAndUpdate (
100
118
req . params . itemId ,
101
119
{ $pull : { likes : req . user . _id } } ,
@@ -106,18 +124,23 @@ const dislikeItem = (req, res) => {
106
124
. catch ( ( err ) => {
107
125
console . log ( err . name ) ;
108
126
if ( err . name === "CastError" ) {
109
- return res
110
- . status ( invalidDataError )
111
- . send ( { message : "Bad Request! Invalid data passed" } ) ;
112
- }
113
- if ( err . name === "DocumentNotFoundError" ) {
114
- return res
115
- . status ( notFoundError )
116
- . send ( { message : " The request was sent to a non-existent address" } ) ;
127
+ next ( new BadRequestError ( "Bad Request! Invalid data passed" ) ) ;
128
+ // return res
129
+ // .status(invalidDataError)
130
+ // .send({ message: "Bad Request! Invalid data passed" });
131
+ } else if ( err . name === "DocumentNotFoundError" ) {
132
+ next (
133
+ new NotFoundError ( " The request was sent to a non-existent address" )
134
+ ) ;
135
+ // return res
136
+ // .status(notFoundError)
137
+ // .send({ message: " The request was sent to a non-existent address" });
138
+ } else {
139
+ next ( err ) ;
117
140
}
118
- return res
119
- . status ( defaultError )
120
- . send ( { message : "An error has occurred on the server" } ) ;
141
+ // return res
142
+ // .status(defaultError)
143
+ // .send({ message: "An error has occurred on the server" });
121
144
} ) ;
122
145
} ;
123
146
0 commit comments