Skip to content

Commit 8914114

Browse files
committed
feat(converters): refactor UserConverter and update DAO classes for better encapsulation
1 parent 3edd2ca commit 8914114

9 files changed

Lines changed: 83 additions & 50 deletions

File tree

src/main/java/com/xpeho/spring_boot_java_random_user/data/converters/UserConverter.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ public UserEntity toDomain(User user) {
3838
}
3939
// API -> Domain
4040
public UserEntity fromApiModel(RandomUserResultDAO model) {
41-
String firstName = model.name != null ? model.name.first : null;
42-
String lastName = model.name != null ? model.name.last : null;
43-
String civility = model.name != null ? model.name.title : null;
44-
String picture = model.picture != null ? model.picture.medium : null;
41+
String firstName = model.getName() != null ? model.getName().getFirst() : null;
42+
String lastName = model.getName() != null ? model.getName().getLast() : null;
43+
String civility = model.getName() != null ? model.getName().getTitle() : null;
44+
String picture = model.getPicture() != null ? model.getPicture().getMedium() : null;
4545
return new UserEntity(
4646
null,
47-
model.gender,
47+
model.getGender(),
4848
firstName,
4949
lastName,
5050
civility,
51-
model.email,
52-
model.phone,
51+
model.getEmail(),
52+
model.getPhone(),
5353
picture,
54-
model.nat
54+
model.getNat()
5555
);
5656
}
5757

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package com.xpeho.spring_boot_java_random_user.data.models;
22

