From b28ba1e99cb1ac4c3b0f6b6610eeb057e6630611 Mon Sep 17 00:00:00 2001 From: HyeJiJUN Date: Thu, 10 Oct 2024 14:31:23 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9D=BC=EA=B8=B0=20=EC=B9=B4=ED=85=8C?= =?UTF-8?q?=EA=B3=A0=EB=A6=AC,=ED=8C=8C=EC=9D=BC=EC=9D=98=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5,=EC=A1=B0=ED=9A=8C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../everymoment/dto/request/FileRequest.java | 2 +- .../repository/DiaryCategoryRepository.java | 5 + .../repository/FileRepository.java | 4 + .../everymoment/service/DiaryService.java | 128 ++++++++++++++---- .../everymoment/service/FileService.java | 2 +- .../service/FriendDiaryService.java | 55 ++++---- .../everymoment/service/FriendService.java | 15 +- 7 files changed, 157 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/potatocake/everymoment/dto/request/FileRequest.java b/src/main/java/com/potatocake/everymoment/dto/request/FileRequest.java index 9d22d27..8c2b494 100644 --- a/src/main/java/com/potatocake/everymoment/dto/request/FileRequest.java +++ b/src/main/java/com/potatocake/everymoment/dto/request/FileRequest.java @@ -4,6 +4,6 @@ @Getter public class FileRequest { - private String filename; + private String imageUrl; private int order; } diff --git a/src/main/java/com/potatocake/everymoment/repository/DiaryCategoryRepository.java b/src/main/java/com/potatocake/everymoment/repository/DiaryCategoryRepository.java index 2989173..2533727 100644 --- a/src/main/java/com/potatocake/everymoment/repository/DiaryCategoryRepository.java +++ b/src/main/java/com/potatocake/everymoment/repository/DiaryCategoryRepository.java @@ -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 { List findByCategoryId(Long categoryId); + + List findByDiary(Diary diary); + + void deleteByDiary(Diary diary); } diff --git a/src/main/java/com/potatocake/everymoment/repository/FileRepository.java b/src/main/java/com/potatocake/everymoment/repository/FileRepository.java index 04fac6d..93db866 100644 --- a/src/main/java/com/potatocake/everymoment/repository/FileRepository.java +++ b/src/main/java/com/potatocake/everymoment/repository/FileRepository.java @@ -9,6 +9,10 @@ public interface FileRepository extends JpaRepository { List findByDiaryId(Long diaryId); + List findByDiary(Diary diary); + + File findByDiaryAndOrder(Diary diary, int order); + void deleteByDiary(Diary diary); } diff --git a/src/main/java/com/potatocake/everymoment/service/DiaryService.java b/src/main/java/com/potatocake/everymoment/service/DiaryService.java index 2e3747d..847730e 100644 --- a/src/main/java/com/potatocake/everymoment/service/DiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/DiaryService.java @@ -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; @@ -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; @@ -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; @@ -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) { @@ -99,9 +110,36 @@ public void createDiaryManual(Long memberId, DiaryManualCreateRequest diaryManua Diary savedDiary = diaryRepository.save(diary); - Long diaryId = savedDiary.getId(); //카테고리 저장 + List 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 fileRequestList = diaryManualCreateRequest.getFile(); + for (FileRequest fileRequest : fileRequestList) { + File file = File.builder() + .diary(savedDiary) + .imageUrl(fileRequest.getImageUrl()) + .order(fileRequest.getOrder()) + .build(); + + fileRepository.save(file); + } } // 내 일기 전체 조회 (타임라인) @@ -118,7 +156,7 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil Specification 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())); @@ -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 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 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()); @@ -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 categoryResponseList = new ArrayList<>(); - categoryResponseList.add(categoryResponse); - - //파일 찾음 - FileResponse fileResponse = FileResponse.builder() - .id(1L) - .imageUrl("image1.url") - .order(1) - .build(); - List fileResponseList = new ArrayList<>(); - fileResponseList.add(fileResponse); + // 카테고리 찾음 + List diaryCategories = diaryCategoryRepository.findByDiary(savedDiary); + List categoryResponseList = diaryCategories.stream() + .map(diaryCategory -> CategoryResponse.builder() + .id(diaryCategory.getCategory().getId()) + .categoryName(diaryCategory.getCategory().getCategoryName()) + .build()) + .collect(Collectors.toList()); + + // 파일 찾음 + List files = fileRepository.findByDiary(savedDiary); + List 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()) @@ -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()) diff --git a/src/main/java/com/potatocake/everymoment/service/FileService.java b/src/main/java/com/potatocake/everymoment/service/FileService.java index 771129e..5ab7bb6 100644 --- a/src/main/java/com/potatocake/everymoment/service/FileService.java +++ b/src/main/java/com/potatocake/everymoment/service/FileService.java @@ -46,7 +46,7 @@ public void uploadFiles(Long diaryId, Long memberId, List files, List 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); } diff --git a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java index 58292da..9284931 100644 --- a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java @@ -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; @@ -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) { @@ -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 categoryResponseDTOList = new ArrayList<>(); - categoryResponseDTOList.add(categoryResponseDTO); - - //파일 찾음 - FileResponse fileResponse = FileResponse.builder() - .id(1L) - .imageUrl("image1.url") - .order(1) - .build(); - List fileResponseDTOList = new ArrayList<>(); - fileResponseDTOList.add(fileResponse); + + // 카테고리 찾음 + List diaryCategories = diaryCategoryRepository.findByDiary(diary); + List categoryResponseList = diaryCategories.stream() + .map(diaryCategory -> CategoryResponse.builder() + .id(diaryCategory.getCategory().getId()) + .categoryName(diaryCategory.getCategory().getCategoryName()) + .build()) + .collect(Collectors.toList()); + + // 파일 찾음 + List files = fileRepository.findByDiary(diary); + List 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()) @@ -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()) diff --git a/src/main/java/com/potatocake/everymoment/service/FriendService.java b/src/main/java/com/potatocake/everymoment/service/FriendService.java index 8238bd1..d134bae 100644 --- a/src/main/java/com/potatocake/everymoment/service/FriendService.java +++ b/src/main/java/com/potatocake/everymoment/service/FriendService.java @@ -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; @@ -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) @@ -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())