Skip to content

Commit 673206c

Browse files
author
John Wang
committed
changing the DTO converter
1 parent 9e4da8c commit 673206c

File tree

14 files changed

+395
-211
lines changed

14 files changed

+395
-211
lines changed

src/main/java/com/example/demo/config/SecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected void configure(final HttpSecurity http) throws Exception {
5858
http.authorizeRequests()
5959
.antMatchers("/api/auth").permitAll()
6060
.antMatchers("/api/register").permitAll()
61-
.antMatchers("/api/user").permitAll()
61+
.antMatchers("/api/user/**").permitAll()
6262
.anyRequest().authenticated()
6363
.and()
6464
.addFilterBefore(new AuthenticationTokenFilter(tokenAuthenticationService),

src/main/java/com/example/demo/controller/SecuredController.java

Lines changed: 136 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,35 @@
11
package com.example.demo.controller;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
53

6-
import javax.servlet.ServletException;
4+
import java.util.List;
75

86
import org.bson.types.ObjectId;
97
import org.springframework.beans.factory.annotation.Autowired;
108
import org.springframework.http.HttpStatus;
119
import org.springframework.http.ResponseEntity;
12-
import org.springframework.web.bind.annotation.PathVariable;
1310
import org.springframework.web.bind.annotation.RequestBody;
1411
import org.springframework.web.bind.annotation.RequestMapping;
1512
import org.springframework.web.bind.annotation.RequestMethod;
16-
import org.springframework.web.bind.annotation.RequestParam;
1713
import org.springframework.web.bind.annotation.RestController;
1814

1915
import com.example.demo.converter.ConverterFacade;
20-
import com.example.demo.model.Authority;
21-
import com.example.demo.model.Geolocation;
16+
2217
import com.example.demo.model.Store;
2318
import com.example.demo.model.User;
2419
import com.example.demo.data.provider.StoreManager;
2520
import com.example.demo.dto.RegisterDTO;
2621
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;
3022
import com.example.demo.service.UserService;
23+
import org.apache.logging.log4j.Logger;
24+
import org.apache.logging.log4j.LogManager;
3125

3226
import com.example.demo.dto.IndexDTO;
3327

3428
@RestController
3529
@RequestMapping("/api/user")
3630
public class SecuredController {
3731

38-
32+
Logger logger = LogManager.getLogger(AuthenticationController.class);
3933
private final UserService service;
4034
private final ConverterFacade converterFacade;
4135
@Autowired
@@ -56,118 +50,163 @@ public ResponseEntity<?> sayHello() {
5650
List<User> listAllUsers() {
5751
return service.findAll();
5852
}
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+
"email": "[email protected]",
67+
"enabled": true
68+
69+
}
70+
71+
*/
6072
@RequestMapping(value = "/update", method = RequestMethod.POST)
6173
public ResponseEntity<?> updateUser(@RequestBody final RegisterDTO dto){
6274

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);
8978

9079
}
9180

81+
/**
82+
*
83+
* @param dto
84+
* @return
85+
{
86+
"_id": "5ca97198bee20412a4f0ffe4"
87+
88+
}
89+
*/
9290
@RequestMapping(value = "/delete", method = RequestMethod.POST)
9391
public ResponseEntity<?> deleteUser(@RequestBody final IndexDTO dto){
9492
String index = service.delete(dto.get_id());
9593
return new ResponseEntity<>(index, HttpStatus.OK);
9694

9795
}
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)
100107
public ResponseEntity<?> getUser(@RequestBody final IndexDTO dto){
101108
User find = service.find(dto.get_id());
102109
return new ResponseEntity<>(find, HttpStatus.OK);
103110
}
104111

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+
"email": "[email protected]",
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+
"email": "[email protected]",
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+
105158
@RequestMapping(value = "/addUserToStore", method = RequestMethod.POST)
106159
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+
}
131164

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+
"email": "[email protected]",
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"
133191
}
192+
193+
}
134194
135-
@RequestMapping(value = "/addNewStore", method = RequestMethod.POST)
195+
196+
*
197+
*/
198+
@RequestMapping(value = "/addNewStoreAndNewUser", method = RequestMethod.POST)
136199
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+
140201
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);
144204
storeManager.addStore(store);
145-
146-
User user = new User();
205+
User user = converterFacade.convertRegisterDTO(dto);
147206
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);
171210

172211
}
173212
}

0 commit comments

Comments
 (0)