1
1
package com .example .demo .controller ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .List ;
5
3
6
- import javax . servlet . ServletException ;
4
+ import java . util . List ;
7
5
8
6
import org .bson .types .ObjectId ;
9
7
import org .springframework .beans .factory .annotation .Autowired ;
10
8
import org .springframework .http .HttpStatus ;
11
9
import org .springframework .http .ResponseEntity ;
12
- import org .springframework .web .bind .annotation .PathVariable ;
13
10
import org .springframework .web .bind .annotation .RequestBody ;
14
11
import org .springframework .web .bind .annotation .RequestMapping ;
15
12
import org .springframework .web .bind .annotation .RequestMethod ;
16
- import org .springframework .web .bind .annotation .RequestParam ;
17
13
import org .springframework .web .bind .annotation .RestController ;
18
14
19
15
import com .example .demo .converter .ConverterFacade ;
20
- import com .example .demo .model .Authority ;
21
- import com .example .demo .model .Geolocation ;
16
+
22
17
import com .example .demo .model .Store ;
23
18
import com .example .demo .model .User ;
24
19
import com .example .demo .data .provider .StoreManager ;
25
20
import com .example .demo .dto .RegisterDTO ;
26
21
import com .example .demo .exceptions .StoreDuplicateItemException ;
27
- import com .example .demo .exceptions .UserDoesntExistException ;
28
- import com .example .demo .exceptions .UserExistedException ;
29
- import com .example .demo .exceptions .UserPasswordMismatchedException ;
30
22
import com .example .demo .service .UserService ;
23
+ import org .apache .logging .log4j .Logger ;
24
+ import org .apache .logging .log4j .LogManager ;
31
25
32
26
import com .example .demo .dto .IndexDTO ;
33
27
34
28
@ RestController
35
29
@ RequestMapping ("/api/user" )
36
30
public class SecuredController {
37
31
38
-
32
+ Logger logger = LogManager . getLogger ( AuthenticationController . class );
39
33
private final UserService service ;
40
34
private final ConverterFacade converterFacade ;
41
35
@ Autowired
@@ -56,118 +50,163 @@ public ResponseEntity<?> sayHello() {
56
50
List <User > listAllUsers () {
57
51
return service .findAll ();
58
52
}
59
-
53
+
54
+ /**
55
+ *
56
+ * @param dto
57
+ * @return
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
+
71
+ */
60
72
@ RequestMapping (value = "/update" , method = RequestMethod .POST )
61
73
public ResponseEntity <?> updateUser (@ RequestBody final RegisterDTO dto ){
62
74
63
- User user = new User ();
64
- ObjectId key = new ObjectId (dto .get_id ());
65
- user .set_id (key );
66
- user .setUsername (dto .getUsername ());
67
- user .setPassword (dto .getPassword ());
68
- user .setStore_id (dto .getStore_id ());
69
- user .setContactNumber (dto .getContactNumber ());
70
- user .setAccountNonExpired (false );
71
- user .setCredentialsNonExpired (false );
72
- user .setEnabled (true );
73
- user .setRole (dto .getRole ());
74
-
75
- //this is just stupid....
76
- List <Authority > authorities = new ArrayList <>();
77
- if (user .getRole ().equals ("ROLE_USER" ))
78
- authorities .add (Authority .ROLE_USER );
79
- else if (user .getRole ().equals ("ROLE_ADMIN" ))
80
- authorities .add (Authority .ROLE_ADMIN );
81
- else if (user .getRole ().equals ("ANONYMOUS" ))
82
- authorities .add (Authority .ANONYMOUS );
83
-
84
- user .setAuthorities (authorities );
85
- user .setEmail (dto .getEmail ());
86
- User find = service .update (key , user );
87
-
88
- return new ResponseEntity <>(find , HttpStatus .OK );
75
+ User user = converterFacade .convertRegisterDTO (dto );
76
+ ObjectId key = new ObjectId (user .get_id ());
77
+ return new ResponseEntity <>(service .update (key , user ), HttpStatus .OK );
89
78
90
79
}
91
80
81
+ /**
82
+ *
83
+ * @param dto
84
+ * @return
85
+ {
86
+ "_id": "5ca97198bee20412a4f0ffe4"
87
+
88
+ }
89
+ */
92
90
@ RequestMapping (value = "/delete" , method = RequestMethod .POST )
93
91
public ResponseEntity <?> deleteUser (@ RequestBody final IndexDTO dto ){
94
92
String index = service .delete (dto .get_id ());
95
93
return new ResponseEntity <>(index , HttpStatus .OK );
96
94
97
95
}
98
-
99
- @ RequestMapping (value = "/get" , method = RequestMethod .POST )
96
+ /**
97
+ *
98
+ * @param dto
99
+ * @return
100
+
101
+ {
102
+ "_id": "5ca6a49e920ede02679e43a7"
103
+ }
104
+
105
+ */
106
+ @ RequestMapping (value = "/get" , method = RequestMethod .GET )
100
107
public ResponseEntity <?> getUser (@ RequestBody final IndexDTO dto ){
101
108
User find = service .find (dto .get_id ());
102
109
return new ResponseEntity <>(find , HttpStatus .OK );
103
110
}
104
111
112
+ /**
113
+ *
114
+ * @param dto
115
+ * @return
116
+ *
117
+ * it doesn't update the store information
118
+ * it requires to have store_id
119
+ *
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
+ }
156
+ */
157
+
105
158
@ RequestMapping (value = "/addUserToStore" , method = RequestMethod .POST )
106
159
public ResponseEntity <?> addUserToAStore (@ RequestBody final RegisterDTO dto ){
107
- User user = new User ();
108
- ObjectId key = new ObjectId (dto .get_id ());
109
- user .set_id (key );
110
- user .setUsername (dto .getUsername ());
111
- user .setPassword (dto .getPassword ());
112
- user .setStore_id (dto .getStore_id ());
113
- user .setContactNumber (dto .getContactNumber ());
114
- user .setAccountNonExpired (false );
115
- user .setCredentialsNonExpired (false );
116
- user .setEnabled (true );
117
- user .setRole (dto .getRole ());
118
-
119
- //this is just stupid....
120
- List <Authority > authorities = new ArrayList <>();
121
- if (user .getRole ().equals ("ROLE_USER" ))
122
- authorities .add (Authority .ROLE_USER );
123
- else if (user .getRole ().equals ("ROLE_ADMIN" ))
124
- authorities .add (Authority .ROLE_ADMIN );
125
- else if (user .getRole ().equals ("ANONYMOUS" ))
126
- authorities .add (Authority .ANONYMOUS );
127
-
128
- user .setAuthorities (authorities );
129
- user .setEmail (dto .getEmail ());
130
- User find = service .create (user );
160
+
161
+ User user = converterFacade .convertRegisterDTO (dto );
162
+ return new ResponseEntity <>(service .create (user ), HttpStatus .OK );
163
+ }
131
164
132
- return new ResponseEntity <>(find , HttpStatus .OK );
165
+ /**
166
+ *
167
+ * @param dto
168
+ * @return
169
+ * @throws StoreDuplicateItemException
170
+ *
171
+ *
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"
133
191
}
192
+
193
+ }
134
194
135
- @ RequestMapping (value = "/addNewStore" , method = RequestMethod .POST )
195
+
196
+ *
197
+ */
198
+ @ RequestMapping (value = "/addNewStoreAndNewUser" , method = RequestMethod .POST )
136
199
public ResponseEntity <?> addNewStore (@ RequestBody final RegisterDTO dto ) throws StoreDuplicateItemException {
137
- Geolocation geolocation = new Geolocation ();
138
- geolocation .setLatitude (dto .getLatitude ());
139
- geolocation .setLongitude (dto .getLongitude ());
200
+
140
201
ObjectId store_id = ObjectId .get ();
141
- Store store = new Store (store_id , dto .getName (), dto .getPictureFileName (), geolocation , dto .getAddress (),
142
- dto .getZipcode (), dto .getCity (), dto .getState ());
143
-
202
+ Store store = converterFacade .convertStoreDTO (dto .getStore ());
203
+ store .set_id (store_id );
144
204
storeManager .addStore (store );
145
-
146
- User user = new User ();
205
+ User user = converterFacade .convertRegisterDTO (dto );
147
206
user .set_id (ObjectId .get ());
148
- user .setUsername (dto .getUsername ());
149
- user .setPassword (dto .getPassword ());
150
- user .setStore_id (store .get_id ());
151
- user .setContactNumber (dto .getContactNumber ());
152
- user .setAccountNonExpired (false );
153
- user .setCredentialsNonExpired (false );
154
- user .setEnabled (true );
155
- user .setRole (dto .getRole ());
156
-
157
- //this is just stupid....
158
- List <Authority > authorities = new ArrayList <>();
159
- if (user .getRole ().equals ("ROLE_USER" ))
160
- authorities .add (Authority .ROLE_USER );
161
- else if (user .getRole ().equals ("ROLE_ADMIN" ))
162
- authorities .add (Authority .ROLE_ADMIN );
163
- else if (user .getRole ().equals ("ANONYMOUS" ))
164
- authorities .add (Authority .ANONYMOUS );
165
-
166
- user .setAuthorities (authorities );
167
- user .setEmail (dto .getEmail ());
168
- User find = service .create (user );
169
-
170
- return new ResponseEntity <>(find , HttpStatus .OK );
207
+ user .setStore_id (store_id .toHexString ());
208
+
209
+ return new ResponseEntity <>(service .create (user ), HttpStatus .OK );
171
210
172
211
}
173
212
}
0 commit comments