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
reactor core operation인 doOnSuccess 를 사용하여 회원가입이 완료되면 유저 정보를 바탕으로 문서를 생성하는 로직을 구현하였습니다.
  • Loading branch information
ori0o0p committed Feb 17, 2024
1 parent 7a0a87d commit cf52c5a
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.daemawiki.domain.auth.service;

import com.example.daemawiki.domain.auth.dto.request.SignupRequest;
import com.example.daemawiki.domain.document.component.CreateDocumentByUser;
import com.example.daemawiki.domain.mail.repository.AuthMailRepository;
import com.example.daemawiki.domain.user.model.User;
import com.example.daemawiki.domain.user.model.UserDetail;
Expand All @@ -14,20 +15,27 @@
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;

/*
유저 회원가입 성공시
해당 유저의 문서 생성
*/

@Service
public class Signup {
private final UserRepository userRepository;
private final AuthMailRepository authMailRepository;
private final PasswordEncoder passwordEncoder;
private final Scheduler scheduler;
private final GetMajorType getMajorType;
private final CreateDocumentByUser createDocumentByUser;

public Signup(UserRepository userRepository,AuthMailRepository authMailRepository, PasswordEncoder passwordEncoder, Scheduler scheduler, GetMajorType getMajorType) {
public Signup(UserRepository userRepository,AuthMailRepository authMailRepository, PasswordEncoder passwordEncoder, Scheduler scheduler, GetMajorType getMajorType, CreateDocumentByUser createDocumentByUser) {
this.userRepository = userRepository;
this.authMailRepository = authMailRepository;
this.passwordEncoder = passwordEncoder;
this.scheduler = scheduler;
this.getMajorType = getMajorType;
this.createDocumentByUser = createDocumentByUser;
}

public Mono<Void> execute(SignupRequest request) {
Expand All @@ -51,7 +59,8 @@ public Mono<Void> execute(SignupRequest request) {
.major(getMajorType.execute(request.major()))
.build())
.build())
.flatMap(userRepository::save);
.flatMap(userRepository::save)
.doOnSuccess(createDocumentByUser::execute);
}))).then();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.example.daemawiki.domain.document.dto.request.SaveDocumentRequest;
import com.example.daemawiki.domain.document.dto.response.GetDocumentResponse;
import com.example.daemawiki.domain.document.dto.response.SimpleDocumentResponse;
import com.example.daemawiki.domain.document.service.CreateDocument;
import com.example.daemawiki.domain.document.service.DeleteDocument;
import com.example.daemawiki.domain.document.service.GetDocument;
import com.example.daemawiki.domain.document.service.UpdateDocument;
import com.example.daemawiki.domain.document.component.service.CreateDocument;
import com.example.daemawiki.domain.document.component.service.DeleteDocument;
import com.example.daemawiki.domain.document.component.service.GetDocument;
import com.example.daemawiki.domain.document.component.service.UpdateDocument;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.daemawiki.domain.document.component;

import com.example.daemawiki.domain.document.component.facade.CreateDocumentFacade;
import com.example.daemawiki.domain.document.dto.request.SaveDocumentRequest;
import com.example.daemawiki.domain.document.model.DefaultDocument;
import com.example.daemawiki.domain.document.repository.DocumentRepository;
import com.example.daemawiki.domain.revision.component.RevisionComponent;
import com.example.daemawiki.domain.revision.dto.request.SaveRevisionHistoryRequest;
import com.example.daemawiki.domain.revision.model.type.RevisionType;
import com.example.daemawiki.domain.user.model.User;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

