feat!: JDK21 -> pattern matching, integration tests implementation & small fixes
This commit is contained in:
parent
2981691ffc
commit
720937bd6c
10 changed files with 184 additions and 49 deletions
|
@ -1,10 +1,13 @@
|
|||
package eu.ztsh.wymiana.web.controller;
|
||||
|
||||
import eu.ztsh.wymiana.exception.UserAlreadyExistsException;
|
||||
import eu.ztsh.wymiana.model.User;
|
||||
import eu.ztsh.wymiana.service.UserService;
|
||||
import eu.ztsh.wymiana.validation.ValidationFailedException;
|
||||
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
@ -17,7 +20,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
@RequiredArgsConstructor
|
||||
@Validated
|
||||
@RestController
|
||||
@RequestMapping("/api/user")
|
||||
@RequestMapping(path = "/api/user", produces = "application/json")
|
||||
public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
|
@ -28,8 +31,17 @@ public class UserController {
|
|||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<Void> create(@Valid @RequestBody UserCreateRequest request) {
|
||||
userService.create(request);
|
||||
public ResponseEntity<String> create(@Valid @RequestBody UserCreateRequest request) {
|
||||
try {
|
||||
userService.create(request);
|
||||
} catch (Exception e) {
|
||||
var status = switch (e) {
|
||||
case ValidationFailedException ignored -> HttpStatus.BAD_REQUEST;
|
||||
case UserAlreadyExistsException ignored -> HttpStatus.CONFLICT;
|
||||
default -> HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
};
|
||||
return ResponseEntity.status(status).body(e.getMessage());
|
||||
}
|
||||
return ResponseEntity.status(204).build();
|
||||
}
|
||||
|
||||
|
|
|
@ -4,12 +4,13 @@ import eu.ztsh.wymiana.validation.Adult;
|
|||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Builder;
|
||||
import org.hibernate.validator.constraints.pl.PESEL;
|
||||
|
||||
@Builder
|
||||
public record UserCreateRequest(
|
||||
@NotNull String name,
|
||||
@NotNull String surname,
|
||||
@Adult String pesel,
|
||||
@PESEL @Adult String pesel,
|
||||
@Min(0) double pln) {
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue