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

Commit

Permalink
refactor DeleteContentTable.java
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p committed Mar 16, 2024
1 parent d8de098 commit fdf1970
Showing 1 changed file with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,20 +36,26 @@ public DeleteContentTable(DocumentFacade documentFacade, RevisionComponent revis
public Mono<Void> 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<User, DefaultDocument> tuple, DeleteContentRequest request) {
removeContent(tuple.getT2(), request.index());
updateDocumentEditorAndUpdatedDate.execute(tuple.getT2(), tuple.getT1());

return tuple.getT2();
}

private Tuple2<User, DefaultDocument> checkUserPermissionAndVersion(Tuple2<User, DefaultDocument> tuple, int version) {
userFilter.userPermissionAndDocumentVersionCheck(tuple.getT2(), tuple.getT1().getEmail(), version);
return tuple;
}


private Mono<Void> createRevision(DefaultDocument document) {
return revisionComponent.saveHistory(SaveRevisionHistoryRequest
.create(RevisionType.UPDATE, document.getId(), document.getTitle()));
Expand Down

0 comments on commit fdf1970

Please sign in to comment.