Skip to content

Commit ab44d04

Browse files
committed
프로필 업데이트 처리
1 parent 14413a1 commit ab44d04

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

profile/src/main/java/com/example/research/profile/v1/profile/ProfileHandler.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.example.research.profile.entity.cache.Profile;
44
import com.example.research.profile.entity.cache.ProfileRepository;
5+
import com.example.research.profile.entity.event.ProfileChangedEvent;
56
import com.example.research.profile.entity.event.ProfileSavedEvent;
67
import com.example.research.profile.entity.storage.ProfileStorageRepository;
78

@@ -10,9 +11,11 @@
1011
import org.springframework.http.MediaType;
1112
import org.springframework.lang.NonNull;
1213
import org.springframework.stereotype.Component;
14+
import org.springframework.util.StringUtils;
1315
import org.springframework.web.reactive.function.server.ServerRequest;
1416
import org.springframework.web.reactive.function.server.ServerResponse;
1517

18+
import java.time.LocalDateTime;
1619
import java.util.function.Function;
1720

1821
import lombok.extern.slf4j.Slf4j;
@@ -33,25 +36,19 @@ public class ProfileHandler {
3336
@NonNull
3437
public Mono<ServerResponse> fetchProfiles(ServerRequest request) {
3538
Flux<Profile> profiles = Flux.fromIterable(profileStorageRepository.findAll())
36-
.flatMap(profile -> {
37-
log.info("call switchIfEmpty");
38-
return profileRepository.save(Profile.from(profile));
39-
});
39+
.flatMap(profile -> profileRepository.save(Profile.from(profile)));
4040
return ok().contentType(MediaType.APPLICATION_JSON).body(profiles, Profile.class);
4141
}
4242

4343
@NonNull
4444
public Mono<ServerResponse> fetch(ServerRequest request) {
4545
String id = request.pathVariable("id");
4646
Mono<Profile> profile = profileRepository.findById(id)
47-
.onErrorResume(throwable -> {
48-
log.error("throwable : ", throwable);
49-
return Mono.just(
50-
profileStorageRepository.findById(id)
51-
.orElseThrow(() -> new IllegalStateException("profile not found"))
52-
)
53-
.flatMap(storedProfile -> profileRepository.save(Profile.from(storedProfile)));
54-
});
47+
.onErrorResume(throwable ->
48+
Mono.just(profileStorageRepository.findById(id)
49+
.orElseThrow(() -> new IllegalStateException("profile not found")))
50+
.flatMap(storedProfile -> profileRepository.save(Profile.from(storedProfile)))
51+
);
5552
return ok().contentType(MediaType.APPLICATION_JSON).body(profile, Profile.class);
5653
}
5754

@@ -62,6 +59,7 @@ public Mono<ServerResponse> save(Mono<ProfileSaveRequest> request) {
6259

6360
Function<com.example.research.profile.entity.storage.Profile, Mono<com.example.research.profile.entity.storage.Profile>>
6461
saveProfile = profile -> {
62+
log.info("profile is {}", profile);
6563
com.example.research.profile.entity.storage.Profile storeProfile = profileStorageRepository.save(profile);
6664
applicationEventPublisher.publishEvent(ProfileSavedEvent.from(storeProfile));
6765
return Mono.just(storeProfile);
@@ -73,7 +71,29 @@ public Mono<ServerResponse> save(Mono<ProfileSaveRequest> request) {
7371
Mono<ProfileSaveResponse> save = request.flatMap(saveProfile.compose(mapToProfile))
7472
.map(mapToResponse); // FIXME throw 처리
7573

76-
return ServerResponse.ok()
77-
.body(save, ProfileSaveResponse.class);
74+
return ok().body(save, ProfileSaveResponse.class);
75+
}
76+
77+
@NonNull
78+
public Mono<ServerResponse> update(ServerRequest request) {
79+
Mono<ProfileSaveRequest> profileSaveRequestMono = request.bodyToMono(ProfileSaveRequest.class);
80+
com.example.research.profile.entity.storage.Profile existProfile = profileStorageRepository
81+
.findById(request.pathVariable("id")).orElseThrow(IllegalStateException::new);
82+
83+
return profileSaveRequestMono.flatMap(requestProfile -> {
84+
existProfile.setName(
85+
StringUtils.isEmpty(requestProfile.getName()) ?
86+
existProfile.getName() : requestProfile.getName()
87+
);
88+
existProfile.setAge(requestProfile.getAge() == null ? existProfile.getAge() : requestProfile.getAge());
89+
existProfile.setSex(requestProfile.getSex() == null ? existProfile.getSex() : requestProfile.getSex());
90+
existProfile.setUpdatedAt(LocalDateTime.now());
91+
profileStorageRepository.save(existProfile);
92+
93+
applicationEventPublisher.publishEvent(ProfileChangedEvent.from(existProfile));
94+
95+
return ok().contentType(MediaType.APPLICATION_JSON).body(fromObject(existProfile))
96+
.switchIfEmpty(ServerResponse.badRequest().build());
97+
});
7898
}
7999
}

profile/src/main/java/com/example/research/profile/v1/profile/ProfileRouter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ProfileRouter {
2020
handler::fetchProfiles)
2121
.andRoute(POST("/api/v1/profiles").and(accept(MediaType.APPLICATION_JSON)),
2222
request -> handler.save(request.bodyToMono(ProfileSaveRequest.class)))
23-
.andRoute(PUT("/api/v1/profiles").and(accept(MediaType.APPLICATION_JSON)),
23+
.andRoute(PUT("/api/v1/profiles/{id}").and(accept(MediaType.APPLICATION_JSON)),
2424
handler::update)
2525
.andRoute(GET("/api/v1/profiles/{id}").and(accept(MediaType.APPLICATION_JSON)),
2626
handler::fetch);

profile/src/test/java/com/example/research/profile/entity/router/ProfileRouterTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313
import org.springframework.boot.test.context.SpringBootTest;
1414
import org.springframework.context.ApplicationContext;
1515
import org.springframework.http.MediaType;
16+
import org.springframework.test.context.ActiveProfiles;
1617
import org.springframework.test.context.junit4.SpringRunner;
1718
import org.springframework.test.web.reactive.server.WebTestClient;
1819
import org.springframework.web.reactive.function.BodyInserters;
1920

21+
import java.util.UUID;
22+
2023
import static org.hibernate.validator.internal.util.Contracts.assertNotNull;
2124

2225
@RunWith(SpringRunner.class)
2326
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
2427
@SpringBootTest(classes = TestApplicationContext.class)
28+
@ActiveProfiles("test")
2529
public class ProfileRouterTests {
2630
@Autowired ApplicationContext applicationContext;
2731

@@ -81,12 +85,14 @@ public void setup() {
8185
.accept(MediaType.APPLICATION_JSON)
8286
.body(BodyInserters.fromObject(profileSaveRequest))
8387
.exchange()
84-
.expectStatus().isOk();
88+
.expectStatus().isOk()
89+
.expectBody()
90+
.jsonPath("$.name").isEqualTo(profileSaveRequest.getName());
8591
}
8692

8793
private ProfileSaveRequest generate() {
8894
return new ProfileSaveRequest(
89-
RandomData.randomString(),
95+
UUID.randomUUID().toString(),
9096
RandomData.name(), RandomData.randomInteger(1, 80),
9197
RandomData.randomBoolean() ? "man" : "woman"
9298
);

0 commit comments

Comments
 (0)