|
| 1 | +package com.rc.uam.controller.rest; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.http.HttpStatus; |
| 5 | +import org.springframework.http.MediaType; |
| 6 | +import org.springframework.http.ResponseEntity; |
| 7 | +import org.springframework.security.access.prepost.PreAuthorize; |
| 8 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 9 | +import org.springframework.web.bind.annotation.GetMapping; |
| 10 | +import org.springframework.web.bind.annotation.PathVariable; |
| 11 | +import org.springframework.web.bind.annotation.PostMapping; |
| 12 | +import org.springframework.web.bind.annotation.PutMapping; |
| 13 | +import org.springframework.web.bind.annotation.RequestBody; |
| 14 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 15 | +import org.springframework.web.bind.annotation.RestController; |
| 16 | + |
| 17 | +import com.rc.uam.exception.UamException; |
| 18 | +import com.rc.uam.model.Book; |
| 19 | +import com.rc.uam.model.User; |
| 20 | +import com.rc.uam.service.UserService; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Rachit Bhasin |
| 24 | + * |
| 25 | + */ |
| 26 | +@RestController |
| 27 | +@RequestMapping( value = "/api", produces = MediaType.APPLICATION_JSON_VALUE ) |
| 28 | +public class UserController { |
| 29 | + @Autowired |
| 30 | + UserService userService; |
| 31 | + |
| 32 | + /*---Add new User---*/ |
| 33 | + @PostMapping("/user") |
| 34 | + @PreAuthorize("hasRole('ADMIN')") |
| 35 | + public ResponseEntity<String> save(@RequestBody User user) throws UamException { |
| 36 | + long id = userService.save(user); |
| 37 | + return ResponseEntity.ok().body("New User has been saved with ID:" + id); |
| 38 | + } |
| 39 | + |
| 40 | + /*---Get a User by id---*/ |
| 41 | + @GetMapping("/user/{id}") |
| 42 | + @PreAuthorize("hasAnyRole('ADMIN','USER')") |
| 43 | + public ResponseEntity<?> get(@PathVariable("id") long id) throws UamException { |
| 44 | + User user = userService.get(id); |
| 45 | + if(user == null) { |
| 46 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found"); |
| 47 | + } |
| 48 | + return ResponseEntity.ok().body(user); |
| 49 | + } |
| 50 | + |
| 51 | + /*---Update a user by id---*/ |
| 52 | + @PutMapping("/user/{id}") |
| 53 | + @PreAuthorize("hasRole('ADMIN')") |
| 54 | + public ResponseEntity<?> update(@PathVariable("id") long id, @RequestBody User user) throws UamException { |
| 55 | + userService.update(id, user); |
| 56 | + return ResponseEntity.ok().body("Book has been updated successfully."); |
| 57 | + } |
| 58 | + |
| 59 | + /*---Delete a user by id---*/ |
| 60 | + @DeleteMapping("/user/{id}") |
| 61 | + @PreAuthorize("hasRole('ADMIN')") |
| 62 | + public ResponseEntity<?> delete(@PathVariable("id") long id) throws UamException { |
| 63 | + userService.delete(id); |
| 64 | + return ResponseEntity.ok().body("Book has been deleted successfully."); |
| 65 | + } |
| 66 | +} |
0 commit comments