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

Commit

Permalink
title 변경 서비스 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p committed Mar 4, 2024
1 parent 969f7db commit ba4e0f5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -42,5 +46,11 @@ public Mono<Void> addContentTable(@Valid @RequestBody AddContentRequest request,
public Mono<Void> deleteContent(@RequestBody DeleteContentRequest request, @NotBlank @PathVariable String documentId) {
return deleteContentService.execute(request, documentId);
}


@PatchMapping("/edit")
@ResponseStatus(HttpStatus.NO_CONTENT)
public Mono<Void> editContentTitle(@RequestBody EditContentTableTitleRequest request, @PathVariable String documentId) {
return editContentTableTitleService.execute(request, documentId);
}

}
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
) {
}
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()));
});
}

}

0 comments on commit ba4e0f5

Please sign in to comment.