@Component
public class CreateDocumentByUser {
private final DocumentRepository documentRepository;
private final CreateDocumentFacade createDocumentFacade;
private final RevisionComponent revisionComponent;

public CreateDocumentByUser(DocumentRepository documentRepository, CreateDocumentFacade createDocumentFacade, RevisionComponent revisionComponent) {
this.documentRepository = documentRepository;
this.createDocumentFacade = createDocumentFacade;
this.revisionComponent = revisionComponent;
}

public Mono<Void> execute(User user) {
return documentRepository.save(createDocument(user))
.onErrorResume(Mono::error)
.flatMap(document -> revisionComponent.saveHistory(SaveRevisionHistoryRequest.builder()
.type(RevisionType.CREATE)
.documentId(document.getId())
.title(document.getTitle())
.build()));
}

private DefaultDocument createDocument(User user) {
List<List<String>> groups = Arrays.asList(
Arrays.asList("학생", user.getDetail().gen() + "기", user.getName()),
Arrays.asList("전공", user.getDetail().major().toString())
);

return createDocumentFacade.execute(SaveDocumentRequest.builder()
.title(user.getName())
.type("student")
.content("회원가입을 통해 자동 생성된 문서입니다.")
.groups(groups)
.build(), user);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.daemawiki.domain.document.component.facade;

import com.example.daemawiki.domain.document.dto.request.SaveDocumentRequest;
import com.example.daemawiki.domain.document.model.DefaultDocument;
import com.example.daemawiki.domain.document.model.DocumentEditor;
import com.example.daemawiki.domain.document.model.type.service.GetDocumentType;
import com.example.daemawiki.domain.user.dto.UserDetailResponse;
import com.example.daemawiki.domain.user.model.User;
import com.example.daemawiki.global.datetime.model.EditDateTime;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

@Component
public class CreateDocumentFacade {
private final GetDocumentType getDocumentType;

public CreateDocumentFacade(GetDocumentType getDocumentType) {
this.getDocumentType = getDocumentType;
}

public DefaultDocument execute(SaveDocumentRequest request, User user) {
UserDetailResponse userDetail = UserDetailResponse.builder()
.id(user.getId())
.name(user.getName())
.profile(user.getProfile())
.build();

return DefaultDocument.builder()
.title(request.title())
.type(getDocumentType.execute(request.type()))
.dateTime(EditDateTime.builder()
.created(LocalDateTime.now())
.updated(LocalDateTime.now())
.build())
.documentEditor(DocumentEditor.builder()
.createdUser(userDetail)
.updatedUser(userDetail)
.build())
.content(request.content())
.groups(request.groups().stream()
.filter(Objects::nonNull)
.map(group -> String.join("/", group))
.toList())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.daemawiki.domain.document.service.facade;
package com.example.daemawiki.domain.document.component.facade;

import com.example.daemawiki.domain.document.model.DefaultDocument;
import com.example.daemawiki.domain.document.repository.DocumentRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.daemawiki.domain.document.component.service;

import com.example.daemawiki.domain.document.component.facade.CreateDocumentFacade;
import com.example.daemawiki.domain.document.dto.request.SaveDocumentRequest;
import com.example.daemawiki.domain.document.repository.DocumentRepository;
import com.example.daemawiki.domain.revision.component.RevisionComponent;
import com.example.daemawiki.domain.revision.dto.request.SaveRevisionHistoryRequest;
import com.example.daemawiki.domain.revision.model.type.RevisionType;
import com.example.daemawiki.domain.user.service.facade.UserFacade;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

@Service
public class CreateDocument {
private final DocumentRepository documentRepository;
private final UserFacade userFacade;
private final RevisionComponent revisionComponent;
private final CreateDocumentFacade createDocumentFacade;

public CreateDocument(DocumentRepository documentRepository, UserFacade userFacade, RevisionComponent revisionComponent, CreateDocumentFacade createDocumentFacade) {
this.documentRepository = documentRepository;
this.userFacade = userFacade;
this.revisionComponent = revisionComponent;
this.createDocumentFacade = createDocumentFacade;
}

public Mono<Void> execute(SaveDocumentRequest request) {
return userFacade.currentUser()
.map(user -> createDocumentFacade.execute(request, user))
.flatMap(documentRepository::save)
.flatMap(document -> revisionComponent.saveHistory(SaveRevisionHistoryRequest.builder()
.type(RevisionType.CREATE)
.documentId(document.getId())
.title(document.getTitle())
.build()));
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.daemawiki.domain.document.service;
package com.example.daemawiki.domain.document.component.service;

import com.example.daemawiki.domain.document.repository.DocumentRepository;
import com.example.daemawiki.domain.revision.dto.request.SaveRevisionHistoryRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.daemawiki.domain.document.service;
package com.example.daemawiki.domain.document.component.service;

import com.example.daemawiki.domain.document.dto.response.GetDocumentResponse;
import com.example.daemawiki.domain.document.dto.response.SimpleDocumentResponse;
import com.example.daemawiki.domain.document.service.facade.DocumentFacade;
import com.example.daemawiki.domain.document.component.facade.DocumentFacade;
import com.example.daemawiki.domain.document.model.mapper.DocumentMapper;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.daemawiki.domain.document.service;
package com.example.daemawiki.domain.document.component.service;

import com.example.daemawiki.domain.document.dto.request.SaveDocumentRequest;
import com.example.daemawiki.domain.document.model.type.service.GetDocumentType;
import com.example.daemawiki.domain.document.repository.DocumentRepository;
import com.example.daemawiki.domain.document.service.facade.DocumentFacade;
import com.example.daemawiki.domain.document.component.facade.DocumentFacade;
import com.example.daemawiki.domain.revision.component.RevisionComponent;
import com.example.daemawiki.domain.revision.dto.request.SaveRevisionHistoryRequest;
import com.example.daemawiki.domain.revision.model.type.RevisionType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;

import java.util.List;

@Builder
public record SaveDocumentRequest(
@NotBlank(message = "문서 제목이 비어있습니다.")
String title,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.daemawiki.domain.revision.model.mapper;

import com.example.daemawiki.domain.document.service.facade.DocumentFacade;
import com.example.daemawiki.domain.document.component.facade.DocumentFacade;
import com.example.daemawiki.domain.revision.dto.response.RevisionDocumentDetailResponse;
import com.example.daemawiki.domain.revision.model.RevisionHistory;
import org.springframework.stereotype.Component;
Expand Down

0 comments on commit cf52c5a

Please sign in to comment.