Skip to content

Commit

Permalink
add create dto
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksim Litvinov committed Nov 29, 2023
1 parent f05323e commit 836883d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,7 +45,7 @@ ResponseEntity<List<UserDTO>> 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);
Expand All @@ -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);
}
}
24 changes: 24 additions & 0 deletions src/main/java/io/hexlet/blog/dto/UserCreateDTO.java
Original file line number Diff line number Diff line change
@@ -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;
}
9 changes: 4 additions & 5 deletions src/main/java/io/hexlet/blog/mapper/UserMapper.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}
Expand Down

0 comments on commit 836883d

Please sign in to comment.