Skip to content

Commit cff6881

Browse files
committed
refactor dto creations
1 parent 7bf03f9 commit cff6881

File tree

6 files changed

+21
-46
lines changed

6 files changed

+21
-46
lines changed

src/main/java/com/myapp/dto/PostDTO.java

-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,4 @@ public static PostDTO newInstance(Micropost post, Boolean isMyPost) {
3636
return PostDTO.newInstance(post, null, isMyPost);
3737
}
3838

39-
public static PostDTO newInstance(Micropost post) {
40-
return PostDTO.newInstance(post, null, null);
41-
}
42-
4339
}

src/main/java/com/myapp/dto/RelatedUserDTO.java

-13
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,6 @@ public class RelatedUserDTO {
1919
private final Boolean isMyself;
2020
private final Boolean isFollowedByMe;
2121

22-
public static RelatedUserDTO newInstance(User user, Relationship relationship, UserStats userStats, Boolean isMyself) {
23-
final String avatarHash = Utils.md5(user.getUsername());
24-
25-
return RelatedUserDTO.builder()
26-
.id(user.getId())
27-
.name(user.getName())
28-
.avatarHash(avatarHash)
29-
.isMyself(isMyself)
30-
.userStats(userStats)
31-
.relationshipId(relationship.getId())
32-
.build();
33-
}
34-
3522
public static RelatedUserDTOBuilder builder2(User user, Relationship relationship, UserStats userStats) {
3623
final String avatarHash = Utils.md5(user.getUsername());
3724

src/main/java/com/myapp/dto/UserDTO.java

+9-21
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,8 @@ public class UserDTO {
1919
private final Boolean isMyself;
2020
private final Boolean isFollowedByMe;
2121

22-
public String getEmail() {
23-
return Optional.ofNullable(isMyself)
24-
.filter(Boolean::booleanValue)
25-
.map(b -> this.email)
26-
.orElse(null);
27-
}
28-
29-
public static UserDTO newInstance(User user, UserStats userStats, Boolean isMyself) {
30-
final String avatarHash = Utils.md5(user.getUsername());
31-
32-
return UserDTO.builder()
33-
.id(user.getId())
34-
.email(user.getUsername())
35-
.name(user.getName())
36-
.avatarHash(avatarHash)
37-
.userStats(userStats)
38-
.isMyself(isMyself)
39-
.build();
40-
}
41-
4222
public static UserDTO newInstance(User user) {
43-
return UserDTO.newInstance(user, null, null);
23+
return UserDTO.builder2(user, null).build();
4424
}
4525

4626
public static UserDTOBuilder builder2(User user, UserStats userStats) {
@@ -54,4 +34,12 @@ public static UserDTOBuilder builder2(User user, UserStats userStats) {
5434
.userStats(userStats);
5535
}
5636

37+
@SuppressWarnings("unused")
38+
public String getEmail() {
39+
return Optional.ofNullable(isMyself)
40+
.filter(Boolean::booleanValue)
41+
.map(b -> this.email)
42+
.orElse(null);
43+
}
44+
5745
}

src/test/groovy/com/myapp/controller/UserControllerTest.groovy

+7-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class UserControllerTest extends BaseControllerTest {
126126
userService.findOne(1) >> {
127127
User user = new User(id: 1, username: "test1@test.com", password: "secret", name: "test")
128128
UserStats userStats = new UserStats(3, 2, 1)
129-
return Optional.of(UserDTO.newInstance(user, userStats, null))
129+
return Optional.of(UserDTO.builder2(user, userStats).build())
130130
}
131131

132132
when:
@@ -166,7 +166,11 @@ class UserControllerTest extends BaseControllerTest {
166166
userService.findMe() >> {
167167
User user = new User(id: 1, username: "test1@test.com", password: "secret", name: "test")
168168
UserStats userStats = new UserStats(3, 2, 1)
169-
return Optional.of(UserDTO.newInstance(user, userStats, true))
169+
Optional.of(UserDTO.builder2(user, userStats)
170+
.isMyself(true)
171+
.isFollowedByMe(false)
172+
.build()
173+
)
170174
}
171175

172176
when:
@@ -179,7 +183,7 @@ class UserControllerTest extends BaseControllerTest {
179183
andExpect(jsonPath('$.email', is("test1@test.com")))
180184
andExpect(jsonPath('$.avatarHash', is("94fba03762323f286d7c3ca9e001c541")))
181185
andExpect(jsonPath('$.isMyself', is(true)))
182-
andExpect(jsonPath('$.isFollowedByMe', nullValue()))
186+
andExpect(jsonPath('$.isFollowedByMe', is(false)))
183187
andExpect(jsonPath('$.userStats').exists())
184188
andExpect(jsonPath('$.userStats.micropostCnt', is(3)))
185189
andExpect(jsonPath('$.userStats.followingCnt', is(2)))

src/test/groovy/com/myapp/controller/UserMicropostControllerTest.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class UserMicropostControllerTest extends BaseControllerTest {
3535
User user = new User(id: 1, username: "akira@test.com", password: "secret", name: "akira")
3636
micropostService.findByUser(1, new PageParams()) >> (0..1).collect {
3737
Micropost post = new Micropost(id: it, content: "content${it}", user: user)
38-
return PostDTO.newInstance(post)
38+
return PostDTO.newInstance(post, null)
3939
}
4040

4141

src/test/groovy/com/myapp/controller/UserRelationshipControllerTest.groovy

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class UserRelationshipControllerTest extends BaseControllerTest {
3939
Relationship r1 = new Relationship(id: 1, follower: follower, followed: followed1)
4040
Relationship r2 = new Relationship(id: 2, follower: follower, followed: followed2)
4141
relationshipService.findFollowings(follower.id, new PageParams()) >> [
42-
RelatedUserDTO.newInstance(followed1, r1, null, null),
43-
RelatedUserDTO.newInstance(followed2, r2, null, null),
42+
RelatedUserDTO.builder2(followed1, r1, null).build(),
43+
RelatedUserDTO.builder2(followed2, r2, null).build(),
4444
]
4545

4646
when:
@@ -66,8 +66,8 @@ class UserRelationshipControllerTest extends BaseControllerTest {
6666
Relationship r1 = new Relationship(id: 1, follower: follower1, followed: followed)
6767
Relationship r2 = new Relationship(id: 2, follower: follower2, followed: followed)
6868
relationshipService.findFollowers(followed.id, new PageParams()) >> [
69-
RelatedUserDTO.newInstance(follower1, r1, null, null),
70-
RelatedUserDTO.newInstance(follower2, r2, null, null),
69+
RelatedUserDTO.builder2(follower1, r1, null).build(),
70+
RelatedUserDTO.builder2(follower2, r2, null).build(),
7171
]
7272

7373
when:

0 commit comments

Comments
 (0)