-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
874 additions
and
874 deletions.
There are no files selected for viewing
80 changes: 40 additions & 40 deletions
80
src/main/java/sg/com/smartinventory/z_KIV/controllers/AuthController.java.dev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,41 @@ | ||
package sg.com.smartinventory.controllers; | ||
|
||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import sg.com.smartinventory.entities.LoginRequest; | ||
import sg.com.smartinventory.entities.User; | ||
import sg.com.smartinventory.security.JwtTokenUtil; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
|
||
@RestController | ||
@RequestMapping("/auth/") | ||
public class AuthController { | ||
private final AuthenticationManager authenticationManager; | ||
|
||
private JwtTokenUtil jwtTokenUtil; | ||
|
||
public AuthController(AuthenticationManager authenticationManager, JwtTokenUtil jwtTokenUtil) { | ||
this.authenticationManager = authenticationManager; | ||
this.jwtTokenUtil = jwtTokenUtil; | ||
} | ||
|
||
@PostMapping("/login") | ||
public ResponseEntity<String> login(@RequestBody LoginRequest login) { | ||
Authentication auth = authenticationManager | ||
.authenticate(new UsernamePasswordAuthenticationToken(login.getUsername(), login.getPassword())); | ||
|
||
String email = auth.getName(); | ||
|
||
String token = jwtTokenUtil.createToken(new User(email)); | ||
|
||
return new ResponseEntity<>(token, HttpStatus.OK); | ||
} | ||
package sg.com.smartinventory.controllers; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import sg.com.smartinventory.entities.LoginRequest; | ||
import sg.com.smartinventory.entities.User; | ||
import sg.com.smartinventory.security.JwtTokenUtil; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
@RestController | ||
@RequestMapping("/auth/") | ||
public class AuthController { | ||
private final AuthenticationManager authenticationManager; | ||
private JwtTokenUtil jwtTokenUtil; | ||
public AuthController(AuthenticationManager authenticationManager, JwtTokenUtil jwtTokenUtil) { | ||
this.authenticationManager = authenticationManager; | ||
this.jwtTokenUtil = jwtTokenUtil; | ||
} | ||
@PostMapping("/login") | ||
public ResponseEntity<String> login(@RequestBody LoginRequest login) { | ||
Authentication auth = authenticationManager | ||
.authenticate(new UsernamePasswordAuthenticationToken(login.getUsername(), login.getPassword())); | ||
String email = auth.getName(); | ||
String token = jwtTokenUtil.createToken(new User(email)); | ||
return new ResponseEntity<>(token, HttpStatus.OK); | ||
} | ||
} |
122 changes: 61 additions & 61 deletions
122
src/main/java/sg/com/smartinventory/z_KIV/controllers/GlobalExceptionHandler.java.dev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
package sg.com.smartinventory.controllers; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.springframework.dao.EmptyResultDataAccessException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.ObjectError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
import sg.com.smartinventory.entities.ErrorResponse; | ||
import sg.com.smartinventory.exceptions.UserNotFoundException; | ||
import sg.com.smartinventory.exceptions.UserRoleNotFoundException; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); | ||
|
||
@ExceptionHandler({ UserNotFoundException.class, UserRoleNotFoundException.class }) | ||
public ResponseEntity<ErrorResponse> handleResourceNotFoundException(Exception ex) { | ||
return new ResponseEntity<>(new ErrorResponse(ex.getMessage(), LocalDateTime.now()), HttpStatus.NOT_FOUND); | ||
} | ||
|
||
@ExceptionHandler(EmptyResultDataAccessException.class) | ||
public ResponseEntity<ErrorResponse> handleEmptyResultDataAccessException(EmptyResultDataAccessException ex) { | ||
ErrorResponse errorResponse = new ErrorResponse("Resource does not exist.", LocalDateTime.now()); | ||
|
||
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND); | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<ErrorResponse> handleValidationExceptions(MethodArgumentNotValidException ex) { | ||
// Get a list of all validation errors from the exception object | ||
List<ObjectError> validationErrors = ex.getBindingResult().getAllErrors(); | ||
|
||
// Create a StringBuilder to store all error messages | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
for (ObjectError error : validationErrors) { | ||
sb.append(error.getDefaultMessage() + ". "); | ||
} | ||
|
||
ErrorResponse errorResponse = new ErrorResponse(sb.toString(), LocalDateTime.now()); | ||
|
||
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ErrorResponse> handleException(Exception ex) { | ||
logger.error("Unknown Exception occured!", ex); | ||
|
||
ErrorResponse errorResponse = new ErrorResponse("Something went wrong. ", LocalDateTime.now()); | ||
|
||
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
package sg.com.smartinventory.controllers; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.dao.EmptyResultDataAccessException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.ObjectError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import sg.com.smartinventory.entities.ErrorResponse; | ||
import sg.com.smartinventory.exceptions.UserNotFoundException; | ||
import sg.com.smartinventory.exceptions.UserRoleNotFoundException; | ||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); | ||
@ExceptionHandler({ UserNotFoundException.class, UserRoleNotFoundException.class }) | ||
public ResponseEntity<ErrorResponse> handleResourceNotFoundException(Exception ex) { | ||
return new ResponseEntity<>(new ErrorResponse(ex.getMessage(), LocalDateTime.now()), HttpStatus.NOT_FOUND); | ||
} | ||
@ExceptionHandler(EmptyResultDataAccessException.class) | ||
public ResponseEntity<ErrorResponse> handleEmptyResultDataAccessException(EmptyResultDataAccessException ex) { | ||
ErrorResponse errorResponse = new ErrorResponse("Resource does not exist.", LocalDateTime.now()); | ||
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND); | ||
} | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<ErrorResponse> handleValidationExceptions(MethodArgumentNotValidException ex) { | ||
// Get a list of all validation errors from the exception object | ||
List<ObjectError> validationErrors = ex.getBindingResult().getAllErrors(); | ||
// Create a StringBuilder to store all error messages | ||
StringBuilder sb = new StringBuilder(); | ||
for (ObjectError error : validationErrors) { | ||
sb.append(error.getDefaultMessage() + ". "); | ||
} | ||
ErrorResponse errorResponse = new ErrorResponse(sb.toString(), LocalDateTime.now()); | ||
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); | ||
} | ||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ErrorResponse> handleException(Exception ex) { | ||
logger.error("Unknown Exception occured!", ex); | ||
ErrorResponse errorResponse = new ErrorResponse("Something went wrong. ", LocalDateTime.now()); | ||
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} |
118 changes: 59 additions & 59 deletions
118
src/main/java/sg/com/smartinventory/z_KIV/controllers/UserController.java.dev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,60 @@ | ||
package sg.com.smartinventory.controllers; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.validation.Valid; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import sg.com.smartinventory.entities.User; | ||
import sg.com.smartinventory.services.UserService; | ||
|
||
@RestController | ||
@RequestMapping("/users") | ||
public class UserController { | ||
private UserService userService; | ||
|
||
public UserController(UserService userService) { | ||
this.userService = userService; | ||
} | ||
|
||
@GetMapping("") | ||
public ResponseEntity<List<User>> getAllUsers() { | ||
return new ResponseEntity<>(userService.getAllUsers(), HttpStatus.OK); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<User> getOneUser(@PathVariable Long id) { | ||
return new ResponseEntity<>(userService.getOneUser(id), HttpStatus.OK); | ||
} | ||
|
||
@PostMapping("") | ||
public ResponseEntity<User> createUser(@Valid @RequestBody User user) { | ||
return new ResponseEntity<>(userService.createUser(user), HttpStatus.CREATED); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public ResponseEntity<User> updateUser(@PathVariable Long id, @Valid @RequestBody User user) { | ||
return new ResponseEntity<>(userService.updateUser(id, user), HttpStatus.OK); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<User> deleteUser(@PathVariable Long id) { | ||
userService.deleteUser(id); | ||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
|
||
@PutMapping("/{id}/userrole/{roleId}") | ||
public ResponseEntity<User> addUserRoleToUser(@PathVariable(name = "id") Long userId, @PathVariable Long roleId) { | ||
return new ResponseEntity<>(userService.addUserRoleToUser(userId, roleId), HttpStatus.OK); | ||
} | ||
package sg.com.smartinventory.controllers; | ||
import java.util.List; | ||
import jakarta.validation.Valid; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import sg.com.smartinventory.entities.User; | ||
import sg.com.smartinventory.services.UserService; | ||
@RestController | ||
@RequestMapping("/users") | ||
public class UserController { | ||
private UserService userService; | ||
public UserController(UserService userService) { | ||
this.userService = userService; | ||
} | ||
@GetMapping("") | ||
public ResponseEntity<List<User>> getAllUsers() { | ||
return new ResponseEntity<>(userService.getAllUsers(), HttpStatus.OK); | ||
} | ||
@GetMapping("/{id}") | ||
public ResponseEntity<User> getOneUser(@PathVariable Long id) { | ||
return new ResponseEntity<>(userService.getOneUser(id), HttpStatus.OK); | ||
} | ||
@PostMapping("") | ||
public ResponseEntity<User> createUser(@Valid @RequestBody User user) { | ||
return new ResponseEntity<>(userService.createUser(user), HttpStatus.CREATED); | ||
} | ||
@PutMapping("/{id}") | ||
public ResponseEntity<User> updateUser(@PathVariable Long id, @Valid @RequestBody User user) { | ||
return new ResponseEntity<>(userService.updateUser(id, user), HttpStatus.OK); | ||
} | ||
@DeleteMapping("/{id}") | ||
public ResponseEntity<User> deleteUser(@PathVariable Long id) { | ||
userService.deleteUser(id); | ||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
@PutMapping("/{id}/userrole/{roleId}") | ||
public ResponseEntity<User> addUserRoleToUser(@PathVariable(name = "id") Long userId, @PathVariable Long roleId) { | ||
return new ResponseEntity<>(userService.addUserRoleToUser(userId, roleId), HttpStatus.OK); | ||
} | ||
} |
Oops, something went wrong.