Skip to content

Implementando a camada de DTO para o User #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/com/code/food/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.code.food.controller;

import com.code.food.dto.InUser;
import com.code.food.dto.OutUser;
import com.code.food.entity.UserEntity;
import com.code.food.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -26,8 +28,8 @@ public class UserController {
// dos dados (geralmente em formato JSON ou XML) e os mapeia para o parâmetro do metodo
// de acordo com o tipo especificado.
@PostMapping
public UserEntity createUser(@RequestBody UserEntity userEntity) {
return userService.createUser(userEntity);
public OutUser createUser(@RequestBody InUser inUser) {
return userService.createUser(inUser);
}

// A anotação @GetMapping serve para especificar que vamos buscar e retornar algo
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/code/food/dto/InUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.code.food.dto;

public class InUser {

private String name;
private String email;
private String password;
private String phone;
private String referralSource;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getReferralSource() {
return referralSource;
}

public void setReferralSource(String referralSource) {
this.referralSource = referralSource;
}
}
50 changes: 50 additions & 0 deletions src/main/java/com/code/food/dto/OutUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.code.food.dto;

public class OutUser {

private Long id;
private String name;
private String email;
private String phone;
private String referralSource;

public OutUser(Long id, String name, String email, String phone, String referralSource) {
this.id = id;
this.name = name;
this.email = email;
this.phone = phone;
this.referralSource = referralSource;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getReferralSource() {
return referralSource;
}

public void setReferralSource(String referralSource) {
this.referralSource = referralSource;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/code/food/entity/UserEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public class UserEntity {
private String phone;
private String referralSource;

public UserEntity(String name, String email, String password, String phone, String referralSource) {
this.name = name;
this.email = email;
this.password = password;
this.phone = phone;
this.referralSource = referralSource;
}

public String getName() {
return name;
}
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/code/food/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.code.food.service;

import com.code.food.dto.InUser;
import com.code.food.dto.OutUser;
import com.code.food.entity.UserEntity;
import com.code.food.repository.UserRepository;
import com.code.food.validation.UserValidator;
Expand Down Expand Up @@ -28,9 +30,19 @@ public class UserService {
// e o salva no banco de dados utilizando o repositório userRepository.
// O metodo retorna o objeto userEntity após ser salvo, com os dados atualizados,
// incluindo o ID gerado automaticamente, se aplicável.
public UserEntity createUser(UserEntity userEntity) {
public OutUser createUser(InUser inUser) {

var userEntity = new UserEntity(inUser.getName(),
inUser.getEmail(),
inUser.getPassword(),
inUser.getPhone(),
inUser.getReferralSource());

validateFields(userEntity);
return userRepository.save(userEntity);

var userSaved = userRepository.save(userEntity);

return new OutUser(userSaved.getId(), userSaved.getName(), userSaved.getEmail(), userSaved.getPhone(), userSaved.getReferralSource());
}

// O metodo findAllUsers() irá retornar uma lista(List) contendo todos os usuários que foram cadastrados
Expand Down
Loading