Skip to content

Commit

Permalink
Added a user controller and an email pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
PavloSliepchenko committed May 5, 2024
1 parent 1965e14 commit 2e105a9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
61 changes: 61 additions & 0 deletions src/main/java/com/example/usersapi/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -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<UserResponseDto> addUser(
@RequestBody @Valid CreateUserRequestDto requestDto) {
return userService.createUser(requestDto);
}

@PatchMapping(value = "/{userId}")
public UserResponseDtoWrapper<UserResponseDto> patchUser(
@PathVariable Long userId, @RequestBody @Valid UserPatchRequestDto requestDto) {
return userService.patchUser(userId, requestDto);
}

@PutMapping(value = "/{userId}")
public UserResponseDtoWrapper<UserResponseDto> 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<List<UserResponseDto>> searchByBirthDates(
@RequestParam String fromDate, @RequestParam String toDate
) {
return userService.searchByBirthDates(fromDate, toDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 2e105a9

Please sign in to comment.