Skip to content

Commit c915751

Browse files
authored
Merge pull request #12 from codeplusofc/implementando-camada-de-dto
Implementando a camada de DTO para o User
2 parents 0adb2dd + ce30d39 commit c915751

File tree

5 files changed

+126
-4
lines changed

5 files changed

+126
-4
lines changed

src/main/java/com/code/food/controller/UserController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.code.food.controller;
22

3+
import com.code.food.dto.InUser;
4+
import com.code.food.dto.OutUser;
35
import com.code.food.entity.UserEntity;
46
import com.code.food.service.UserService;
57
import org.springframework.beans.factory.annotation.Autowired;
@@ -26,8 +28,8 @@ public class UserController {
2628
// dos dados (geralmente em formato JSON ou XML) e os mapeia para o parâmetro do metodo
2729
// de acordo com o tipo especificado.
2830
@PostMapping
29-
public UserEntity createUser(@RequestBody UserEntity userEntity) {
30-
return userService.createUser(userEntity);
31+
public OutUser createUser(@RequestBody InUser inUser) {
32+
return userService.createUser(inUser);
3133
}
3234

3335
// A anotação @GetMapping serve para especificar que vamos buscar e retornar algo
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.code.food.dto;
2+
3+
public class InUser {
4+
5+
private String name;
6+
private String email;
7+
private String password;
8+
private String phone;
9+
private String referralSource;
10+
11+
public String getName() {
12+
return name;
13+
}
14+
15+
public void setName(String name) {
16+
this.name = name;
17+
}
18+
19+
public String getEmail() {
20+
return email;
21+
}
22+
23+
public void setEmail(String email) {
24+
this.email = email;
25+
}
26+
27+
public String getPassword() {
28+
return password;
29+
}
30+
31+
public void setPassword(String password) {
32+
this.password = password;
33+
}
34+
35+
public String getPhone() {
36+
return phone;
37+
}
38+
39+
public void setPhone(String phone) {
40+
this.phone = phone;
41+
}
42+
43+
public String getReferralSource() {
44+
return referralSource;
45+
}
46+
47+
public void setReferralSource(String referralSource) {
48+
this.referralSource = referralSource;
49+
}
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.code.food.dto;
2+
3+
public class OutUser {
4+
5+
private Long id;
6+
private String name;
7+
private String email;
8+
private String phone;
9+
private String referralSource;
10+
11+
public OutUser(Long id, String name, String email, String phone, String referralSource) {
12+
this.id = id;
13+
this.name = name;
14+
this.email = email;
15+
this.phone = phone;
16+
this.referralSource = referralSource;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public String getEmail() {
28+
return email;
29+
}
30+
31+
public void setEmail(String email) {
32+
this.email = email;
33+
}
34+
35+
public String getPhone() {
36+
return phone;
37+
}
38+
39+
public void setPhone(String phone) {
40+
this.phone = phone;
41+
}
42+
43+
public String getReferralSource() {
44+
return referralSource;
45+
}
46+
47+
public void setReferralSource(String referralSource) {
48+
this.referralSource = referralSource;
49+
}
50+
}

src/main/java/com/code/food/entity/UserEntity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public class UserEntity {
2727
private String phone;
2828
private String referralSource;
2929

30+
public UserEntity(String name, String email, String password, String phone, String referralSource) {
31+
this.name = name;
32+
this.email = email;
33+
this.password = password;
34+
this.phone = phone;
35+
this.referralSource = referralSource;
36+
}
37+
3038
public String getName() {
3139
return name;
3240
}

src/main/java/com/code/food/service/UserService.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.code.food.service;
22

3+
import com.code.food.dto.InUser;
4+
import com.code.food.dto.OutUser;
35
import com.code.food.entity.UserEntity;
46
import com.code.food.repository.UserRepository;
57
import com.code.food.validation.UserValidator;
@@ -28,9 +30,19 @@ public class UserService {
2830
// e o salva no banco de dados utilizando o repositório userRepository.
2931
// O metodo retorna o objeto userEntity após ser salvo, com os dados atualizados,
3032
// incluindo o ID gerado automaticamente, se aplicável.
31-
public UserEntity createUser(UserEntity userEntity) {
33+
public OutUser createUser(InUser inUser) {
34+
35+
var userEntity = new UserEntity(inUser.getName(),
36+
inUser.getEmail(),
37+
inUser.getPassword(),
38+
inUser.getPhone(),
39+
inUser.getReferralSource());
40+
3241
validateFields(userEntity);
33-
return userRepository.save(userEntity);
42+
43+
var userSaved = userRepository.save(userEntity);
44+
45+
return new OutUser(userSaved.getId(), userSaved.getName(), userSaved.getEmail(), userSaved.getPhone(), userSaved.getReferralSource());
3446
}
3547

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

0 commit comments

Comments
 (0)