1 | package finki.diplomska.tripplanner.web.rest;
|
---|
2 |
|
---|
3 | import finki.diplomska.tripplanner.models.User;
|
---|
4 | import finki.diplomska.tripplanner.models.dto.UserDto;
|
---|
5 | import finki.diplomska.tripplanner.service.UserService;
|
---|
6 | import finki.diplomska.tripplanner.service.impl.MapValidationErrorService;
|
---|
7 | import finki.diplomska.tripplanner.validator.UserValidator;
|
---|
8 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
9 | import org.springframework.http.HttpStatus;
|
---|
10 | import org.springframework.http.ResponseEntity;
|
---|
11 | import org.springframework.validation.BindingResult;
|
---|
12 | import org.springframework.web.bind.annotation.*;
|
---|
13 |
|
---|
14 | import javax.validation.Valid;
|
---|
15 |
|
---|
16 | @RestController
|
---|
17 | @CrossOrigin(origins = "http://localhost:4200")
|
---|
18 | @RequestMapping(value = "/api/users")
|
---|
19 | public class UserController {
|
---|
20 |
|
---|
21 | @Autowired
|
---|
22 | private MapValidationErrorService mapValidationErrorService;
|
---|
23 |
|
---|
24 | @Autowired
|
---|
25 | private UserService userService;
|
---|
26 |
|
---|
27 | @Autowired
|
---|
28 | private UserValidator userValidator;
|
---|
29 |
|
---|
30 | @PostMapping("/register")
|
---|
31 | public ResponseEntity<?> registerUser(@Valid @RequestBody User user, BindingResult result){
|
---|
32 | // Validate passwords match
|
---|
33 | userValidator.validate(user, result);
|
---|
34 | ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result);
|
---|
35 | if(errorMap != null)return errorMap;
|
---|
36 |
|
---|
37 | User newUser = userService.saveUser(user);
|
---|
38 |
|
---|
39 | return new ResponseEntity<User>(newUser, HttpStatus.CREATED);
|
---|
40 | }
|
---|
41 | }
|
---|