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

Commit

Permalink
조회수 추가 및 버전관리 변경
Browse files Browse the repository at this point in the history
조회수 save 이슈로 @Version 삭제 후 version 증가 코드 작성
  • Loading branch information
ori0o0p committed Mar 1, 2024
1 parent fbbda61 commit dcfbe78
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public Mono<Void> execute(AddContentRequest request) {
};

document.getContent().sort(customComparator);
document.increaseVersion();

return Mono.just(document);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public WriteContent(DocumentFacade documentFacade, DocumentRepository documentRe
public Mono<Void> 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);
Expand All @@ -48,13 +50,12 @@ public Mono<Void> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<DefaultDocument> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public DefaultDocument execute(SaveDocumentRequest request, User user) {
.build())
.content(Collections.singletonList(request.content()))
.groups(request.groups())
.view(0)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,4 +34,9 @@ public Flux<DefaultDocument> getDocumentOrderByVersion() {
return documentRepository.findTop10ByOrderByVersionDesc();
}

public Mono<DefaultDocument> saveDocument(DefaultDocument document) {
return documentRepository.save(document)
.onErrorMap(e -> ExecuteFailedException.EXCEPTION);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -28,7 +28,7 @@ public CreateDocument(DocumentRepository documentRepository, UserFacade userFaca
public Mono<Void> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +22,9 @@ public GetDocument(DocumentFacade documentFacade, DocumentMapper documentMapper)

public Mono<GetDocumentResponse> getDocumentById(String id) {
return documentFacade.findDocumentById(id)
.flatMap(documentMapper::defaultDocumentToGetResponse);
.doOnSuccess(DefaultDocument::increaseView)
.flatMap(documentFacade::saveDocument)
.flatMap(documentMapper::defaultDocumentToGetResponse);
}

public Mono<GetDocumentResponse> getDocumentByRandom() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -39,6 +36,9 @@ public Mono<Void> 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())
Expand All @@ -51,12 +51,11 @@ public Mono<Void> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public record GetDocumentResponse(
List<List<String>> groups,
DocumentEditor editor,
List<Contents> content,
Integer view,

Integer version
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,8 +38,9 @@ public class DefaultDocument {

private List<Contents> content;

@Version
private Integer version;
private Integer view = 0;

private Integer version = 0;

@Builder
public DefaultDocument(String title, DocumentType type, EditDateTime dateTime, List<Info> info, List<List<String>> groups, DocumentEditor documentEditor, List<Contents> content) {
Expand All @@ -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<List<String>> groups) {
this.title = title;
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class DocumentSearchResult {
private DocumentType type;
private EditDateTime dateTime;
private Contents content;
private Integer view;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public Mono<GetDocumentResponse> defaultDocumentToGetResponse(DefaultDocument do
.groups(document.getGroups())
.editor(document.getEditor())
.content(document.getContent())
.view(document.getView())
.version(document.getVersion())
.build());
}
Expand All @@ -29,6 +30,7 @@ public Mono<SimpleDocumentResponse> defaultDocumentToSimpleDocumentResponse(Defa
.title(document.getTitle())
.numberOfUpdate(document.getVersion())
.updatedDate(document.getDateTime().getUpdated())
.view(document.getView())
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public Mono<Void> execute(UpdateInfoRequest request) {
return Mono.error(NoEditPermissionUserException.EXCEPTION);
}
document.setInfo(request.infoList());
document.increaseVersion();
return documentRepository.save(document);
}))
.flatMap(document -> revisionComponent.saveHistory(SaveRevisionHistoryRequest.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public Mono<Void> execute(EditUserRequest request) {
}

document.updateByUserEdit(user.getName(), newGroups);
document.increaseVersion();

return documentRepository.save(document);
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public Mono<Void> execute(UpdateClubRequest request) {

document.setGroups(newGroups);
}
document.increaseVersion();
})
.flatMap(documentRepository::save))
.subscribeOn(scheduler)
Expand Down

0 comments on commit dcfbe78

Please sign in to comment.