Skip to content

Commit

Permalink
feat: 일기 카테고리,파일의 저장,조회 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
HyeJiJUN committed Oct 10, 2024
1 parent c240318 commit b28ba1e
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

@Getter
public class FileRequest {
private String filename;
private String imageUrl;
private int order;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.potatocake.everymoment.repository;

import com.potatocake.everymoment.entity.Diary;
import com.potatocake.everymoment.entity.DiaryCategory;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DiaryCategoryRepository extends JpaRepository<DiaryCategory, Long> {
List<DiaryCategory> findByCategoryId(Long categoryId);

List<DiaryCategory> findByDiary(Diary diary);

void deleteByDiary(Diary diary);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public interface FileRepository extends JpaRepository<File, Long> {

List<File> findByDiaryId(Long diaryId);

List<File> findByDiary(Diary diary);

File findByDiaryAndOrder(Diary diary, int order);

void deleteByDiary(Diary diary);

}
128 changes: 103 additions & 25 deletions src/main/java/com/potatocake/everymoment/service/DiaryService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.potatocake.everymoment.service;

import com.potatocake.everymoment.dto.request.CategoryRequest;
import com.potatocake.everymoment.dto.request.DiaryAutoCreateRequest;
import com.potatocake.everymoment.dto.request.DiaryFilterRequest;
import com.potatocake.everymoment.dto.request.DiaryManualCreateRequest;
import com.potatocake.everymoment.dto.request.FileRequest;
import com.potatocake.everymoment.dto.response.CategoryResponse;
import com.potatocake.everymoment.dto.response.FileResponse;
import com.potatocake.everymoment.dto.response.MyDiariesResponse;
Expand All @@ -12,14 +14,18 @@
import com.potatocake.everymoment.dto.response.ThumbnailResponse;
import com.potatocake.everymoment.entity.Diary;
import com.potatocake.everymoment.entity.DiaryCategory;
import com.potatocake.everymoment.entity.File;
import com.potatocake.everymoment.entity.Member;
import com.potatocake.everymoment.entity.Notification;
import com.potatocake.everymoment.exception.ErrorCode;
import com.potatocake.everymoment.exception.GlobalException;
import com.potatocake.everymoment.repository.CategoryRepository;
import com.potatocake.everymoment.repository.DiaryCategoryRepository;
import com.potatocake.everymoment.repository.DiaryRepository;
import com.potatocake.everymoment.repository.FileRepository;
import com.potatocake.everymoment.repository.MemberRepository;
import com.potatocake.everymoment.repository.NotificationRepository;
import com.potatocake.everymoment.security.MemberDetails;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand All @@ -28,6 +34,9 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -40,6 +49,8 @@ public class DiaryService {
private final DiaryCategoryRepository diaryCategoryRepository;
private final NotificationRepository notificationRepository;
private final MemberRepository memberRepository;
private final CategoryRepository categoryRepository;
private final FileRepository fileRepository;

// 자동 일기 저장 (LocationPoint, Name, Adress 만 저장)
public NotificationResponse createDiaryAuto(Long memberId, DiaryAutoCreateRequest diaryAutoCreateRequest) {
Expand Down Expand Up @@ -99,9 +110,36 @@ public void createDiaryManual(Long memberId, DiaryManualCreateRequest diaryManua

Diary savedDiary = diaryRepository.save(diary);

Long diaryId = savedDiary.getId();
//카테고리 저장
List<CategoryRequest> categoryRequestList = diaryManualCreateRequest.getCategories();
for (CategoryRequest categoryRequest : categoryRequestList) {
Long categoryId = categoryRequest.getCategoryId();

DiaryCategory diaryCategory = DiaryCategory.builder()
.diary(savedDiary)
.category(categoryRepository.findById(categoryId)
.map(category -> {
// Category가 현재 사용자의 소유인지 확인
category.checkOwner(currentMember.getId());
return category;
})
.orElseThrow(() -> new GlobalException(ErrorCode.CATEGORY_NOT_FOUND)))
.build();

diaryCategoryRepository.save(diaryCategory);

}
//파일 저장
List<FileRequest> fileRequestList = diaryManualCreateRequest.getFile();
for (FileRequest fileRequest : fileRequestList) {
File file = File.builder()
.diary(savedDiary)
.imageUrl(fileRequest.getImageUrl())
.order(fileRequest.getOrder())
.build();

fileRepository.save(file);
}
}

// 내 일기 전체 조회 (타임라인)
Expand All @@ -118,7 +156,7 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil
Specification<Diary> spec = DiarySpecification.filterDiaries(diaryFilterRequest.getKeyword(),
diaryFilterRequest.getEmoji(), diaryFilterRequest.getDate(), diaryFilterRequest.getFrom(),
diaryFilterRequest.getUntil(), diaryFilterRequest.getBookmark())
.and((root, query, builder) -> builder.equal(root.get("member"), currentMember));
.and((root, query, builder) -> builder.equal(root.get("memberId"), currentMember));

diaryPage = diaryRepository.findAll(spec,
PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize()));
Expand Down Expand Up @@ -163,8 +201,43 @@ public MyDiaryResponse getMyDiary(Long memberId, Long diaryId) {
public void updateDiary(Long memberId, Long diaryId, DiaryManualCreateRequest diaryManualCreateRequest) {
Diary existingDiary = getExistDiary(memberId, diaryId);

//카테고리 업데이트
//파일 업데이트
// 카테고리 업데이트
List<CategoryRequest> categoryRequestList = diaryManualCreateRequest.getCategories();
if (categoryRequestList != null && !categoryRequestList.isEmpty()) {
diaryCategoryRepository.deleteByDiary(existingDiary);

for (CategoryRequest categoryRequest : categoryRequestList) {
Long categoryId = categoryRequest.getCategoryId();

DiaryCategory diaryCategory = DiaryCategory.builder()
.diary(existingDiary)
.category(categoryRepository.findById(categoryId)
.map(category -> {
category.checkOwner(memberId);
return category;
})
.orElseThrow(() -> new GlobalException(ErrorCode.CATEGORY_NOT_FOUND)))
.build();

diaryCategoryRepository.save(diaryCategory);
}
}

// 파일 업데이트
List<FileRequest> fileRequestList = diaryManualCreateRequest.getFile();
if (fileRequestList != null && !fileRequestList.isEmpty()) {
fileRepository.deleteByDiary(existingDiary);

for (FileRequest fileRequest : fileRequestList) {
File file = File.builder()
.diary(existingDiary)
.imageUrl(fileRequest.getImageUrl())
.order(fileRequest.getOrder())
.build();

fileRepository.save(file);
}
}

//다이어리 업데이트
existingDiary.updateContent(diaryManualCreateRequest.getContent());
Expand Down Expand Up @@ -212,22 +285,24 @@ private Diary getExistDiary(Long memberId, Long diaryId) {

//상세 조회시 일기DTO 변환
private MyDiaryResponse convertToMyDiaryResponseDto(Diary savedDiary) {
//카테고리 찾음
CategoryResponse categoryResponse = CategoryResponse.builder()
.id(1L)
.categoryName("일상")
.build();
List<CategoryResponse> categoryResponseList = new ArrayList<>();
categoryResponseList.add(categoryResponse);

//파일 찾음
FileResponse fileResponse = FileResponse.builder()
.id(1L)
.imageUrl("image1.url")
.order(1)
.build();
List<FileResponse> fileResponseList = new ArrayList<>();
fileResponseList.add(fileResponse);
// 카테고리 찾음
List<DiaryCategory> diaryCategories = diaryCategoryRepository.findByDiary(savedDiary);
List<CategoryResponse> categoryResponseList = diaryCategories.stream()
.map(diaryCategory -> CategoryResponse.builder()
.id(diaryCategory.getCategory().getId())
.categoryName(diaryCategory.getCategory().getCategoryName())
.build())
.collect(Collectors.toList());

// 파일 찾음
List<File> files = fileRepository.findByDiary(savedDiary);
List<FileResponse> fileResponseList = files.stream()
.map(file -> FileResponse.builder()
.id(file.getId())
.imageUrl(file.getImageUrl())
.order(file.getOrder())
.build())
.collect(Collectors.toList());

return MyDiaryResponse.builder()
.id(savedDiary.getId())
Expand All @@ -244,11 +319,14 @@ private MyDiaryResponse convertToMyDiaryResponseDto(Diary savedDiary) {

//일기 전체 불러올 때, 일기DTO 변환
private MyDiarySimpleResponse convertToMyDiarySimpleResponseDto(Diary savedDiary) {
//파일 찾음
ThumbnailResponse thumbnailResponse = ThumbnailResponse.builder()
.id(1L)
.imageUrl("image1.url")
.build();
File thumbnailFile = fileRepository.findByDiaryAndOrder(savedDiary, 1);
ThumbnailResponse thumbnailResponse = null;
if (thumbnailFile != null) {
thumbnailResponse = ThumbnailResponse.builder()
.id(thumbnailFile.getId())
.imageUrl(thumbnailFile.getImageUrl())
.build();
}

return MyDiarySimpleResponse.builder()
.id(savedDiary.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void uploadFiles(Long diaryId, Long memberId, List<MultipartFile> files,
List<File> fileEntities = new ArrayList<>();

for (FileRequest info : infos) {
MultipartFile file = fileMap.get(info.getFilename());
MultipartFile file = fileMap.get(info.getImageUrl());
if (file == null) {
throw new GlobalException(ErrorCode.FILE_NOT_FOUND);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import com.potatocake.everymoment.dto.response.ThumbnailResponse;
import com.potatocake.everymoment.entity.Diary;
import com.potatocake.everymoment.entity.DiaryCategory;
import com.potatocake.everymoment.entity.File;
import com.potatocake.everymoment.entity.Friend;
import com.potatocake.everymoment.entity.Member;
import com.potatocake.everymoment.exception.ErrorCode;
import com.potatocake.everymoment.exception.GlobalException;
import com.potatocake.everymoment.repository.DiaryCategoryRepository;
import com.potatocake.everymoment.repository.DiaryRepository;
import com.potatocake.everymoment.repository.FileRepository;
import com.potatocake.everymoment.repository.FriendRepository;
import com.potatocake.everymoment.repository.MemberRepository;
import java.util.ArrayList;
Expand All @@ -35,6 +37,7 @@ public class FriendDiaryService {
private final DiaryCategoryRepository diaryCategoryRepository;
private final FriendRepository friendRepository;
private final MemberRepository memberRepository;
private final FileRepository fileRepository;

//친구 일기 조회
public FriendDiariesResponse getFriendDiaries(Long memberId, DiaryFilterRequest diaryFilterRequest) {
Expand Down Expand Up @@ -106,32 +109,35 @@ public FriendDiaryResponse getFriendDiary(Long memberId, Long diaryId) {
if (!friendIdList.contains(diary.getMember())) {
throw new GlobalException(ErrorCode.FRIEND_NOT_FOUND);
}
//카테고리 찾음
CategoryResponse categoryResponseDTO = CategoryResponse.builder()
.id(1L)
.categoryName("일상")
.build();
List<CategoryResponse> categoryResponseDTOList = new ArrayList<>();
categoryResponseDTOList.add(categoryResponseDTO);

//파일 찾음
FileResponse fileResponse = FileResponse.builder()
.id(1L)
.imageUrl("image1.url")
.order(1)
.build();
List<FileResponse> fileResponseDTOList = new ArrayList<>();
fileResponseDTOList.add(fileResponse);

// 카테고리 찾음
List<DiaryCategory> diaryCategories = diaryCategoryRepository.findByDiary(diary);
List<CategoryResponse> categoryResponseList = diaryCategories.stream()
.map(diaryCategory -> CategoryResponse.builder()
.id(diaryCategory.getCategory().getId())
.categoryName(diaryCategory.getCategory().getCategoryName())
.build())
.collect(Collectors.toList());

// 파일 찾음
List<File> files = fileRepository.findByDiary(diary);
List<FileResponse> fileResponseList = files.stream()
.map(file -> FileResponse.builder()
.id(file.getId())
.imageUrl(file.getImageUrl())
.order(file.getOrder())
.build())
.collect(Collectors.toList());

//like 갯수 반환
Integer likeCount = 11;

FriendDiaryResponse diaryResponseDTO = FriendDiaryResponse.builder()
.id(diary.getId())
.categories(categoryResponseDTOList)
.categories(categoryResponseList)
.locationName(diary.getLocationName())
.emoji(diary.getEmoji())
.file(fileResponseDTOList)
.file(fileResponseList)
.content(diary.getContent())
.likeCount(likeCount)
.createAt(diary.getCreateAt())
Expand All @@ -142,11 +148,14 @@ public FriendDiaryResponse getFriendDiary(Long memberId, Long diaryId) {

//친구 일기 DTO변환
private FriendDiarySimpleResponse convertToFriendDiariesResponseDTO(Diary savedDiary) {
//파일 찾음
ThumbnailResponse thumbnailResponse = ThumbnailResponse.builder()
.id(1L)
.imageUrl("image1.url")
.build();
File thumbnailFile = fileRepository.findByDiaryAndOrder(savedDiary, 1);
ThumbnailResponse thumbnailResponse = null;
if (thumbnailFile != null) {
thumbnailResponse = ThumbnailResponse.builder()
.id(thumbnailFile.getId())
.imageUrl(thumbnailFile.getImageUrl())
.build();
}

return FriendDiarySimpleResponse.builder()
.id(savedDiary.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import com.potatocake.everymoment.dto.response.OneFriendDiariesResponse;
import com.potatocake.everymoment.dto.response.ThumbnailResponse;
import com.potatocake.everymoment.entity.Diary;
import com.potatocake.everymoment.entity.File;
import com.potatocake.everymoment.entity.Friend;
import com.potatocake.everymoment.entity.Member;
import com.potatocake.everymoment.exception.ErrorCode;
import com.potatocake.everymoment.exception.GlobalException;
import com.potatocake.everymoment.repository.DiaryRepository;
import com.potatocake.everymoment.repository.FileRepository;
import com.potatocake.everymoment.repository.FriendRepository;
import com.potatocake.everymoment.repository.MemberRepository;
import java.time.LocalDate;
Expand All @@ -32,6 +34,7 @@ public class FriendService {
private final FriendRepository friendRepository;
private final MemberRepository memberRepository;
private final DiaryRepository diaryRepository;
private final FileRepository fileRepository;

//특정 친구 일기 조회
@Transactional(readOnly = true)
Expand Down Expand Up @@ -124,10 +127,14 @@ public void toggleCloseFriend(Long memberId, Long friendId) {
//다이어리 DTO 변환
private FriendDiarySimpleResponse convertToFriendDiariesResponseDTO(Diary savedDiary) {
//파일 찾음
ThumbnailResponse thumbnailResponse = ThumbnailResponse.builder()
.id(1L)
.imageUrl("image1.url")
.build();
File thumbnailFile = fileRepository.findByDiaryAndOrder(savedDiary, 1);
ThumbnailResponse thumbnailResponse = null;
if (thumbnailFile != null) {
thumbnailResponse = ThumbnailResponse.builder()
.id(thumbnailFile.getId())
.imageUrl(thumbnailFile.getImageUrl())
.build();
}

return FriendDiarySimpleResponse.builder()
.id(savedDiary.getId())
Expand Down

0 comments on commit b28ba1e

Please sign in to comment.