Skip to content

Commit

Permalink
feat: 아카이브 불러오기 (#56)
Browse files Browse the repository at this point in the history
* feat: 아카이브 탭에 대한 enum 생성

* feat: 아카이브 조회 api 추가

* test: 아카이브 조회 테스트 생성

* fix: 아카이브 조회시 최신순 정렬

* fix: 리뷰 코멘트 반영
  • Loading branch information
shinyubin989 authored Jan 31, 2024
1 parent 5beec47 commit 3aea445
Show file tree
Hide file tree
Showing 17 changed files with 589 additions and 18 deletions.
29 changes: 29 additions & 0 deletions src/main/java/com/baro/archive/application/ArchiveService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package com.baro.archive.application;

import com.baro.archive.application.dto.ArchiveUnitResult;
import com.baro.archive.application.dto.GetArchiveQuery;
import com.baro.archive.domain.Archive;
import com.baro.archive.domain.ArchiveRepository;
import com.baro.member.domain.Member;
import com.baro.member.domain.MemberRepository;
import com.baro.memofolder.domain.MemoFolder;
import com.baro.memofolder.domain.MemoFolderRepository;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -10,5 +20,24 @@
@Service
public class ArchiveService {

private final MemberRepository memberRepository;
private final MemoFolderRepository memoFolderRepository;
private final ArchiveRepository archiveRepository;

public List<ArchiveUnitResult> getArchive(GetArchiveQuery query) {
Member member = memberRepository.getById(query.memberId());
MemoFolder memoFolder = memoFolderRepository.getById(query.folderId());
memoFolder.matchOwner(member.getId());

Comparator<Archive> comparator = Comparator.comparing(Archive::getCreatedAt).reversed();

return switch (query.tab()) {
case ALL -> archiveRepository.findAllArchives(query.memberId(), query.folderId())
.stream().sorted(comparator).map(ArchiveUnitResult::of).collect(Collectors.toList());
case MEMO -> archiveRepository.findAllArchivedMemos(query.memberId(), query.folderId())
.stream().sorted(comparator).map(ArchiveUnitResult::of).collect(Collectors.toList());
case TEMPLATE -> archiveRepository.findAllArchivedTemplates(query.memberId(), query.folderId())
.stream().sorted(comparator).map(ArchiveUnitResult::of).collect(Collectors.toList());
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.baro.archive.application.dto;

import com.baro.archive.domain.Archive;
import com.baro.archive.domain.ArchiveTab;

public record ArchiveUnitResult(
Long archiveId,
String tabName,
String categoryName,
String content,
Integer copiedCount,
Integer savedCount
) {

public static ArchiveUnitResult of(Archive archive) {
if (archive.isMemo()) {
return memoToArchiveUnit(archive);
}
return templateToArchiveUnit(archive);
}

private static ArchiveUnitResult memoToArchiveUnit(Archive archive) {
return new ArchiveUnitResult(
archive.getId(),
ArchiveTab.MEMO.getName(),
null,
archive.getContent().value(),
null,
null
);
}

private static ArchiveUnitResult templateToArchiveUnit(Archive archive) {
return new ArchiveUnitResult(
archive.getId(),
ArchiveTab.TEMPLATE.getName(),
archive.getTemplate().getCategory().getName(),
archive.getContent().value(),
archive.getTemplate().getCopiedCount(),
archive.getTemplate().getSavedCount()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baro.archive.application.dto;

import com.baro.archive.domain.ArchiveTab;

public record GetArchiveQuery(
Long memberId,
Long folderId,
ArchiveTab tab
) {
}
24 changes: 14 additions & 10 deletions src/main/java/com/baro/archive/domain/Archive.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -47,25 +48,28 @@ public class Archive extends BaseEntity {
@JoinColumn(name = "template_id", nullable = true, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Template template;

public Archive(Member member, MemoFolder memoFolder, MemoContent content, Template template) {
public Archive(Long id, Member member, MemoFolder memoFolder, MemoContent content, Template template) {
this.id = id;
this.member = member;
this.memoFolder = memoFolder;
this.content = content;
this.template = template;
}

public Archive(Long id, Member member, MemoFolder memoFolder, MemoContent memoContent) {
this(id, member, memoFolder, memoContent, null);
}

public Archive(Member member, MemoFolder memoFolder, MemoContent content, Template template) {
this(null, member, memoFolder, content, template);
}

public Archive(Member member, MemoFolder memoFolder, MemoContent content) {
this.member = member;
this.memoFolder = memoFolder;
this.content = content;
this(null, member, memoFolder, content, null);
}

public Archive(Long id, Member member, MemoFolder memoFolder, MemoContent content, Template template) {
this.id = id;
this.member = member;
this.memoFolder = memoFolder;
this.content = content;
this.template = template;
public boolean isMemo() {
return Objects.isNull(this.template);
}

public void changeMemoFolder(MemoFolder memoFolder) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/baro/archive/domain/ArchiveRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public interface ArchiveRepository {

void delete(Archive archive);

List<Archive> findAllArchives(Long memberId, Long folderId);

List<Archive> findAllArchivedMemos(Long memberId, Long folderId);

List<Archive> findAllArchivedTemplates(Long memberId, Long folderId);

void deleteAllByMemberIdAndMemoFolderId(Long memberId, Long memoFolderId);

List<Archive> findAllByMemberIdAndMemoFolderId(Long memberId, Long memoFolderId);
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/baro/archive/domain/ArchiveTab.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baro.archive.domain;

import com.baro.archive.exception.ArchiveException;
import com.baro.archive.exception.ArchiveExceptionType;
import java.util.Arrays;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Getter
public enum ArchiveTab {
ALL("전체"),
MEMO("끄적이는"),
TEMPLATE("참고하는"),
;

private final String name;

public static ArchiveTab from(String tabName) {
return Arrays.stream(values())
.filter(tab -> tab.name().equalsIgnoreCase(tabName))
.findFirst()
.orElseThrow(() -> new ArchiveException(ArchiveExceptionType.NOT_EXIST_TAB));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum ArchiveExceptionType implements RequestExceptionType {
NOT_EXIST_ARCHIVE("AR01", "존재하지 않는 아카이브 입니다.", HttpStatus.NOT_FOUND),
ARCHIVED_TEMPLATE("AR02", "이미 저장한 템플릿입니다.", HttpStatus.BAD_REQUEST),
NOT_ARCHIVED_TEMPLATE("AR03", "저장하지 않은 템플릿입니다.", HttpStatus.BAD_REQUEST),
NOT_EXIST_TAB("AR04", "존재하지 않는 탭입니다.", HttpStatus.BAD_REQUEST),
;

private final String errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ public interface ArchiveJpaRepository extends JpaRepository<Archive, Long> {
void deleteAllByMemberIdAndMemoFolderId(Long memberId, Long memoFolderId);

List<Archive> findAllByMemberIdAndMemoFolderId(Long memberId, Long memoFolderId);

List<Archive> findAllByMemberIdAndMemoFolderIdAndTemplateIdIsNull(Long memberId, Long folderId);

List<Archive> findAllByMemberIdAndMemoFolderIdAndTemplateIdIsNotNull(Long memberId, Long folderId);
}
15 changes: 15 additions & 0 deletions src/main/java/com/baro/archive/infra/ArchiveRepositoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,19 @@ public void deleteAllByMemberIdAndMemoFolderId(Long memberId, Long memoFolderId)
public List<Archive> findAllByMemberIdAndMemoFolderId(Long memberId, Long memoFolderId) {
return archiveJpaRepository.findAllByMemberIdAndMemoFolderId(memberId, memoFolderId);
}

@Override
public List<Archive> findAllArchives(Long memberId, Long folderId) {
return archiveJpaRepository.findAllByMemberIdAndMemoFolderId(memberId, folderId);
}

@Override
public List<Archive> findAllArchivedMemos(Long memberId, Long folderId) {
return archiveJpaRepository.findAllByMemberIdAndMemoFolderIdAndTemplateIdIsNull(memberId, folderId);
}

@Override
public List<Archive> findAllArchivedTemplates(Long memberId, Long folderId) {
return archiveJpaRepository.findAllByMemberIdAndMemoFolderIdAndTemplateIdIsNotNull(memberId, folderId);
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/baro/archive/presentation/ArchiveController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package com.baro.archive.presentation;

import com.baro.archive.application.ArchiveService;
import com.baro.archive.application.dto.ArchiveUnitResult;
import com.baro.archive.application.dto.GetArchiveQuery;
import com.baro.archive.domain.ArchiveTab;
import com.baro.auth.domain.AuthMember;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
Expand All @@ -11,4 +20,12 @@
public class ArchiveController {

private final ArchiveService archiveService;

@GetMapping("/folder/{folderId}")
public ResponseEntity<List<ArchiveUnitResult>> getArchive(AuthMember member,
@PathVariable("folderId") Long folderId,
@RequestParam String tabName) {
GetArchiveQuery query = new GetArchiveQuery(member.id(), folderId, ArchiveTab.from(tabName));
return ResponseEntity.ok(archiveService.getArchive(query));
}
}
16 changes: 9 additions & 7 deletions src/main/java/com/baro/template/domain/TemplateCategory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
@Getter
public enum TemplateCategory {

ASK,
REPORT,
CELEBRATE,
THANK,
COMFORT,
REGARD,
ETC,
ASK("부탁하기"),
REPORT("보고하기"),
CELEBRATE("축하하기"),
THANK("감사 전하기"),
COMFORT("위로하기"),
REGARD("안부 전하기"),
ETC("기타"),
;

private final String name;

public static TemplateCategory getCategory(String name) {
return Arrays.stream(TemplateCategory.values())
.filter(category -> category.name().equalsIgnoreCase(name))
Expand Down
Loading

0 comments on commit 3aea445

Please sign in to comment.