1
1
package com .example .demo .controller ;
2
2
3
-
3
+ import java . util . ArrayList ;
4
4
import java .util .List ;
5
5
6
6
import org .bson .types .ObjectId ;
13
13
import org .springframework .web .bind .annotation .RestController ;
14
14
15
15
import com .example .demo .converter .ConverterFacade ;
16
-
16
+ import com . example . demo . model . Authority ;
17
17
import com .example .demo .model .Store ;
18
18
import com .example .demo .model .User ;
19
19
import com .example .demo .data .provider .StoreManager ;
@@ -34,130 +34,138 @@ public class SecuredController {
34
34
private final ConverterFacade converterFacade ;
35
35
@ Autowired
36
36
private StoreManager storeManager ;
37
-
37
+
38
38
@ Autowired
39
- public SecuredController (final UserService service , final ConverterFacade converterFacade ) {
39
+ public SecuredController (final UserService service , final ConverterFacade converterFacade ) {
40
40
this .service = service ;
41
41
this .converterFacade = converterFacade ;
42
42
}
43
-
44
- @ RequestMapping (method = RequestMethod .GET )
45
- public ResponseEntity <?> sayHello () {
46
- return new ResponseEntity <>("Secured hello!" , HttpStatus .OK );
47
- }
48
-
43
+
44
+ @ RequestMapping (method = RequestMethod .GET )
45
+ public ResponseEntity <?> sayHello () {
46
+ return new ResponseEntity <>("Secured hello!" , HttpStatus .OK );
47
+ }
48
+
49
49
@ RequestMapping (value = "/list" , method = RequestMethod .GET )
50
50
List <User > listAllUsers () {
51
51
return service .findAll ();
52
52
}
53
-
53
+
54
54
/**
55
55
*
56
56
* @param dto
57
57
* @return
58
58
*
59
- {
60
- "_id": "5ca97198bee20412a4f0ffe4",
61
- "username": "halo23",
62
- "password": "halo23",
63
- "store_id": "5ca97198bee20412a4f0ffe2",
64
- "contactNumber": "6262678982",
65
- "role": "ROLE_ADMIN",
66
-
67
- "enabled": true
68
-
69
- }
70
-
59
+ {
60
+ "_id": "5ca97198bee20412a4f0ffe4", "username": "halo23",
61
+ "password": "halo23", "store_id": "5ca97198bee20412a4f0ffe2",
62
+ "contactNumber": "6262678982", "role": "ROLE_ADMIN", "email":
63
+ "[email protected] ", "enabled": true
64
+ }
65
+ *
71
66
*/
72
67
@ RequestMapping (value = "/update" , method = RequestMethod .POST )
73
- public ResponseEntity <?> updateUser (@ RequestBody final RegisterDTO dto ){
74
-
75
- User user = converterFacade .convertRegisterDTO (dto );
68
+ public ResponseEntity <?> updateUser (@ RequestBody final RegisterDTO dto ) {
69
+
70
+ final User user = new User ();
71
+
72
+ user .setUsername (dto .getUsername ());
73
+ user .setPassword (dto .getPassword ());
74
+ user .setAccountNonExpired (false );
75
+ user .setCredentialsNonExpired (false );
76
+ user .setEnabled (true );
77
+
78
+ List <Authority > authorities = new ArrayList <>();
79
+ if (user .getRole ().equals ("ROLE_USER" ))
80
+ authorities .add (Authority .ROLE_USER );
81
+ else if (user .getRole ().equals ("ROLE_ADMIN" ))
82
+ authorities .add (Authority .ROLE_ADMIN );
83
+ else if (user .getRole ().equals ("ANONYMOUS" ))
84
+ authorities .add (Authority .ANONYMOUS );
85
+
86
+ user .setAuthorities (authorities );
87
+ // User user = converterFacade.convertRegisterDTO(dto);
76
88
ObjectId key = new ObjectId (user .get_id ());
77
89
return new ResponseEntity <>(service .update (key , user ), HttpStatus .OK );
78
-
90
+
79
91
}
80
92
81
93
/**
82
94
*
83
95
* @param dto
84
- * @return
85
- {
86
- "_id": "5ca97198bee20412a4f0ffe4"
87
-
88
- }
96
+ * @return { "_id": "5ca97198bee20412a4f0ffe4"
97
+ *
98
+ * }
89
99
*/
90
- @ RequestMapping (value = "/delete" , method = RequestMethod .DELETE )
91
- public ResponseEntity <?> deleteUser (@ RequestBody final IndexDTO dto ){
100
+ @ RequestMapping (value = "/delete" , method = RequestMethod .POST )
101
+ public ResponseEntity <?> deleteUser (@ RequestBody final IndexDTO dto ) {
92
102
String index = service .delete (dto .get_id ());
93
103
return new ResponseEntity <>(index , HttpStatus .OK );
94
-
104
+
95
105
}
106
+
96
107
/**
97
108
*
98
109
* @param dto
99
110
* @return
100
-
101
- {
102
- "_id": "5ca6a49e920ede02679e43a7"
103
- }
104
-
111
+ *
112
+ * { "_id": "5ca6a49e920ede02679e43a7" }
113
+ *
105
114
*/
106
115
@ RequestMapping (value = "/get" , method = RequestMethod .GET )
107
- public ResponseEntity <?> getUser (@ RequestBody final IndexDTO dto ){
116
+ public ResponseEntity <?> getUser (@ RequestBody final IndexDTO dto ) {
108
117
User find = service .find (dto .get_id ());
109
- return new ResponseEntity <>(find , HttpStatus .OK );
118
+ return new ResponseEntity <>(find , HttpStatus .OK );
110
119
}
111
120
112
121
/**
113
122
*
114
123
* @param dto
115
124
* @return
116
125
*
117
- * it doesn't update the store information
118
- * it requires to have store_id
126
+ * it doesn't update the store information it requires to have store_id
119
127
*
120
-
121
- jason input pattern 1
122
-
123
- {
124
- "username": "halo9",
125
- "password": "halo9",
126
- "contactNumber":"6262678982",
127
- "role": "ROLE_ADMIN",
128
-
129
- "store_id": "5ca96bdfbee204128f3762da",
130
- "store":{
131
- }
132
- }
133
-
134
- jason input pattern 2
135
- {
136
- "username": "halo5",
137
- "password": "halo5",
138
- "contactNumber":"6262678982",
139
- "role": "ROLE_ADMIN",
140
-
141
- "store":{
142
- "name": "new Xxxx-v5",
143
- "pictureFileName": "sdfa",
144
- "address": "asdfa",
145
- "zipcode": "afdsa",
146
- "city": "adfadfs",
147
- "state": "afdsasf",
148
- "geolocation":{
149
- "latitude": 23.229999542236328,
150
- "longitude": 32.22999954223633
151
- },
152
- "storeAddress": "asdfa adfadfs afdsa , afdsasf"
153
- }
154
-
155
- }
128
+ *
129
+ jason input pattern 1
130
+
131
+ {
132
+ "username": "halo9",
133
+ "password": "halo9",
134
+ "contactNumber":"6262678982",
135
+ "role": "ROLE_ADMIN",
136
+
137
+ "store_id": "5ca96bdfbee204128f3762da",
138
+ "store":{ }
139
+ }
140
+
141
+ jason input pattern 2
142
+
143
+ {
144
+ "username": "halo5",
145
+ "password": "halo5",
146
+ "contactNumber":"6262678982",
147
+ "role": "ROLE_ADMIN",
148
+
149
+ "store":{
150
+ "name": "new Xxxx-v5",
151
+ "pictureFileName":"sdfa",
152
+ "address": "asdfa",
153
+ "zipcode": "afdsa",
154
+ "city": "adfadfs",
155
+ "state": "afdsasf",
156
+ "geolocation":{
157
+ "latitude": 23.229999542236328,
158
+ "longitude": 32.22999954223633
159
+ },
160
+ "storeAddress": "asdfa adfadfsafdsa , afdsasf"
161
+ }
162
+
163
+ }
156
164
*/
157
-
165
+
158
166
@ RequestMapping (value = "/addUserToStore" , method = RequestMethod .POST )
159
- public ResponseEntity <?> addUserToAStore (@ RequestBody final RegisterDTO dto ){
160
-
167
+ public ResponseEntity <?> addUserToAStore (@ RequestBody final RegisterDTO dto ) {
168
+
161
169
User user = converterFacade .convertRegisterDTO (dto );
162
170
return new ResponseEntity <>(service .create (user ), HttpStatus .OK );
163
171
}
@@ -169,35 +177,29 @@ public ResponseEntity<?> addUserToAStore(@RequestBody final RegisterDTO dto){
169
177
* @throws StoreDuplicateItemException
170
178
*
171
179
*
172
-
173
- {
174
- "username": "halo10",
175
- "password": "halo10",
176
- "contactNumber":"6262678982",
177
- "role": "ROLE_ADMIN",
178
-
179
- "store":{
180
- "name": "new Xxxx-v10",
181
- "pictureFileName": "sdfa",
182
- "address": "asdfa",
183
- "zipcode": "afdsa",
184
- "city": "adfadfs",
185
- "state": "afdsasf",
186
- "geolocation":{
187
- "latitude": 23.229999542236328,
188
- "longitude": 32.22999954223633
189
- },
190
- "storeAddress": "asdfa adfadfs afdsa, afdsasf"
191
- }
192
-
193
- }
194
-
195
-
180
+ *
181
+ {
182
+ "username": "halo10", "password":
183
+ "halo10", "contactNumber":"6262678982",
184
+ "role": "ROLE_ADMIN", "email":
185
+ "[email protected] ", "store":{ "name": "new
186
+ Xxxx-v10", "pictureFileName": "sdfa",
187
+ "address": "asdfa", "zipcode": "afdsa",
188
+ "city": "adfadfs", "state": "afdsasf",
189
+ "geolocation":{
190
+ "latitude":23.229999542236328,
191
+ "longitude":32.22999954223633
192
+ },
193
+ "storeAddress":"asdfa adfadfs afdsa, afdsasf"
194
+ }
195
+ }
196
+ *
197
+ *
196
198
*
197
199
*/
198
200
@ RequestMapping (value = "/addNewStoreAndNewUser" , method = RequestMethod .POST )
199
201
public ResponseEntity <?> addNewStore (@ RequestBody final RegisterDTO dto ) throws StoreDuplicateItemException {
200
-
202
+
201
203
ObjectId store_id = ObjectId .get ();
202
204
Store store = converterFacade .convertStoreDTO (dto .getStore ());
203
205
store .set_id (store_id );
@@ -207,7 +209,6 @@ public ResponseEntity<?> addNewStore(@RequestBody final RegisterDTO dto) throws
207
209
user .setStore_id (store_id .toHexString ());
208
210
209
211
return new ResponseEntity <>(service .create (user ), HttpStatus .OK );
210
-
212
+
211
213
}
212
214
}
213
-
0 commit comments