1
- import bcrypt from " bcrypt" ;
2
- import { isValidObjectId } from " mongoose" ;
1
+ import bcrypt from ' bcrypt' ;
2
+ import { isValidObjectId } from ' mongoose' ;
3
3
import {
4
4
createUser as _createUser ,
5
5
deleteUserById as _deleteUserById ,
@@ -10,15 +10,17 @@ import {
10
10
findUserByUsernameOrEmail as _findUserByUsernameOrEmail ,
11
11
updateUserById as _updateUserById ,
12
12
updateUserPrivilegeById as _updateUserPrivilegeById ,
13
- } from " ../model/repository.js" ;
13
+ } from ' ../model/repository.js' ;
14
14
15
15
export async function createUser ( req , res ) {
16
16
try {
17
17
const { username, email, password } = req . body ;
18
18
if ( username && email && password ) {
19
19
const existingUser = await _findUserByUsernameOrEmail ( username , email ) ;
20
20
if ( existingUser ) {
21
- return res . status ( 409 ) . json ( { message : "username or email already exists" } ) ;
21
+ return res
22
+ . status ( 409 )
23
+ . json ( { message : 'username or email already exists' } ) ;
22
24
}
23
25
24
26
const salt = bcrypt . genSaltSync ( 10 ) ;
@@ -29,11 +31,15 @@ export async function createUser(req, res) {
29
31
data : formatUserResponse ( createdUser ) ,
30
32
} ) ;
31
33
} else {
32
- return res . status ( 400 ) . json ( { message : "username and/or email and/or password are missing" } ) ;
34
+ return res
35
+ . status ( 400 )
36
+ . json ( { message : 'username and/or email and/or password are missing' } ) ;
33
37
}
34
38
} catch ( err ) {
35
39
console . error ( err ) ;
36
- return res . status ( 500 ) . json ( { message : "Unknown error when creating new user!" } ) ;
40
+ return res
41
+ . status ( 500 )
42
+ . json ( { message : 'Unknown error when creating new user!' } ) ;
37
43
}
38
44
}
39
45
@@ -48,22 +54,30 @@ export async function getUser(req, res) {
48
54
if ( ! user ) {
49
55
return res . status ( 404 ) . json ( { message : `User ${ userId } not found` } ) ;
50
56
} else {
51
- return res . status ( 200 ) . json ( { message : `Found user` , data : formatUserResponse ( user ) } ) ;
57
+ return res
58
+ . status ( 200 )
59
+ . json ( { message : `Found user` , data : formatUserResponse ( user ) } ) ;
52
60
}
53
61
} catch ( err ) {
54
62
console . error ( err ) ;
55
- return res . status ( 500 ) . json ( { message : "Unknown error when getting user!" } ) ;
63
+ return res
64
+ . status ( 500 )
65
+ . json ( { message : 'Unknown error when getting user!' } ) ;
56
66
}
57
67
}
58
68
59
69
export async function getAllUsers ( req , res ) {
60
70
try {
61
71
const users = await _findAllUsers ( ) ;
62
72
63
- return res . status ( 200 ) . json ( { message : `Found users` , data : users . map ( formatUserResponse ) } ) ;
73
+ return res
74
+ . status ( 200 )
75
+ . json ( { message : `Found users` , data : users . map ( formatUserResponse ) } ) ;
64
76
} catch ( err ) {
65
77
console . error ( err ) ;
66
- return res . status ( 500 ) . json ( { message : "Unknown error when getting all users!" } ) ;
78
+ return res
79
+ . status ( 500 )
80
+ . json ( { message : 'Unknown error when getting all users!' } ) ;
67
81
}
68
82
}
69
83
@@ -82,11 +96,11 @@ export async function updateUser(req, res) {
82
96
if ( username || email ) {
83
97
let existingUser = await _findUserByUsername ( username ) ;
84
98
if ( existingUser && existingUser . id !== userId ) {
85
- return res . status ( 409 ) . json ( { message : " username already exists" } ) ;
99
+ return res . status ( 409 ) . json ( { message : ' username already exists' } ) ;
86
100
}
87
101
existingUser = await _findUserByEmail ( email ) ;
88
102
if ( existingUser && existingUser . id !== userId ) {
89
- return res . status ( 409 ) . json ( { message : " email already exists" } ) ;
103
+ return res . status ( 409 ) . json ( { message : ' email already exists' } ) ;
90
104
}
91
105
}
92
106
@@ -95,25 +109,36 @@ export async function updateUser(req, res) {
95
109
const salt = bcrypt . genSaltSync ( 10 ) ;
96
110
hashedPassword = bcrypt . hashSync ( password , salt ) ;
97
111
}
98
- const updatedUser = await _updateUserById ( userId , username , email , hashedPassword ) ;
112
+ const updatedUser = await _updateUserById (
113
+ userId ,
114
+ username ,
115
+ email ,
116
+ hashedPassword ,
117
+ ) ;
99
118
return res . status ( 200 ) . json ( {
100
119
message : `Updated data for user ${ userId } ` ,
101
120
data : formatUserResponse ( updatedUser ) ,
102
121
} ) ;
103
122
} else {
104
- return res . status ( 400 ) . json ( { message : "No field to update: username and email and password are all missing!" } ) ;
123
+ return res . status ( 400 ) . json ( {
124
+ message :
125
+ 'No field to update: username and email and password are all missing!' ,
126
+ } ) ;
105
127
}
106
128
} catch ( err ) {
107
129
console . error ( err ) ;
108
- return res . status ( 500 ) . json ( { message : "Unknown error when updating user!" } ) ;
130
+ return res
131
+ . status ( 500 )
132
+ . json ( { message : 'Unknown error when updating user!' } ) ;
109
133
}
110
134
}
111
135
112
136
export async function updateUserPrivilege ( req , res ) {
113
137
try {
114
138
const { isAdmin } = req . body ;
115
139
116
- if ( isAdmin !== undefined ) { // isAdmin can have boolean value true or false
140
+ if ( isAdmin !== undefined ) {
141
+ // isAdmin can have boolean value true or false
117
142
const userId = req . params . id ;
118
143
if ( ! isValidObjectId ( userId ) ) {
119
144
return res . status ( 404 ) . json ( { message : `User ${ userId } not found` } ) ;
@@ -123,17 +148,22 @@ export async function updateUserPrivilege(req, res) {
123
148
return res . status ( 404 ) . json ( { message : `User ${ userId } not found` } ) ;
124
149
}
125
150
126
- const updatedUser = await _updateUserPrivilegeById ( userId , isAdmin === true ) ;
151
+ const updatedUser = await _updateUserPrivilegeById (
152
+ userId ,
153
+ isAdmin === true ,
154
+ ) ;
127
155
return res . status ( 200 ) . json ( {
128
156
message : `Updated privilege for user ${ userId } ` ,
129
157
data : formatUserResponse ( updatedUser ) ,
130
158
} ) ;
131
159
} else {
132
- return res . status ( 400 ) . json ( { message : " isAdmin is missing!" } ) ;
160
+ return res . status ( 400 ) . json ( { message : ' isAdmin is missing!' } ) ;
133
161
}
134
162
} catch ( err ) {
135
163
console . error ( err ) ;
136
- return res . status ( 500 ) . json ( { message : "Unknown error when updating user privilege!" } ) ;
164
+ return res
165
+ . status ( 500 )
166
+ . json ( { message : 'Unknown error when updating user privilege!' } ) ;
137
167
}
138
168
}
139
169
@@ -149,10 +179,14 @@ export async function deleteUser(req, res) {
149
179
}
150
180
151
181
await _deleteUserById ( userId ) ;
152
- return res . status ( 200 ) . json ( { message : `Deleted user ${ userId } successfully` } ) ;
182
+ return res
183
+ . status ( 200 )
184
+ . json ( { message : `Deleted user ${ userId } successfully` } ) ;
153
185
} catch ( err ) {
154
186
console . error ( err ) ;
155
- return res . status ( 500 ) . json ( { message : "Unknown error when deleting user!" } ) ;
187
+ return res
188
+ . status ( 500 )
189
+ . json ( { message : 'Unknown error when deleting user!' } ) ;
156
190
}
157
191
}
158
192
0 commit comments