From fdf1970a7c1401ab2727848c39d881580353e008 Mon Sep 17 00:00:00 2001 From: 101 Date: Sat, 16 Mar 2024 18:31:49 +0900 Subject: [PATCH] refactor DeleteContentTable.java --- .../content/service/DeleteContentTable.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/example/daemawiki/domain/content/service/DeleteContentTable.java b/src/main/java/com/example/daemawiki/domain/content/service/DeleteContentTable.java index bc228e5f..92212289 100644 --- a/src/main/java/com/example/daemawiki/domain/content/service/DeleteContentTable.java +++ b/src/main/java/com/example/daemawiki/domain/content/service/DeleteContentTable.java @@ -8,12 +8,14 @@ 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 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 com.example.daemawiki.global.exception.h500.ExecuteFailedException; import org.springframework.stereotype.Service; import reactor.core.publisher.Mono; +import reactor.util.function.Tuple2; @Service public class DeleteContentTable { @@ -34,20 +36,26 @@ public DeleteContentTable(DocumentFacade documentFacade, RevisionComponent revis public Mono execute(DeleteContentRequest request, String documentId) { return userFacade.currentUser() .zipWith(documentFacade.findDocumentById(documentId)) - .map(tuple -> { - userFilter.checkUserAndDocument(tuple.getT1(), tuple.getT2(), request.version()); - return tuple; - }) - .map(tuple -> { - removeContent(tuple.getT2(), request.index()); - updateDocumentEditorAndUpdatedDate.execute(tuple.getT2(), tuple.getT1()); - return tuple.getT2(); - }) + .map(tuple -> checkUserPermissionAndVersion(tuple, request.version())) + .map(tuple -> deleteDocumentContentTable(tuple, request)) .flatMap(document -> documentFacade.saveDocument(document) .then(createRevision(document))) .onErrorMap(this::mapException); } + private DefaultDocument deleteDocumentContentTable(Tuple2 tuple, DeleteContentRequest request) { + removeContent(tuple.getT2(), request.index()); + updateDocumentEditorAndUpdatedDate.execute(tuple.getT2(), tuple.getT1()); + + return tuple.getT2(); + } + + private Tuple2 checkUserPermissionAndVersion(Tuple2 tuple, int version) { + userFilter.userPermissionAndDocumentVersionCheck(tuple.getT2(), tuple.getT1().getEmail(), version); + return tuple; + } + + private Mono createRevision(DefaultDocument document) { return revisionComponent.saveHistory(SaveRevisionHistoryRequest .create(RevisionType.UPDATE, document.getId(), document.getTitle()));