diff --git a/src/main/java/io/hexlet/blog/controller/api/UsersController.java b/src/main/java/io/hexlet/blog/controller/api/UsersController.java index 512a534..b6660ec 100644 --- a/src/main/java/io/hexlet/blog/controller/api/UsersController.java +++ b/src/main/java/io/hexlet/blog/controller/api/UsersController.java @@ -2,6 +2,7 @@ import java.util.List; +import io.hexlet.blog.dto.UserCreateDTO; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -44,7 +45,7 @@ ResponseEntity> index() { @PostMapping("/users") @ResponseStatus(HttpStatus.CREATED) - UserDTO create(@Valid @RequestBody UserDTO userData) { + UserDTO create(@Valid @RequestBody UserCreateDTO userData) { var user = userMapper.map(userData); repository.save(user); return userMapper.map(user); @@ -63,9 +64,9 @@ UserDTO update(@RequestBody UserUpdateDTO userData, @PathVariable Long id) { @GetMapping("/users/{id}") @ResponseStatus(HttpStatus.OK) - User show(@PathVariable Long id) { + UserDTO show(@PathVariable Long id) { var user = repository.findById(id) .orElseThrow(() -> new ResourceNotFoundException("Not Found")); - return user; + return userMapper.map(user); } } diff --git a/src/main/java/io/hexlet/blog/dto/UserCreateDTO.java b/src/main/java/io/hexlet/blog/dto/UserCreateDTO.java new file mode 100644 index 0000000..f07737b --- /dev/null +++ b/src/main/java/io/hexlet/blog/dto/UserCreateDTO.java @@ -0,0 +1,24 @@ +package io.hexlet.blog.dto; + +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +public class UserCreateDTO { + + @NotBlank + private String firstName; + + @NotBlank + private String lastName; + + @Email + private String email; + + @Size(min = 3, max = 100) + private String password; +} diff --git a/src/main/java/io/hexlet/blog/mapper/UserMapper.java b/src/main/java/io/hexlet/blog/mapper/UserMapper.java index ac4dc08..959f86e 100644 --- a/src/main/java/io/hexlet/blog/mapper/UserMapper.java +++ b/src/main/java/io/hexlet/blog/mapper/UserMapper.java @@ -1,5 +1,6 @@ package io.hexlet.blog.mapper; +import io.hexlet.blog.dto.UserCreateDTO; import org.mapstruct.BeforeMapping; import org.mapstruct.InheritInverseConfiguration; import org.mapstruct.Mapper; @@ -26,21 +27,19 @@ public abstract class UserMapper { @Autowired private BCryptPasswordEncoder encoder; - @Mapping(target = "id", ignore = true) - @Mapping(target = "email", source = "username") @Mapping(target = "passwordDigest", source = "password") - public abstract User map(UserDTO model); + public abstract User map(UserCreateDTO model); public abstract User map(UserUpdateDTO model); - @InheritInverseConfiguration + @Mapping(target = "username", source = "email") @Mapping(target = "password", ignore = true) public abstract UserDTO map(User model); public abstract void update(UserUpdateDTO update, @MappingTarget User destination); @BeforeMapping - public void encryptPassword(UserDTO data) { + public void encryptPassword(UserCreateDTO data) { var password = data.getPassword(); data.setPassword(encoder.encode(password)); }