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

Commit

Permalink
api 개발
Browse files Browse the repository at this point in the history
목차, 문서 정보
  • Loading branch information
ori0o0p committed Feb 27, 2024
1 parent 9dcf0a1 commit 5a7daaa
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.daemawiki.domain.content.dto;

public record AddContentRequest(
String documentId,
String index,
String title
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.daemawiki.domain.content.dto;

public record WriteContentRequest(
String documentId,
String index,
String content
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.daemawiki.domain.content.model;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class Contents {

private String index;

private String title;

private String content;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.example.daemawiki.domain.content.service;

import com.example.daemawiki.domain.content.dto.AddContentRequest;
import com.example.daemawiki.domain.content.model.Contents;
import com.example.daemawiki.domain.document.component.facade.DocumentFacade;
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;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.Comparator;

@Service
public class AddContentTable {
private final DocumentFacade documentFacade;
private final DocumentRepository documentRepository;
private final RevisionComponent revisionComponent;

public AddContentTable(DocumentFacade documentFacade, DocumentRepository documentRepository, RevisionComponent revisionComponent) {
this.documentFacade = documentFacade;
this.documentRepository = documentRepository;
this.revisionComponent = revisionComponent;
}

public Mono<Void> execute(AddContentRequest request) {
return documentFacade.findDocumentById(request.documentId())
.map(document -> {
Contents newContent = Contents.builder()
.index(request.index())
.title(request.title())
.content("빈 내용")
.build();

document.getContent().add(newContent);

Comparator<Contents> customComparator = (c1, c2) -> {
String[] index1 = c1.getIndex().split("\\.");
String[] index2 = c2.getIndex().split("\\.");

for (int i = 0; i < Math.max(index1.length, index2.length); i++) {
int part1 = i < index1.length ? Integer.parseInt(index1[i]) : 0;
int part2 = i < index2.length ? Integer.parseInt(index2[i]) : 0;

if (part1 != part2) {
return part1 - part2;
}
}

return 0;
};

document.getContent().sort(customComparator);

return document;
})
.flatMap(documentRepository::save)
.flatMap(document -> revisionComponent.saveHistory(SaveRevisionHistoryRequest.builder()
.type(RevisionType.UPDATE)
.documentId(request.documentId())
.title(document.getTitle())
.build()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.daemawiki.domain.content.service;

import com.example.daemawiki.domain.content.dto.WriteContentRequest;
import com.example.daemawiki.domain.content.model.Contents;
import com.example.daemawiki.domain.document.component.facade.DocumentFacade;
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;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class WriteContent {
private final DocumentFacade documentFacade;
private final DocumentRepository documentRepository;
private final RevisionComponent revisionComponent;

public WriteContent(DocumentFacade documentFacade, DocumentRepository documentRepository, RevisionComponent revisionComponent) {
this.documentFacade = documentFacade;
this.documentRepository = documentRepository;
this.revisionComponent = revisionComponent;
}

public Mono<Void> execute(WriteContentRequest request) {
return documentFacade.findDocumentById(request.documentId())
.map(document -> {
Map<String, Contents> contentsMap = document.getContent().stream()
.collect(Collectors.toMap(Contents::getIndex, Function.identity()));

if (contentsMap.containsKey(request.index())) {
Contents content = contentsMap.get(request.index());
content.setContent(request.content());
}

return document;
})
.flatMap(documentRepository::save)
.flatMap(document -> revisionComponent.saveHistory(SaveRevisionHistoryRequest.builder()
.type(RevisionType.UPDATE)
.documentId(request.documentId())
.title(document.getTitle())
.build()));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.example.daemawiki.domain.document.api;

import com.example.daemawiki.domain.content.dto.AddContentRequest;
import com.example.daemawiki.domain.content.dto.WriteContentRequest;
import com.example.daemawiki.domain.content.service.AddContentTable;
import com.example.daemawiki.domain.content.service.WriteContent;
import com.example.daemawiki.domain.document.component.service.CreateDocument;
import com.example.daemawiki.domain.document.component.service.DeleteDocument;
import com.example.daemawiki.domain.document.component.service.GetDocument;
Expand All @@ -25,13 +29,17 @@ public class DocumentController {
private final DeleteDocument deleteDocumentService;
private final UpdateDocument updateDocumentService;
private final UpdateInfo updateInfoService;
private final WriteContent writeContentService;
private final AddContentTable addContentTableService;

public DocumentController(CreateDocument createDocument, GetDocument getDocument, DeleteDocument deleteDocument, UpdateDocument updateDocument, UpdateInfo updateInfoService) {
public DocumentController(CreateDocument createDocument, GetDocument getDocument, DeleteDocument deleteDocument, UpdateDocument updateDocument, UpdateInfo updateInfoService, WriteContent writeContentService, AddContentTable addContentTableService) {
this.createDocumentService = createDocument;
this.getDocumentService = getDocument;
this.deleteDocumentService = deleteDocument;
this.updateDocumentService = updateDocument;
this.updateInfoService = updateInfoService;
this.writeContentService = writeContentService;
this.addContentTableService = addContentTableService;
}

@PostMapping
Expand Down Expand Up @@ -78,4 +86,16 @@ public Mono<Void> updateInfo(@RequestBody UpdateInfoRequest request) {
return updateInfoService.execute(request);
}

@PatchMapping("/content")
@ResponseStatus(HttpStatus.NO_CONTENT)
public Mono<Void> writeContent(@RequestBody WriteContentRequest request) {
return writeContentService.execute(request);
}

@PatchMapping("/content/table")
@ResponseStatus(HttpStatus.NO_CONTENT)
public Mono<Void> addContentTable(@RequestBody AddContentRequest request) {
return addContentTableService.execute(request);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.daemawiki.domain.document.component;

import com.example.daemawiki.domain.content.model.Contents;
import com.example.daemawiki.domain.document.component.facade.CreateDocumentFacade;
import com.example.daemawiki.domain.document.dto.request.SaveDocumentRequest;
import com.example.daemawiki.domain.document.model.DefaultDocument;
Expand Down Expand Up @@ -47,7 +48,11 @@ private DefaultDocument createDocument(User user) {
return createDocumentFacade.execute(SaveDocumentRequest.builder()
.title(user.getName())
.type("student")
.content("회원가입을 통해 자동 생성된 문서입니다.")
.content(Contents.builder()
.index("1")
.title("개요")
.content("회원가입을 통해 자동 생성된 문서입니다.")
.build())
.groups(groups)
.build(), user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Collections;

@Component
public class CreateDocumentFacade {
Expand Down Expand Up @@ -37,7 +38,7 @@ public DefaultDocument execute(SaveDocumentRequest request, User user) {
.createdUser(userDetail)
.updatedUser(userDetail)
.build())
.content(request.content())
.content(Collections.singletonList(request.content()))
.groups(request.groups())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ public Mono<Void> execute(SaveDocumentRequest request, String documentId) {

document.update(request.title(),
getDocumentType.execute(request.type()),
request.content(),
request.groups());

document.getContent().add(request.content());

return document;
})
.filter(document -> Objects.equals(document.getVersion(), request.version()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.daemawiki.domain.document.dto.request;

import com.example.daemawiki.domain.content.model.Contents;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
Expand All @@ -13,8 +14,8 @@ public record SaveDocumentRequest(
String title,
@NotBlank(message = "문서의 유형이 비어있습니다.")
String type,
@NotBlank(message = "문서의 내용이 비어있습니다.")
String content,
@Nullable
Contents content,
@NotNull(message = "문서의 분류가 비어있습니다.")
List<List<String>> groups,
@Nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.daemawiki.domain.document.dto.response;

import com.example.daemawiki.domain.content.model.Contents;
import com.example.daemawiki.domain.document.model.DocumentEditor;
import com.example.daemawiki.domain.document.model.type.DocumentType;
import com.example.daemawiki.domain.info.model.Info;
Expand All @@ -17,7 +18,7 @@ public record GetDocumentResponse(
List<Info> info,
List<List<String>> groups,
DocumentEditor editor,
String content,
List<Contents> content,

Integer version
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.daemawiki.domain.document.model;

import com.example.daemawiki.domain.content.model.Contents;
import com.example.daemawiki.domain.document.model.type.DocumentType;
import com.example.daemawiki.domain.info.model.Info;
import com.example.daemawiki.global.datetime.model.EditDateTime;
Expand Down Expand Up @@ -35,13 +36,13 @@ public class DefaultDocument {

private DocumentEditor editor;

private String content;
private List<Contents> content;

@Version
private Integer version;

@Builder
public DefaultDocument(String title, DocumentType type, EditDateTime dateTime, List<Info> info, List<List<String>> groups, DocumentEditor documentEditor, String content) {
public DefaultDocument(String title, DocumentType type, EditDateTime dateTime, List<Info> info, List<List<String>> groups, DocumentEditor documentEditor, List<Contents> content) {
this.title = title;
this.type = type;
this.dateTime = dateTime;
Expand All @@ -51,10 +52,9 @@ public DefaultDocument(String title, DocumentType type, EditDateTime dateTime, L
this.content = content;
}

public void update(String title, DocumentType type, String content, List<List<String>> groups) {
public void update(String title, DocumentType type, List<List<String>> groups) {
this.title = title;
this.type = type;
this.content = content;
this.groups = groups;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Mono<SearchDocumentResponse> defaultDocumentToSearchDocumentResponse(Defa
.title(document.getTitle())
.type(document.getType())
.dateTime(document.getDateTime())
.content(document.getContent().substring(0, Math.min(document.getContent().length(), 100)))
.content(document.getContent().getFirst().getContent().substring(0, Math.min(document.getContent().getFirst().getContent().length(), 100)))
.build());
}

Expand Down

0 comments on commit 5a7daaa

Please sign in to comment.