This repository has been archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/example/daemawiki/domain/content/dto/EditContentTableTitleRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.example.daemawiki.domain.content.dto; | ||
|
||
public record EditContentTableTitleRequest( | ||
String index, | ||
String newTitle, | ||
Integer version | ||
) { | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/example/daemawiki/domain/content/service/EditContentTableTitle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Void> 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())); | ||
}); | ||
} | ||
|
||
} |