33
public class RandomUserPictureDAO {
4-
public String medium;
4+
private String medium;
5+
6+
public String getMedium() {
7+
return medium;
8+
}
9+
10+
public void setMedium(String medium) {
11+
this.medium = medium;
12+
}
513
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package com.xpeho.spring_boot_java_random_user.data.models.api;
22

33
public class RandomUserNameDAO {
4-
public String title;
5-
public String first;
6-
public String last;
4+
private String title;
5+
private String first;
6+
private String last;
7+
8+
public String getTitle() { return title; }
9+
public void setTitle(String title) { this.title = title; }
10+
public String getFirst() { return first; }
11+
public void setFirst(String first) { this.first = first; }
12+
public String getLast() { return last; }
13+
public void setLast(String last) { this.last = last; }
714
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package com.xpeho.spring_boot_java_random_user.data.models.api;
22

33
public class RandomUserPictureDAO {
4-
public String medium;
4+
private String medium;
5+
6+
public String getMedium() {
7+
return medium;
8+
}
9+
10+
public void setMedium(String medium) {
11+
this.medium = medium;
12+
}
513
}
Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
package com.xpeho.spring_boot_java_random_user.data.models.api;
22

33
public class RandomUserResultDAO {
4-
public String gender;
5-
public RandomUserNameDAO name;
6-
public String email;
7-
public String phone;
8-
public RandomUserPictureDAO picture;
9-
public String nat;
4+
private String gender;
5+
private RandomUserNameDAO name;
6+
private String email;
7+
private String phone;
8+
private RandomUserPictureDAO picture;
9+
private String nat;
10+
11+
public String getGender() { return gender; }
12+
public void setGender(String gender) { this.gender = gender; }
13+
public RandomUserNameDAO getName() { return name; }
14+
public void setName(RandomUserNameDAO name) { this.name = name; }
15+
public String getEmail() { return email; }
16+
public void setEmail(String email) { this.email = email; }
17+
public String getPhone() { return phone; }
18+
public void setPhone(String phone) { this.phone = phone; }
19+
public RandomUserPictureDAO getPicture() { return picture; }
20+
public void setPicture(RandomUserPictureDAO picture) { this.picture = picture; }
21+
public String getNat() { return nat; }
22+
public void setNat(String nat) { this.nat = nat; }
1023
}

src/main/java/com/xpeho/spring_boot_java_random_user/data/services/UserServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public List<UserEntity> saveAll(List<UserEntity> users) {
2727
List<User> daoUsers = users.stream().map(userConverter::toDao).toList();
2828
Iterable<User> saved = userRepository.saveAll(daoUsers);
2929
return StreamSupport.stream(saved.spliterator(), false)
30-
.map(userConverter::toDomain)
31-
.collect(Collectors.toList());
30+
.map(userConverter::toDomain)
31+
.toList();
3232
}
3333
}

src/main/java/com/xpeho/spring_boot_java_random_user/presentation/controllers/UserController.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.xpeho.spring_boot_java_random_user.presentation.controllers;
22

3-
import com.xpeho.spring_boot_java_random_user.presentation.dto.UserDTO;
43
import org.springframework.http.ResponseEntity;
4+
import java.util.List;
5+
import com.xpeho.spring_boot_java_random_user.presentation.dto.UserDTO;
56
import jakarta.validation.constraints.Max;
67
import jakarta.validation.constraints.Min;
78
import org.springframework.web.bind.annotation.GetMapping;
@@ -15,7 +16,6 @@
1516
import io.swagger.v3.oas.annotations.responses.ApiResponses;
1617
import io.swagger.v3.oas.annotations.tags.Tag;
1718

18-
import java.util.List;
1919

2020
@RequestMapping("/random-users")
2121
@Tag(name = "User", description = "Endpoints for random user generation")
@@ -29,12 +29,10 @@ public interface UserController {
2929
@Parameter(name = "count", description = "Number of users to generate (max 5000)", example = "500")
3030
}
3131
)
32-
@ApiResponses({
33-
@ApiResponse(responseCode = "200", description = "List of users successfully fetched and saved"),
34-
@ApiResponse(responseCode = "500", description = "Internal server error"),
35-
@ApiResponse(responseCode = "503", description = "External service unavailable")
36-
})
37-
ResponseEntity<?> getRandomUsers(
32+
@ApiResponse(responseCode = "200", description = "List of users successfully fetched and saved")
33+
@ApiResponse(responseCode = "500", description = "Internal server error")
34+
@ApiResponse(responseCode = "503", description = "External service unavailable")
35+
ResponseEntity<List<UserDTO>> getRandomUsers(
3836
@RequestParam(defaultValue = "500")
3937
@Min(1)
4038
@Max(5000)

src/main/java/com/xpeho/spring_boot_java_random_user/presentation/handlers/UserHandler.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.List;
1616
import java.util.Map;
1717

18+
import static java.util.Collections.emptyList;
19+
1820
@RestController
1921
public class UserHandler implements UserController {
2022

@@ -29,18 +31,15 @@ public UserHandler(FetchAndSaveRandomUsersUseCase fetchAndSaveRandomUsersUseCase
2931
}
3032

3133
@Override
32-
public ResponseEntity<?> getRandomUsers(int count) {
34+
public ResponseEntity<List<UserDTO>> getRandomUsers(int count) {
3335
try {
3436
List<UserEntity> users = fetchAndSaveRandomUsersUseCase.execute(count);
3537
List<UserDTO> dtos = users.stream().map(userConverter::toDto).toList();
3638
return ResponseEntity.ok(dtos);
3739
} catch (IOException e) {
3840
logger.error("Error fetching random users: {}", e.getMessage(), e);
3941
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
40-
.body(Map.of(
41-
"error", "External API Error",
42-
"message", "Impossible to fetch random users from the external API. Please try again later."
43-
));
42+
.body(emptyList());
4443
}
4544
}
4645
}

src/test/java/com/xpeho/spring_boot_java_random_user/data/converters/UserConverterTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ class UserConverterTest {
1616
@Test
1717
void fromApiModel_fullData() {
1818
RandomUserNameDAO name = new RandomUserNameDAO();
19-
name.first = "John";
20-
name.last = "Doe";
21-
name.title = "Mr";
19+
name.setFirst("John");
20+
name.setLast("Doe");
21+
name.setTitle("Mr");
2222
RandomUserPictureDAO picture = new RandomUserPictureDAO();
23-
picture.medium = "pic.jpg";
23+
picture.setMedium("pic.jpg");
2424
RandomUserResultDAO api = new RandomUserResultDAO();
25-
api.gender = "male";
26-
api.name = name;
27-
api.email = "john@doe.com";
28-
api.phone = "1234";
29-
api.picture = picture;
30-
api.nat = "FR";
25+
api.setGender("male");
26+
api.setName(name);
27+
api.setEmail("john@doe.com");
28+
api.setPhone("1234");
29+
api.setPicture(picture);
30+
api.setNat("FR");
3131
UserEntity entity = converter.fromApiModel(api);
3232
assertNull(entity.id());
3333
assertEquals("male", entity.gender());
@@ -43,12 +43,12 @@ void fromApiModel_fullData() {
4343
@Test
4444
void fromApiModel_nullNameAndPicture() {
4545
RandomUserResultDAO api = new RandomUserResultDAO();
46-
api.gender = "female";
47-
api.name = null;
48-
api.email = "jane@doe.com";
49-
api.phone = "5678";
50-
api.picture = null;
51-
api.nat = "US";
46+
api.setGender("female");
47+
api.setName(null);
48+
api.setEmail("jane@doe.com");
49+
api.setPhone("5678");
50+
api.setPicture(null);
51+
api.setNat("US");
5252
UserEntity entity = converter.fromApiModel(api);
5353
assertNull(entity.firstname());
5454
assertNull(entity.lastname());

0 commit comments

Comments
 (0)