Skip to content

Commit

Permalink
Refactor: 문서 타입으로 조회시 프로필 전달 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobhboy committed Mar 12, 2024
1 parent 3d17233 commit b93296c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ResponseEntity<DocsTypeDto> findAllByDocsType(
throw DocsTypeNotFoundException.EXCEPTION;
}

return ResponseEntity.ok(DocsTypeDto.from(docsInformationService.findByEnroll(docsType)));
return ResponseEntity.ok(DocsTypeDto.from(docsInformationService.findByDocsTypeOrderByEnroll(docsType)));
}

@GetMapping("/find/all/title/{title}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
package com.project.bumawiki.domain.docs.presentation.dto.response;

import java.time.LocalDateTime;
import java.util.List;

import com.project.bumawiki.domain.docs.domain.Docs;
import com.project.bumawiki.domain.docs.domain.VersionDocs;
import com.project.bumawiki.domain.docs.domain.type.DocsType;
import lombok.Getter;
import com.project.bumawiki.domain.docs.service.DocsUtil;

import java.time.LocalDateTime;
import java.util.List;
import lombok.Getter;

@Getter
public class DocsNameAndEnrollResponseDto {

private Long id;
private String title;
private int enroll;
private String simpleContents;
private DocsType docsType;
private LocalDateTime lastModifiedAt;

public DocsNameAndEnrollResponseDto(Docs docs) {
this.id = docs.getId();
this.title = docs.getTitle();
this.enroll = docs.getEnroll();
this.docsType = docs.getDocsType();
this.lastModifiedAt = docs.getLastModifiedAt();
this.simpleContents = getSimpleContents(docs);
}

public String getSimpleContents(Docs docs) {
List<VersionDocs> docsVersion = docs.getDocsVersion();
int currentDocsSize = docsVersion.size() - 1;
String contents = docsVersion.get(currentDocsSize).getContents();
int endIndex = Math.min(contents.length(), 200);
return contents.substring(0, endIndex);
}
private Long id;
private String title;
private int enroll;
private String simpleContents;
private DocsType docsType;
private LocalDateTime lastModifiedAt;
private String thumbnail;

public DocsNameAndEnrollResponseDto(Docs docs) {
this.id = docs.getId();
this.title = docs.getTitle();
this.enroll = docs.getEnroll();
this.docsType = docs.getDocsType();
this.lastModifiedAt = docs.getLastModifiedAt();
this.simpleContents = getSimpleContents(docs);
this.thumbnail = DocsUtil.getThumbnail(getContents(docs));
}

private String getSimpleContents(Docs docs) {
String contents = getContents(docs);
int endIndex = Math.min(contents.length(), 200);
return contents.substring(0, endIndex);
}

private String getContents(Docs docs) {
List<VersionDocs> docsVersion = docs.getDocsVersion();
int currentDocsSize = docsVersion.size() - 1;
return docsVersion.get(currentDocsSize).getContents();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch.Diff;
Expand All @@ -45,7 +47,7 @@ public List<DocsNameAndEnrollResponseDto> findByDocsType(final DocsType docsType
.collect(Collectors.toList());
}

public Map<Integer, List<DocsNameAndEnrollResponseDto>> findByEnroll(final DocsType docsType) {
public Map<Integer, List<DocsNameAndEnrollResponseDto>> findByDocsTypeOrderByEnroll(final DocsType docsType) {
List<Docs> allDocs = docsRepository.findByDocsType(docsType);

List<DocsNameAndEnrollResponseDto> docsList = allDocs.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.project.bumawiki.domain.docs.service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DocsUtil {
public static String getThumbnail(String contents) {
String pattern = "(?<=<사진 [^>]*>)(.*?)(?=</사진>)|(?<=<<)([^>]*)(?=>>:\\{)";

Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(contents);

if (matcher.find()) {
return matcher.group(0);
} else {
return null;
}

}
}

0 comments on commit b93296c

Please sign in to comment.