diff --git a/src/main/java/com/example/daemawiki/domain/auth/service/Signup.java b/src/main/java/com/example/daemawiki/domain/auth/service/Signup.java index d6c3c798..6eea31c6 100644 --- a/src/main/java/com/example/daemawiki/domain/auth/service/Signup.java +++ b/src/main/java/com/example/daemawiki/domain/auth/service/Signup.java @@ -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; @@ -14,6 +15,11 @@ import reactor.core.publisher.Mono; import reactor.core.scheduler.Scheduler; +/* + 유저 회원가입 성공시 + 해당 유저의 문서 생성 +*/ + @Service public class Signup { private final UserRepository userRepository; @@ -21,13 +27,15 @@ public class Signup { 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 execute(SignupRequest request) { @@ -51,7 +59,8 @@ public Mono execute(SignupRequest request) { .major(getMajorType.execute(request.major())) .build()) .build()) - .flatMap(userRepository::save); + .flatMap(userRepository::save) + .doOnSuccess(createDocumentByUser::execute); }))).then(); } diff --git a/src/main/java/com/example/daemawiki/domain/document/api/DocumentController.java b/src/main/java/com/example/daemawiki/domain/document/api/DocumentController.java index fcbc90cf..f4b3190c 100644 --- a/src/main/java/com/example/daemawiki/domain/document/api/DocumentController.java +++ b/src/main/java/com/example/daemawiki/domain/document/api/DocumentController.java @@ -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.*; diff --git a/src/main/java/com/example/daemawiki/domain/document/component/CreateDocumentByUser.java b/src/main/java/com/example/daemawiki/domain/document/component/CreateDocumentByUser.java new file mode 100644 index 00000000..c212c51f --- /dev/null +++ b/src/main/java/com/example/daemawiki/domain/document/component/CreateDocumentByUser.java @@ -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 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> 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); + } + +} diff --git a/src/main/java/com/example/daemawiki/domain/document/component/facade/CreateDocumentFacade.java b/src/main/java/com/example/daemawiki/domain/document/component/facade/CreateDocumentFacade.java new file mode 100644 index 00000000..e543e358 --- /dev/null +++ b/src/main/java/com/example/daemawiki/domain/document/component/facade/CreateDocumentFacade.java @@ -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(); + } + +} diff --git a/src/main/java/com/example/daemawiki/domain/document/service/facade/DocumentFacade.java b/src/main/java/com/example/daemawiki/domain/document/component/facade/DocumentFacade.java similarity index 94% rename from src/main/java/com/example/daemawiki/domain/document/service/facade/DocumentFacade.java rename to src/main/java/com/example/daemawiki/domain/document/component/facade/DocumentFacade.java index 754c46dc..ce7b014e 100644 --- a/src/main/java/com/example/daemawiki/domain/document/service/facade/DocumentFacade.java +++ b/src/main/java/com/example/daemawiki/domain/document/component/facade/DocumentFacade.java @@ -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; diff --git a/src/main/java/com/example/daemawiki/domain/document/component/service/CreateDocument.java b/src/main/java/com/example/daemawiki/domain/document/component/service/CreateDocument.java new file mode 100644 index 00000000..ce0c7128 --- /dev/null +++ b/src/main/java/com/example/daemawiki/domain/document/component/service/CreateDocument.java @@ -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 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())); + } + +} diff --git a/src/main/java/com/example/daemawiki/domain/document/service/DeleteDocument.java b/src/main/java/com/example/daemawiki/domain/document/component/service/DeleteDocument.java similarity index 95% rename from src/main/java/com/example/daemawiki/domain/document/service/DeleteDocument.java rename to src/main/java/com/example/daemawiki/domain/document/component/service/DeleteDocument.java index b9e00a03..14faf670 100644 --- a/src/main/java/com/example/daemawiki/domain/document/service/DeleteDocument.java +++ b/src/main/java/com/example/daemawiki/domain/document/component/service/DeleteDocument.java @@ -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; diff --git a/src/main/java/com/example/daemawiki/domain/document/service/GetDocument.java b/src/main/java/com/example/daemawiki/domain/document/component/service/GetDocument.java similarity index 91% rename from src/main/java/com/example/daemawiki/domain/document/service/GetDocument.java rename to src/main/java/com/example/daemawiki/domain/document/component/service/GetDocument.java index e1746407..444050bd 100644 --- a/src/main/java/com/example/daemawiki/domain/document/service/GetDocument.java +++ b/src/main/java/com/example/daemawiki/domain/document/component/service/GetDocument.java @@ -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; diff --git a/src/main/java/com/example/daemawiki/domain/document/service/UpdateDocument.java b/src/main/java/com/example/daemawiki/domain/document/component/service/UpdateDocument.java similarity index 93% rename from src/main/java/com/example/daemawiki/domain/document/service/UpdateDocument.java rename to src/main/java/com/example/daemawiki/domain/document/component/service/UpdateDocument.java index 306aebb5..693df743 100644 --- a/src/main/java/com/example/daemawiki/domain/document/service/UpdateDocument.java +++ b/src/main/java/com/example/daemawiki/domain/document/component/service/UpdateDocument.java @@ -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; diff --git a/src/main/java/com/example/daemawiki/domain/document/dto/request/SaveDocumentRequest.java b/src/main/java/com/example/daemawiki/domain/document/dto/request/SaveDocumentRequest.java index e4860ca8..6cd4605e 100644 --- a/src/main/java/com/example/daemawiki/domain/document/dto/request/SaveDocumentRequest.java +++ b/src/main/java/com/example/daemawiki/domain/document/dto/request/SaveDocumentRequest.java @@ -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, diff --git a/src/main/java/com/example/daemawiki/domain/document/service/CreateDocument.java b/src/main/java/com/example/daemawiki/domain/document/service/CreateDocument.java deleted file mode 100644 index 4886aa3b..00000000 --- a/src/main/java/com/example/daemawiki/domain/document/service/CreateDocument.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.example.daemawiki.domain.document.service; - -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.document.repository.DocumentRepository; -import com.example.daemawiki.domain.revision.dto.request.SaveRevisionHistoryRequest; -import com.example.daemawiki.domain.revision.model.type.RevisionType; -import com.example.daemawiki.domain.revision.component.RevisionComponent; -import com.example.daemawiki.domain.user.dto.UserDetailResponse; -import com.example.daemawiki.domain.user.service.facade.UserFacade; -import com.example.daemawiki.global.datetime.model.EditDateTime; -import org.springframework.stereotype.Service; -import reactor.core.publisher.Mono; - -import java.time.LocalDateTime; -import java.util.Objects; - -@Service -public class CreateDocument { - private final DocumentRepository documentRepository; - private final UserFacade userFacade; - private final RevisionComponent revisionComponent; - private final GetDocumentType getDocumentType; - - public CreateDocument(DocumentRepository documentRepository, UserFacade userFacade, RevisionComponent revisionComponent, GetDocumentType getDocumentType) { - this.documentRepository = documentRepository; - this.userFacade = userFacade; - this.revisionComponent = revisionComponent; - this.getDocumentType = getDocumentType; - } - - public Mono execute(SaveDocumentRequest request) { - return userFacade.currentUser() - .map(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(); - }) - .flatMap(documentRepository::save) - .flatMap(document -> revisionComponent.saveHistory(SaveRevisionHistoryRequest.builder() - .type(RevisionType.CREATE) - .documentId(document.getId()) - .title(document.getTitle()) - .build())); - } - -} diff --git a/src/main/java/com/example/daemawiki/domain/revision/model/mapper/RevisionMapper.java b/src/main/java/com/example/daemawiki/domain/revision/model/mapper/RevisionMapper.java index c0432ba0..a47cc138 100644 --- a/src/main/java/com/example/daemawiki/domain/revision/model/mapper/RevisionMapper.java +++ b/src/main/java/com/example/daemawiki/domain/revision/model/mapper/RevisionMapper.java @@ -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;