diff --git a/src/main/java/com/example/usersapi/controller/UserController.java b/src/main/java/com/example/usersapi/controller/UserController.java new file mode 100644 index 0000000..6ad893c --- /dev/null +++ b/src/main/java/com/example/usersapi/controller/UserController.java @@ -0,0 +1,61 @@ +package com.example.usersapi.controller; + +import com.example.usersapi.dto.CreateUserRequestDto; +import com.example.usersapi.dto.UserPatchRequestDto; +import com.example.usersapi.dto.UserResponseDto; +import com.example.usersapi.dto.UserResponseDtoWrapper; +import com.example.usersapi.service.UserService; +import jakarta.validation.Valid; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +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.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping(value = "/api/users") +public class UserController { + private final UserService userService; + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public UserResponseDtoWrapper addUser( + @RequestBody @Valid CreateUserRequestDto requestDto) { + return userService.createUser(requestDto); + } + + @PatchMapping(value = "/{userId}") + public UserResponseDtoWrapper patchUser( + @PathVariable Long userId, @RequestBody @Valid UserPatchRequestDto requestDto) { + return userService.patchUser(userId, requestDto); + } + + @PutMapping(value = "/{userId}") + public UserResponseDtoWrapper updateUser( + @PathVariable Long userId, @RequestBody @Valid CreateUserRequestDto requestDto) { + return userService.updateUser(userId, requestDto); + } + + @DeleteMapping(value = "/{userId}") + @ResponseStatus(HttpStatus.NO_CONTENT) + public void deleteUserById(@PathVariable Long userId) { + userService.deleteUser(userId); + } + + @GetMapping(value = "/search") + public UserResponseDtoWrapper> searchByBirthDates( + @RequestParam String fromDate, @RequestParam String toDate + ) { + return userService.searchByBirthDates(fromDate, toDate); + } +} diff --git a/src/main/java/com/example/usersapi/dto/CreateUserRequestDto.java b/src/main/java/com/example/usersapi/dto/CreateUserRequestDto.java index 4ac621c..6c76720 100644 --- a/src/main/java/com/example/usersapi/dto/CreateUserRequestDto.java +++ b/src/main/java/com/example/usersapi/dto/CreateUserRequestDto.java @@ -2,9 +2,11 @@ import jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Pattern; public record CreateUserRequestDto( - @Email + @Email(regexp = "[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,3}", + flags = Pattern.Flag.CASE_INSENSITIVE) @NotBlank String email, @NotBlank diff --git a/src/main/java/com/example/usersapi/dto/UserPatchRequestDto.java b/src/main/java/com/example/usersapi/dto/UserPatchRequestDto.java index 8522d7d..3096272 100644 --- a/src/main/java/com/example/usersapi/dto/UserPatchRequestDto.java +++ b/src/main/java/com/example/usersapi/dto/UserPatchRequestDto.java @@ -1,9 +1,11 @@ package com.example.usersapi.dto; import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.Pattern; public record UserPatchRequestDto( - @Email + @Email(regexp = "[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,3}", + flags = Pattern.Flag.CASE_INSENSITIVE) String email, String firstName, String lastName,