|
| 1 | +package org.basic.spring.security.rest.controller; |
| 2 | + |
| 3 | +import java.security.Principal; |
| 4 | + |
| 5 | +import org.basic.spring.security.rest.dto.entity.user.AuthenticationDto; |
| 6 | +import org.basic.spring.security.rest.dto.entity.user.UserDto; |
| 7 | +import org.basic.spring.security.rest.dto.entity.user.UserList; |
| 8 | +import org.basic.spring.security.rest.dto.response.AbstractResponseDto; |
| 9 | +import org.basic.spring.security.rest.service.UserManagedService; |
| 10 | +import org.basic.spring.security.rest.util.ResponseUtil; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.http.HttpEntity; |
| 13 | +import org.springframework.http.HttpStatus; |
| 14 | +import org.springframework.http.ResponseEntity; |
| 15 | +import org.springframework.web.bind.annotation.PathVariable; |
| 16 | +import org.springframework.web.bind.annotation.RequestBody; |
| 17 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 18 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 19 | +import org.springframework.web.bind.annotation.RestController; |
| 20 | + |
| 21 | +@RestController |
| 22 | +public class UserController { |
| 23 | + |
| 24 | + @Autowired |
| 25 | + UserManagedService userService; //Service which will do all data retrieval/manipulation work |
| 26 | + |
| 27 | + //-------------------User Profile Detail-------------------------------------------------------- |
| 28 | + |
| 29 | + @RequestMapping(value = "/user/profile", method = RequestMethod.GET) |
| 30 | + public HttpEntity<AbstractResponseDto> profileDetail(Principal principal) { |
| 31 | + UserDto user = userService.findByUsername(principal.getName()); |
| 32 | + return ResponseUtil.success().body(user).send(HttpStatus.OK); |
| 33 | + } |
| 34 | + |
| 35 | + //-------------------Count user-------------------------------------------------------- |
| 36 | + |
| 37 | + @RequestMapping(value = "/user/count", method = RequestMethod.GET) |
| 38 | + public HttpEntity<AbstractResponseDto> userCout() { |
| 39 | + |
| 40 | + return ResponseUtil.success().body(userService.count()).send(HttpStatus.OK); |
| 41 | + } |
| 42 | + |
| 43 | + //-------------------Retrieve All Users-------------------------------------------------------- |
| 44 | + |
| 45 | + @RequestMapping(value = "/user/", method = RequestMethod.GET) |
| 46 | + public HttpEntity<AbstractResponseDto> listAllUsers() { |
| 47 | + UserList users = userService.findAllUsers(); |
| 48 | + if(users.getUserList().isEmpty()){ |
| 49 | + return ResponseUtil.error().message("No data found").send(HttpStatus.NOT_FOUND); |
| 50 | + } |
| 51 | + return ResponseUtil.success().body(users).message("User list fetched successfully !!!").send(HttpStatus.OK); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + //-------------------Retrieve Single User-------------------------------------------------------- |
| 56 | + |
| 57 | + @RequestMapping(value = "/user/{id}", method = RequestMethod.GET) |
| 58 | + public ResponseEntity<AbstractResponseDto> getUser(@PathVariable("id") long id) { |
| 59 | + UserDto user = userService.findById(id); |
| 60 | + if (user == null) { |
| 61 | + return ResponseUtil.error().message("No data found").send(HttpStatus.NOT_FOUND); |
| 62 | + } |
| 63 | + return ResponseUtil.success().body(user).message("User fetched successfully !!!").send(HttpStatus.OK); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + //-------------------Create a User-------------------------------------------------------- |
| 69 | + |
| 70 | + @RequestMapping(value = "/user/", method = RequestMethod.POST) |
| 71 | + public ResponseEntity<AbstractResponseDto> createUser(@RequestBody AuthenticationDto user) { |
| 72 | + |
| 73 | + userService.saveUser(user); |
| 74 | + |
| 75 | + return ResponseUtil.success().body(user.detail()).message("User created successfully !!!").send(HttpStatus.CREATED); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + //------------------- Update a User -------------------------------------------------------- |
| 80 | + |
| 81 | + @RequestMapping(value = "/user/{id}", method = RequestMethod.PUT) |
| 82 | + public ResponseEntity<AbstractResponseDto> updateUser(@PathVariable("id") long id, @RequestBody UserDto user) { |
| 83 | + |
| 84 | + UserDto currentUser = userService.findById(id); |
| 85 | + |
| 86 | + if (currentUser==null) { |
| 87 | + return ResponseUtil.error().message("User with id " + id + " not found").send(HttpStatus.NOT_FOUND); |
| 88 | + } |
| 89 | + |
| 90 | + userService.updateUser(currentUser, user); |
| 91 | + return ResponseUtil.success().body(currentUser).message("User updated successfully !!!").send(HttpStatus.OK); |
| 92 | + } |
| 93 | + |
| 94 | + //------------------- Delete a User -------------------------------------------------------- |
| 95 | + |
| 96 | + @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE) |
| 97 | + public ResponseEntity<AbstractResponseDto> deleteUser(@PathVariable("id") long id) { |
| 98 | + |
| 99 | + UserDto user = userService.findById(id); |
| 100 | + if (user == null) { |
| 101 | + System.out.println("Unable to delete. User with id " + id + " not found"); |
| 102 | + return ResponseUtil.error().message("Unable to delete. User with id " + id + " not found").send(HttpStatus.NOT_FOUND); |
| 103 | + } |
| 104 | + |
| 105 | + userService.deleteUserById(id); |
| 106 | + return ResponseUtil.success().body(user).message("User deleted successfully !!!").send(HttpStatus.OK); |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + //------------------- Delete All Users -------------------------------------------------------- |
| 111 | + |
| 112 | + @RequestMapping(value = "/user/", method = RequestMethod.DELETE) |
| 113 | + public ResponseEntity<AbstractResponseDto> deleteAllUsers() { |
| 114 | + |
| 115 | + userService.deleteAllUsers(); |
| 116 | + return ResponseUtil.success().body(new UserList()).message("All User deleted successfully !!!").send(HttpStatus.OK); |
| 117 | + } |
| 118 | +} |
0 commit comments