1
1
import { BaseContext } from 'koa' ;
2
- import { getManager , Repository } from 'typeorm' ;
2
+ import { getManager , Repository , Not , Equal } from 'typeorm' ;
3
3
import { validate , ValidationError } from 'class-validator' ;
4
4
import { User } from '../entity/user' ;
5
5
@@ -13,7 +13,8 @@ export default class UserController {
13
13
// load all users
14
14
const users : User [ ] = await userRepository . find ( ) ;
15
15
16
- // return loaded users
16
+ // return OK status code and loaded users array
17
+ ctx . status = 200 ;
17
18
ctx . body = users ;
18
19
}
19
20
@@ -23,10 +24,11 @@ export default class UserController {
23
24
const userRepository : Repository < User > = getManager ( ) . getRepository ( User ) ;
24
25
25
26
// load user by id
26
- const user : User = await userRepository . findOne ( ctx . params . id ) ;
27
+ const user : User = await userRepository . findOne ( + ctx . params . id || 0 ) ;
27
28
28
29
if ( user ) {
29
- // return loaded user
30
+ // return OK status code and loaded user object
31
+ ctx . status = 200 ;
30
32
ctx . body = user ;
31
33
} else {
32
34
// return a BAD REQUEST status code and error message
@@ -50,13 +52,17 @@ export default class UserController {
50
52
const errors : ValidationError [ ] = await validate ( userToBeSaved ) ; // errors is an array of validation errors
51
53
52
54
if ( errors . length > 0 ) {
53
- // return bad request status code and errors array
55
+ // return BAD REQUEST status code and errors array
54
56
ctx . status = 400 ;
55
57
ctx . body = errors ;
58
+ } else if ( await userRepository . findOne ( { email : userToBeSaved . email } ) ) {
59
+ // return BAD REQUEST status code and email already exists error
60
+ ctx . status = 400 ;
61
+ ctx . body = 'The specified e-mail address already exists' ;
56
62
} else {
57
63
// save the user contained in the POST body
58
64
const user = await userRepository . save ( userToBeSaved ) ;
59
- // return created status code and updated user
65
+ // return CREATED status code and updated user
60
66
ctx . status = 201 ;
61
67
ctx . body = user ;
62
68
}
@@ -67,34 +73,35 @@ export default class UserController {
67
73
// get a user repository to perform operations with user
68
74
const userRepository : Repository < User > = getManager ( ) . getRepository ( User ) ;
69
75
70
- // check if a user with the specified id exists
71
- if ( await userRepository . findOne ( ctx . params . id ) ) {
72
- // update the user by specified id
73
- // build up entity user to be updated
74
- const userToBeUpdated : User = new User ( ) ;
75
- userToBeUpdated . id = + ctx . params . id ;
76
- userToBeUpdated . name = ctx . request . body . name ;
77
- userToBeUpdated . email = ctx . request . body . email ;
78
-
79
- // validate user entity
80
- const errors : ValidationError [ ] = await validate ( userToBeUpdated ) ; // errors is an array of validation errors
81
-
82
- if ( errors . length > 0 ) {
83
- // return bad request status code and errors array
84
- ctx . status = 400 ;
85
- ctx . body = errors ;
86
- } else {
87
- // save the user contained in the PUT body
88
- const user = await userRepository . save ( userToBeUpdated ) ;
89
- // return created status code and updated user
90
- ctx . status = 201 ;
91
- ctx . body = user ;
92
- }
76
+ // update the user by specified id
77
+ // build up entity user to be updated
78
+ const userToBeUpdated : User = new User ( ) ;
79
+ userToBeUpdated . id = + ctx . params . id || 0 ; // will always have a number, this will avoid errors
80
+ userToBeUpdated . name = ctx . request . body . name ;
81
+ userToBeUpdated . email = ctx . request . body . email ;
93
82
94
- } else {
83
+ // validate user entity
84
+ const errors : ValidationError [ ] = await validate ( userToBeUpdated ) ; // errors is an array of validation errors
85
+
86
+ if ( errors . length > 0 ) {
87
+ // return BAD REQUEST status code and errors array
88
+ ctx . status = 400 ;
89
+ ctx . body = errors ;
90
+ } else if ( ! await userRepository . findOne ( userToBeUpdated . id ) ) {
91
+ // check if a user with the specified id exists
95
92
// return a BAD REQUEST status code and error message
96
93
ctx . status = 400 ;
97
94
ctx . body = 'The user you are trying to update doesn\'t exist in the db' ;
95
+ } else if ( await userRepository . findOne ( { id : Not ( Equal ( userToBeUpdated . id ) ) , email : userToBeUpdated . email } ) ) {
96
+ // return BAD REQUEST status code and email already exists error
97
+ ctx . status = 400 ;
98
+ ctx . body = 'The specified e-mail address already exists' ;
99
+ } else {
100
+ // save the user contained in the PUT body
101
+ const user = await userRepository . save ( userToBeUpdated ) ;
102
+ // return CREATED status code and updated user
103
+ ctx . status = 201 ;
104
+ ctx . body = user ;
98
105
}
99
106
100
107
}
@@ -105,16 +112,21 @@ export default class UserController {
105
112
const userRepository = getManager ( ) . getRepository ( User ) ;
106
113
107
114
// find the user by specified id
108
- const userToRemove : User = await userRepository . findOne ( ctx . params . id ) ;
109
- if ( userToRemove ) {
115
+ const userToRemove : User = await userRepository . findOne ( + ctx . params . id || 0 ) ;
116
+ if ( ! userToRemove ) {
117
+ // return a BAD REQUEST status code and error message
118
+ ctx . status = 400 ;
119
+ ctx . body = 'The user you are trying to delete doesn\'t exist in the db' ;
120
+ } else if ( + ctx . state . user . id !== userToRemove . id ) {
121
+ // check user's token id and user id are the same
122
+ // if not, return a FORBIDDEN status code and error message
123
+ ctx . status = 403 ;
124
+ ctx . body = 'A user can only be deleted by himself' ;
125
+ } else {
110
126
// the user is there so can be removed
111
127
await userRepository . remove ( userToRemove ) ;
112
128
// return a NO CONTENT status code
113
129
ctx . status = 204 ;
114
- } else {
115
- // return a BAD REQUEST status code and error message
116
- ctx . status = 400 ;
117
- ctx . body = 'The user you are trying to delete doesn\'t exist in the db' ;
118
130
}
119
131
120
132
}
0 commit comments