From ba4e0f57db58a74b7bb81cf50ed81040def285b0 Mon Sep 17 00:00:00 2001 From: 101 Date: Mon, 4 Mar 2024 13:24:56 +0900 Subject: [PATCH] =?UTF-8?q?title=20=EB=B3=80=EA=B2=BD=20=EC=84=9C=EB=B9=84?= =?UTF-8?q?=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/DocumentContentController.java | 14 ++++- .../dto/EditContentTableTitleRequest.java | 8 +++ .../service/EditContentTableTitle.java | 57 +++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/example/daemawiki/domain/content/dto/EditContentTableTitleRequest.java create mode 100644 src/main/java/com/example/daemawiki/domain/content/service/EditContentTableTitle.java diff --git a/src/main/java/com/example/daemawiki/domain/content/api/DocumentContentController.java b/src/main/java/com/example/daemawiki/domain/content/api/DocumentContentController.java index c35d6615..67f24bcb 100644 --- a/src/main/java/com/example/daemawiki/domain/content/api/DocumentContentController.java +++ b/src/main/java/com/example/daemawiki/domain/content/api/DocumentContentController.java @@ -2,9 +2,11 @@ import com.example.daemawiki.domain.content.dto.AddContentRequest; import com.example.daemawiki.domain.content.dto.DeleteContentRequest; +import com.example.daemawiki.domain.content.dto.EditContentTableTitleRequest; import com.example.daemawiki.domain.content.dto.WriteContentRequest; import com.example.daemawiki.domain.content.service.AddContentTable; import com.example.daemawiki.domain.content.service.DeleteContent; +import com.example.daemawiki.domain.content.service.EditContentTableTitle; import com.example.daemawiki.domain.content.service.WriteContent; import jakarta.validation.Valid; import jakarta.validation.constraints.NotBlank; @@ -18,11 +20,13 @@ public class DocumentContentController { private final WriteContent writeContentService; private final AddContentTable addContentTableService; private final DeleteContent deleteContentService; + private final EditContentTableTitle editContentTableTitleService; - public DocumentContentController(WriteContent writeContentService, AddContentTable addContentTableService, DeleteContent deleteContentService) { + public DocumentContentController(WriteContent writeContentService, AddContentTable addContentTableService, DeleteContent deleteContentService, EditContentTableTitle editContentTableTitleService) { this.writeContentService = writeContentService; this.addContentTableService = addContentTableService; this.deleteContentService = deleteContentService; + this.editContentTableTitleService = editContentTableTitleService; } @PatchMapping @@ -42,5 +46,11 @@ public Mono addContentTable(@Valid @RequestBody AddContentRequest request, public Mono deleteContent(@RequestBody DeleteContentRequest request, @NotBlank @PathVariable String documentId) { return deleteContentService.execute(request, documentId); } - + + @PatchMapping("/edit") + @ResponseStatus(HttpStatus.NO_CONTENT) + public Mono editContentTitle(@RequestBody EditContentTableTitleRequest request, @PathVariable String documentId) { + return editContentTableTitleService.execute(request, documentId); + } + } diff --git a/src/main/java/com/example/daemawiki/domain/content/dto/EditContentTableTitleRequest.java b/src/main/java/com/example/daemawiki/domain/content/dto/EditContentTableTitleRequest.java new file mode 100644 index 00000000..433cf97c --- /dev/null +++ b/src/main/java/com/example/daemawiki/domain/content/dto/EditContentTableTitleRequest.java @@ -0,0 +1,8 @@ +package com.example.daemawiki.domain.content.dto; + +public record EditContentTableTitleRequest( + String index, + String newTitle, + Integer version +) { +} diff --git a/src/main/java/com/example/daemawiki/domain/content/service/EditContentTableTitle.java b/src/main/java/com/example/daemawiki/domain/content/service/EditContentTableTitle.java new file mode 100644 index 00000000..01233163 --- /dev/null +++ b/src/main/java/com/example/daemawiki/domain/content/service/EditContentTableTitle.java @@ -0,0 +1,57 @@ +package com.example.daemawiki.domain.content.service; + +import com.example.daemawiki.domain.content.dto.EditContentTableTitleRequest; +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; +import com.example.daemawiki.domain.user.service.facade.UserFacade; +import com.example.daemawiki.global.exception.h400.VersionMismatchException; +import com.example.daemawiki.global.exception.h403.NoEditPermissionUserException; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Mono; + +import java.util.Objects; + +@Service +public class EditContentTableTitle { + private final DocumentFacade documentFacade; + private final RevisionComponent revisionComponent; + private final UserFacade userFacade; + + public EditContentTableTitle(DocumentFacade documentFacade, RevisionComponent revisionComponent, UserFacade userFacade) { + this.documentFacade = documentFacade; + this.revisionComponent = revisionComponent; + this.userFacade = userFacade; + } + + public Mono execute(EditContentTableTitleRequest request, String documentId) { + return userFacade.currentUser() + .zipWith(documentFacade.findDocumentById(documentId), (user, document) -> { + if (!Objects.equals(document.getVersion(), request.version())) { + throw VersionMismatchException.EXCEPTION; + } + if (document.getEditor().hasEditPermission(user.getEmail())) { + throw NoEditPermissionUserException.EXCEPTION; + } + return document; + }) + .flatMap(document -> { + document.getContent() + .stream() + .filter(c -> c.getIndex().equals(request.index())) + .findFirst() + .ifPresent(contents -> contents.setTitle(request.newTitle())); + + document.increaseVersion(); + + return documentFacade.saveDocument(document) + .then(revisionComponent.saveHistory(SaveRevisionHistoryRequest.builder() + .type(RevisionType.UPDATE) + .documentId(documentId) + .title(document.getTitle()) + .build())); + }); + } + +}