Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
유저 동아리 추가 서비스 구현
Browse files Browse the repository at this point in the history
Mono.when을 사용해 독립적, 병렬적으로 실행하도록 구현
  • Loading branch information
ori0o0p committed Feb 25, 2024
1 parent 0c2dece commit 87318ee
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.daemawiki.domain.user.dto;

public record UpdateClubRequest(
String club
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.daemawiki.domain.user.service;

import com.example.daemawiki.domain.document.component.facade.DocumentFacade;
import com.example.daemawiki.domain.document.model.DefaultDocument;
import com.example.daemawiki.domain.document.repository.DocumentRepository;
import com.example.daemawiki.domain.user.dto.UpdateClubRequest;
import com.example.daemawiki.domain.user.model.User;
import com.example.daemawiki.domain.user.repository.UserRepository;
import com.example.daemawiki.domain.user.service.facade.UserFacade;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.Arrays;

@Service
public class UpdateClub {
private final UserFacade userFacade;
private final DocumentFacade documentFacade;
private final UserRepository userRepository;
private final DocumentRepository documentRepository;

public UpdateClub(UserFacade userFacade, DocumentFacade documentFacade, UserRepository userRepository, DocumentRepository documentRepository) {
this.userFacade = userFacade;
this.documentFacade = documentFacade;
this.userRepository = userRepository;
this.documentRepository = documentRepository;
}

public Mono<Void> execute(UpdateClubRequest request) {
Mono<User> userMono = userFacade.currentUser()
.doOnNext(user -> user.getDetail().setClub(request.club()))
.flatMap(userRepository::save);

Mono<DefaultDocument> documentMono = userMono
.flatMap(user -> documentFacade.findDocumentById(user.getDocumentId()))
.doOnNext(document -> document.getGroups().add(Arrays.asList("동아리", request.club())))
.flatMap(documentRepository::save);

return Mono.when(userMono, documentMono).then();
}

}

0 comments on commit 87318ee

Please sign in to comment.