From dcfbe780d6e997f2191b87d00d199e02e7af0809 Mon Sep 17 00:00:00 2001 From: 101 Date: Sat, 2 Mar 2024 01:09:59 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A1=B0=ED=9A=8C=EC=88=98=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EB=B0=8F=20=EB=B2=84=EC=A0=84=EA=B4=80=EB=A6=AC=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 조회수 save 이슈로 @version 삭제 후 version 증가 코드 작성 --- .../domain/content/service/AddContentTable.java | 1 + .../domain/content/service/WriteContent.java | 5 +++-- .../document/component/CreateDocumentByUser.java | 10 +++++----- .../component/facade/CreateDocumentFacade.java | 1 + .../document/component/facade/DocumentFacade.java | 6 ++++++ .../document/component/service/CreateDocument.java | 10 +++++----- .../document/component/service/GetDocument.java | 5 ++++- .../document/component/service/UpdateDocument.java | 13 ++++++------- .../document/dto/response/GetDocumentResponse.java | 1 + .../dto/response/SimpleDocumentResponse.java | 3 ++- .../domain/document/model/DefaultDocument.java | 14 +++++++++++--- .../document/model/DocumentSearchResult.java | 1 + .../document/model/mapper/DocumentMapper.java | 2 ++ .../daemawiki/domain/info/service/UpdateInfo.java | 1 + .../daemawiki/domain/user/service/EditUser.java | 1 + .../daemawiki/domain/user/service/UpdateClub.java | 1 + 16 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/example/daemawiki/domain/content/service/AddContentTable.java b/src/main/java/com/example/daemawiki/domain/content/service/AddContentTable.java index 24ac0c79..a5fef199 100644 --- a/src/main/java/com/example/daemawiki/domain/content/service/AddContentTable.java +++ b/src/main/java/com/example/daemawiki/domain/content/service/AddContentTable.java @@ -65,6 +65,7 @@ public Mono execute(AddContentRequest request) { }; document.getContent().sort(customComparator); + document.increaseVersion(); return Mono.just(document); }) diff --git a/src/main/java/com/example/daemawiki/domain/content/service/WriteContent.java b/src/main/java/com/example/daemawiki/domain/content/service/WriteContent.java index 8f403a74..1649cfc1 100644 --- a/src/main/java/com/example/daemawiki/domain/content/service/WriteContent.java +++ b/src/main/java/com/example/daemawiki/domain/content/service/WriteContent.java @@ -37,6 +37,8 @@ public WriteContent(DocumentFacade documentFacade, DocumentRepository documentRe public Mono execute(WriteContentRequest request) { return userFacade.currentUser() .flatMap(user -> documentFacade.findDocumentById(request.documentId()) + .filter(document -> Objects.equals(document.getVersion(), request.version())) + .switchIfEmpty(Mono.error(VersionMismatchException.EXCEPTION)) .flatMap(document -> { if (document.getEditor().hasEditPermission(user.getEmail())) { return Mono.error(NoEditPermissionUserException.EXCEPTION); @@ -48,13 +50,12 @@ public Mono execute(WriteContentRequest request) { if (contentsMap.containsKey(request.index())) { Contents content = contentsMap.get(request.index()); content.setContent(request.content()); + document.increaseVersion(); return Mono.just(document); } else { return Mono.error(ContentNotFoundException.EXCEPTION); } }) - .filter(document -> Objects.equals(document.getVersion(), request.version())) - .switchIfEmpty(Mono.error(VersionMismatchException.EXCEPTION)) .flatMap(documentRepository::save)) .flatMap(document -> revisionComponent.saveHistory(SaveRevisionHistoryRequest.builder() .type(RevisionType.UPDATE) diff --git a/src/main/java/com/example/daemawiki/domain/document/component/CreateDocumentByUser.java b/src/main/java/com/example/daemawiki/domain/document/component/CreateDocumentByUser.java index 2fa25415..72741a46 100644 --- a/src/main/java/com/example/daemawiki/domain/document/component/CreateDocumentByUser.java +++ b/src/main/java/com/example/daemawiki/domain/document/component/CreateDocumentByUser.java @@ -2,9 +2,9 @@ import com.example.daemawiki.domain.content.model.Contents; import com.example.daemawiki.domain.document.component.facade.CreateDocumentFacade; +import com.example.daemawiki.domain.document.component.facade.DocumentFacade; import com.example.daemawiki.domain.document.dto.request.SaveDocumentRequest; import com.example.daemawiki.domain.document.model.DefaultDocument; -import com.example.daemawiki.domain.document.repository.DocumentRepository; 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; @@ -18,18 +18,18 @@ @Component public class CreateDocumentByUser { - private final DocumentRepository documentRepository; + private final DocumentFacade documentFacade; private final CreateDocumentFacade createDocumentFacade; private final RevisionComponent revisionComponent; - public CreateDocumentByUser(DocumentRepository documentRepository, CreateDocumentFacade createDocumentFacade, RevisionComponent revisionComponent) { - this.documentRepository = documentRepository; + public CreateDocumentByUser(DocumentFacade documentFacade, CreateDocumentFacade createDocumentFacade, RevisionComponent revisionComponent) { + this.documentFacade = documentFacade; this.createDocumentFacade = createDocumentFacade; this.revisionComponent = revisionComponent; } public Mono execute(User user) { - return documentRepository.save(createDocument(user)) + return documentFacade.saveDocument(createDocument(user)) .flatMap(document -> revisionComponent.saveHistory(SaveRevisionHistoryRequest.builder() .type(RevisionType.CREATE) .documentId(document.getId()) diff --git a/src/main/java/com/example/daemawiki/domain/document/component/facade/CreateDocumentFacade.java b/src/main/java/com/example/daemawiki/domain/document/component/facade/CreateDocumentFacade.java index 48808761..1efac6cd 100644 --- a/src/main/java/com/example/daemawiki/domain/document/component/facade/CreateDocumentFacade.java +++ b/src/main/java/com/example/daemawiki/domain/document/component/facade/CreateDocumentFacade.java @@ -45,6 +45,7 @@ public DefaultDocument execute(SaveDocumentRequest request, User user) { .build()) .content(Collections.singletonList(request.content())) .groups(request.groups()) + .view(0) .build(); } diff --git a/src/main/java/com/example/daemawiki/domain/document/component/facade/DocumentFacade.java b/src/main/java/com/example/daemawiki/domain/document/component/facade/DocumentFacade.java index fd311036..01a62d7d 100644 --- a/src/main/java/com/example/daemawiki/domain/document/component/facade/DocumentFacade.java +++ b/src/main/java/com/example/daemawiki/domain/document/component/facade/DocumentFacade.java @@ -4,6 +4,7 @@ import com.example.daemawiki.domain.document.model.DocumentSearchResult; import com.example.daemawiki.domain.document.repository.DocumentRepository; import com.example.daemawiki.global.exception.h404.DocumentNotFoundException; +import com.example.daemawiki.global.exception.h500.ExecuteFailedException; import org.springframework.stereotype.Component; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -33,4 +34,9 @@ public Flux getDocumentOrderByVersion() { return documentRepository.findTop10ByOrderByVersionDesc(); } + public Mono saveDocument(DefaultDocument document) { + return documentRepository.save(document) + .onErrorMap(e -> ExecuteFailedException.EXCEPTION); + } + } diff --git a/src/main/java/com/example/daemawiki/domain/document/component/service/CreateDocument.java b/src/main/java/com/example/daemawiki/domain/document/component/service/CreateDocument.java index 1d0a626f..2877f7fd 100644 --- a/src/main/java/com/example/daemawiki/domain/document/component/service/CreateDocument.java +++ b/src/main/java/com/example/daemawiki/domain/document/component/service/CreateDocument.java @@ -1,8 +1,8 @@ package com.example.daemawiki.domain.document.component.service; import com.example.daemawiki.domain.document.component.facade.CreateDocumentFacade; +import com.example.daemawiki.domain.document.component.facade.DocumentFacade; import com.example.daemawiki.domain.document.dto.request.SaveDocumentRequest; -import com.example.daemawiki.domain.document.repository.DocumentRepository; 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; @@ -13,13 +13,13 @@ @Service public class CreateDocument { - private final DocumentRepository documentRepository; + private final DocumentFacade documentFacade; private final UserFacade userFacade; private final RevisionComponent revisionComponent; private final CreateDocumentFacade createDocumentFacade; - public CreateDocument(DocumentRepository documentRepository, UserFacade userFacade, RevisionComponent revisionComponent, CreateDocumentFacade createDocumentFacade) { - this.documentRepository = documentRepository; + public CreateDocument(DocumentFacade documentFacade, UserFacade userFacade, RevisionComponent revisionComponent, CreateDocumentFacade createDocumentFacade) { + this.documentFacade = documentFacade; this.userFacade = userFacade; this.revisionComponent = revisionComponent; this.createDocumentFacade = createDocumentFacade; @@ -28,7 +28,7 @@ public CreateDocument(DocumentRepository documentRepository, UserFacade userFaca public Mono execute(SaveDocumentRequest request) { return userFacade.currentUser() .map(user -> createDocumentFacade.execute(request, user)) - .flatMap(documentRepository::save) + .flatMap(documentFacade::saveDocument) .flatMap(document -> revisionComponent.saveHistory(SaveRevisionHistoryRequest.builder() .type(RevisionType.CREATE) .documentId(document.getId()) diff --git a/src/main/java/com/example/daemawiki/domain/document/component/service/GetDocument.java b/src/main/java/com/example/daemawiki/domain/document/component/service/GetDocument.java index 91ab99d7..d5e0c80e 100644 --- a/src/main/java/com/example/daemawiki/domain/document/component/service/GetDocument.java +++ b/src/main/java/com/example/daemawiki/domain/document/component/service/GetDocument.java @@ -3,6 +3,7 @@ import com.example.daemawiki.domain.document.component.facade.DocumentFacade; import com.example.daemawiki.domain.document.dto.response.GetDocumentResponse; import com.example.daemawiki.domain.document.dto.response.SimpleDocumentResponse; +import com.example.daemawiki.domain.document.model.DefaultDocument; import com.example.daemawiki.domain.document.model.DocumentSearchResult; import com.example.daemawiki.domain.document.model.mapper.DocumentMapper; import org.springframework.stereotype.Service; @@ -21,7 +22,9 @@ public GetDocument(DocumentFacade documentFacade, DocumentMapper documentMapper) public Mono getDocumentById(String id) { return documentFacade.findDocumentById(id) - .flatMap(documentMapper::defaultDocumentToGetResponse); + .doOnSuccess(DefaultDocument::increaseView) + .flatMap(documentFacade::saveDocument) + .flatMap(documentMapper::defaultDocumentToGetResponse); } public Mono getDocumentByRandom() { diff --git a/src/main/java/com/example/daemawiki/domain/document/component/service/UpdateDocument.java b/src/main/java/com/example/daemawiki/domain/document/component/service/UpdateDocument.java index 06d5c78a..6965982e 100644 --- a/src/main/java/com/example/daemawiki/domain/document/component/service/UpdateDocument.java +++ b/src/main/java/com/example/daemawiki/domain/document/component/service/UpdateDocument.java @@ -3,7 +3,6 @@ import com.example.daemawiki.domain.document.component.facade.DocumentFacade; import com.example.daemawiki.domain.document.dto.request.SaveDocumentRequest; import com.example.daemawiki.domain.document.model.type.service.GetDocumentType; -import com.example.daemawiki.domain.document.repository.DocumentRepository; 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; @@ -21,14 +20,12 @@ public class UpdateDocument { private final DocumentFacade documentFacade; private final UserFacade userFacade; - private final DocumentRepository documentRepository; private final GetDocumentType getDocumentType; private final RevisionComponent revisionComponent; - public UpdateDocument(DocumentFacade documentFacade, UserFacade userFacade, DocumentRepository documentRepository, GetDocumentType getDocumentType, RevisionComponent revisionComponent) { + public UpdateDocument(DocumentFacade documentFacade, UserFacade userFacade, GetDocumentType getDocumentType, RevisionComponent revisionComponent) { this.documentFacade = documentFacade; this.userFacade = userFacade; - this.documentRepository = documentRepository; this.getDocumentType = getDocumentType; this.revisionComponent = revisionComponent; } @@ -39,6 +36,9 @@ public Mono execute(SaveDocumentRequest request, String documentId) { if (document.getEditor().hasEditPermission(user.getEmail())) { throw NoEditPermissionUserException.EXCEPTION; } + if (!Objects.equals(document.getVersion(), request.version())) { + throw VersionMismatchException.EXCEPTION; + } document.getEditor().update(UserDetailResponse.builder() .id(user.getId()) @@ -51,12 +51,11 @@ public Mono execute(SaveDocumentRequest request, String documentId) { request.groups()); document.getContent().add(request.content()); + document.increaseVersion(); return document; }) - .filter(document -> Objects.equals(document.getVersion(), request.version())) - .switchIfEmpty(Mono.error(VersionMismatchException.EXCEPTION)) - .flatMap(documentRepository::save) + .flatMap(documentFacade::saveDocument) .flatMap(d -> revisionComponent.saveHistory(SaveRevisionHistoryRequest.builder() .type(RevisionType.UPDATE) .documentId(d.getId()) diff --git a/src/main/java/com/example/daemawiki/domain/document/dto/response/GetDocumentResponse.java b/src/main/java/com/example/daemawiki/domain/document/dto/response/GetDocumentResponse.java index 858ddfff..a5c7efa1 100644 --- a/src/main/java/com/example/daemawiki/domain/document/dto/response/GetDocumentResponse.java +++ b/src/main/java/com/example/daemawiki/domain/document/dto/response/GetDocumentResponse.java @@ -19,6 +19,7 @@ public record GetDocumentResponse( List> groups, DocumentEditor editor, List content, + Integer view, Integer version ) { diff --git a/src/main/java/com/example/daemawiki/domain/document/dto/response/SimpleDocumentResponse.java b/src/main/java/com/example/daemawiki/domain/document/dto/response/SimpleDocumentResponse.java index 127db89f..e7d2fdfd 100644 --- a/src/main/java/com/example/daemawiki/domain/document/dto/response/SimpleDocumentResponse.java +++ b/src/main/java/com/example/daemawiki/domain/document/dto/response/SimpleDocumentResponse.java @@ -11,6 +11,7 @@ public record SimpleDocumentResponse( String title, Integer numberOfUpdate, @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "Asia/Seoul") - LocalDateTime updatedDate + LocalDateTime updatedDate, + Integer view ) { } diff --git a/src/main/java/com/example/daemawiki/domain/document/model/DefaultDocument.java b/src/main/java/com/example/daemawiki/domain/document/model/DefaultDocument.java index 1991e9a0..cec427df 100644 --- a/src/main/java/com/example/daemawiki/domain/document/model/DefaultDocument.java +++ b/src/main/java/com/example/daemawiki/domain/document/model/DefaultDocument.java @@ -11,7 +11,6 @@ import lombok.Setter; import org.eclipse.collections.api.factory.Lists; import org.springframework.data.annotation.Id; -import org.springframework.data.annotation.Version; import org.springframework.data.mongodb.core.mapping.Document; import java.util.List; @@ -39,8 +38,9 @@ public class DefaultDocument { private List content; - @Version - private Integer version; + private Integer view = 0; + + private Integer version = 0; @Builder public DefaultDocument(String title, DocumentType type, EditDateTime dateTime, List info, List> groups, DocumentEditor documentEditor, List content) { @@ -53,6 +53,14 @@ public DefaultDocument(String title, DocumentType type, EditDateTime dateTime, L this.content = content; } + public void increaseView() { + this.view++; + } + + public void increaseVersion() { + this.version++; + } + public void update(String title, DocumentType type, List> groups) { this.title = title; this.type = type; diff --git a/src/main/java/com/example/daemawiki/domain/document/model/DocumentSearchResult.java b/src/main/java/com/example/daemawiki/domain/document/model/DocumentSearchResult.java index 216e535a..45ea8e82 100644 --- a/src/main/java/com/example/daemawiki/domain/document/model/DocumentSearchResult.java +++ b/src/main/java/com/example/daemawiki/domain/document/model/DocumentSearchResult.java @@ -12,4 +12,5 @@ public class DocumentSearchResult { private DocumentType type; private EditDateTime dateTime; private Contents content; + private Integer view; } \ No newline at end of file diff --git a/src/main/java/com/example/daemawiki/domain/document/model/mapper/DocumentMapper.java b/src/main/java/com/example/daemawiki/domain/document/model/mapper/DocumentMapper.java index b81b38f7..01dfce5a 100644 --- a/src/main/java/com/example/daemawiki/domain/document/model/mapper/DocumentMapper.java +++ b/src/main/java/com/example/daemawiki/domain/document/model/mapper/DocumentMapper.java @@ -19,6 +19,7 @@ public Mono defaultDocumentToGetResponse(DefaultDocument do .groups(document.getGroups()) .editor(document.getEditor()) .content(document.getContent()) + .view(document.getView()) .version(document.getVersion()) .build()); } @@ -29,6 +30,7 @@ public Mono defaultDocumentToSimpleDocumentResponse(Defa .title(document.getTitle()) .numberOfUpdate(document.getVersion()) .updatedDate(document.getDateTime().getUpdated()) + .view(document.getView()) .build()); } diff --git a/src/main/java/com/example/daemawiki/domain/info/service/UpdateInfo.java b/src/main/java/com/example/daemawiki/domain/info/service/UpdateInfo.java index 50011a70..95f5ca96 100644 --- a/src/main/java/com/example/daemawiki/domain/info/service/UpdateInfo.java +++ b/src/main/java/com/example/daemawiki/domain/info/service/UpdateInfo.java @@ -39,6 +39,7 @@ public Mono execute(UpdateInfoRequest request) { return Mono.error(NoEditPermissionUserException.EXCEPTION); } document.setInfo(request.infoList()); + document.increaseVersion(); return documentRepository.save(document); })) .flatMap(document -> revisionComponent.saveHistory(SaveRevisionHistoryRequest.builder() diff --git a/src/main/java/com/example/daemawiki/domain/user/service/EditUser.java b/src/main/java/com/example/daemawiki/domain/user/service/EditUser.java index 3e3329b4..a46b10a3 100644 --- a/src/main/java/com/example/daemawiki/domain/user/service/EditUser.java +++ b/src/main/java/com/example/daemawiki/domain/user/service/EditUser.java @@ -48,6 +48,7 @@ public Mono execute(EditUserRequest request) { } document.updateByUserEdit(user.getName(), newGroups); + document.increaseVersion(); return documentRepository.save(document); })) diff --git a/src/main/java/com/example/daemawiki/domain/user/service/UpdateClub.java b/src/main/java/com/example/daemawiki/domain/user/service/UpdateClub.java index 3f827de4..e5e6b909 100644 --- a/src/main/java/com/example/daemawiki/domain/user/service/UpdateClub.java +++ b/src/main/java/com/example/daemawiki/domain/user/service/UpdateClub.java @@ -50,6 +50,7 @@ public Mono execute(UpdateClubRequest request) { document.setGroups(newGroups); } + document.increaseVersion(); }) .flatMap(documentRepository::save)) .subscribeOn(scheduler)