From b28ba1e99cb1ac4c3b0f6b6610eeb057e6630611 Mon Sep 17 00:00:00 2001 From: HyeJiJUN Date: Thu, 10 Oct 2024 14:31:23 +0900 Subject: [PATCH 1/9] =?UTF-8?q?feat:=20=EC=9D=BC=EA=B8=B0=20=EC=B9=B4?= =?UTF-8?q?=ED=85=8C=EA=B3=A0=EB=A6=AC,=ED=8C=8C=EC=9D=BC=EC=9D=98=20?= =?UTF-8?q?=EC=A0=80=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()) From c1dec83b79cd1ef9a7bfa671d46aedf4e59c3303 Mon Sep 17 00:00:00 2001 From: HyeJiJUN Date: Thu, 10 Oct 2024 14:38:28 +0900 Subject: [PATCH 2/9] =?UTF-8?q?feat:=20=EC=9D=BC=EA=B8=B0=20=EC=9C=84?= =?UTF-8?q?=EC=B9=98=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../everymoment/controller/DiaryController.java | 14 ++++++++++++++ .../potatocake/everymoment/dto/LocationPoint.java | 2 ++ .../everymoment/service/DiaryService.java | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/main/java/com/potatocake/everymoment/controller/DiaryController.java b/src/main/java/com/potatocake/everymoment/controller/DiaryController.java index 66047a9..65a7d95 100644 --- a/src/main/java/com/potatocake/everymoment/controller/DiaryController.java +++ b/src/main/java/com/potatocake/everymoment/controller/DiaryController.java @@ -1,5 +1,6 @@ package com.potatocake.everymoment.controller; +import com.potatocake.everymoment.dto.LocationPoint; import com.potatocake.everymoment.dto.SuccessResponse; import com.potatocake.everymoment.dto.request.CommentRequest; import com.potatocake.everymoment.dto.request.DiaryAutoCreateRequest; @@ -112,6 +113,19 @@ public ResponseEntity> getMyDiary( .body(SuccessResponse.ok(response)); } + @GetMapping("/location/{diaryId}") + public ResponseEntity> getLocation( + @AuthenticationPrincipal MemberDetails memberDetails, + @PathVariable Long diaryId) { + Long memberId = memberDetails.getId(); + + LocationPoint response = diaryService.getDiaryLocation(memberId, diaryId); + + return ResponseEntity.ok() + .body(SuccessResponse.ok(response)); + } + + //일기 수정 @PatchMapping("/{diaryId}") public ResponseEntity> updateDiary( diff --git a/src/main/java/com/potatocake/everymoment/dto/LocationPoint.java b/src/main/java/com/potatocake/everymoment/dto/LocationPoint.java index 58baa90..6210f07 100644 --- a/src/main/java/com/potatocake/everymoment/dto/LocationPoint.java +++ b/src/main/java/com/potatocake/everymoment/dto/LocationPoint.java @@ -1,10 +1,12 @@ package com.potatocake.everymoment.dto; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; @AllArgsConstructor @Getter +@Builder public class LocationPoint { private double latitude; private double longitude; diff --git a/src/main/java/com/potatocake/everymoment/service/DiaryService.java b/src/main/java/com/potatocake/everymoment/service/DiaryService.java index 847730e..c6779fa 100644 --- a/src/main/java/com/potatocake/everymoment/service/DiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/DiaryService.java @@ -1,5 +1,6 @@ package com.potatocake.everymoment.service; +import com.potatocake.everymoment.dto.LocationPoint; import com.potatocake.everymoment.dto.request.CategoryRequest; import com.potatocake.everymoment.dto.request.DiaryAutoCreateRequest; import com.potatocake.everymoment.dto.request.DiaryFilterRequest; @@ -197,6 +198,17 @@ public MyDiaryResponse getMyDiary(Long memberId, Long diaryId) { return convertToMyDiaryResponseDto(diary); } + // 내 일기 위치 조회 + public LocationPoint getDiaryLocation(Long memberId, Long diaryId) { + Diary diary = getExistDiary(memberId, diaryId); + List locations = List.of(diary.getLocationPoint().split("/")); + + return LocationPoint.builder() + .latitude(Double.parseDouble(locations.get(0))) + .longitude(Double.parseDouble(locations.get(1))) + .build(); + } + // 내 일기 수정 public void updateDiary(Long memberId, Long diaryId, DiaryManualCreateRequest diaryManualCreateRequest) { Diary existingDiary = getExistDiary(memberId, diaryId); From 3aa56d3e1d604c39cc62b24ce4df966898c0c4d9 Mon Sep 17 00:00:00 2001 From: HyeJiJUN Date: Fri, 11 Oct 2024 16:19:04 +0900 Subject: [PATCH 3/9] =?UTF-8?q?feat:=20=EC=9D=BC=EA=B8=B0=20=ED=95=84?= =?UTF-8?q?=ED=84=B0=EB=A7=81=20=EC=A1=B0=EA=B1=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DiaryController.java | 10 +++-- .../dto/request/DiaryFilterRequest.java | 23 +++++++++- .../everymoment/service/DiaryService.java | 43 +++++++----------- .../service/DiarySpecification.java | 17 +++++-- .../service/FriendDiaryService.java | 45 +++++++------------ .../everymoment/service/FriendService.java | 2 +- 6 files changed, 73 insertions(+), 67 deletions(-) diff --git a/src/main/java/com/potatocake/everymoment/controller/DiaryController.java b/src/main/java/com/potatocake/everymoment/controller/DiaryController.java index 65a7d95..3d3e598 100644 --- a/src/main/java/com/potatocake/everymoment/controller/DiaryController.java +++ b/src/main/java/com/potatocake/everymoment/controller/DiaryController.java @@ -73,7 +73,7 @@ public ResponseEntity> getMyDiaries( @AuthenticationPrincipal MemberDetails memberDetails, @RequestParam(required = false) String keyword, @RequestParam(required = false) String emoji, - @RequestParam(required = false) Long category, + @RequestParam(required = false) String category, @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date, @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate from, @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate until, @@ -82,6 +82,7 @@ public ResponseEntity> getMyDiaries( @RequestParam(defaultValue = "10") int size ) { Long memberId = memberDetails.getId(); + DiaryFilterRequest diaryFilterRequest = DiaryFilterRequest.builder() .keyword(keyword) .emoji(emoji) @@ -89,7 +90,7 @@ public ResponseEntity> getMyDiaries( .date(date) .from(from) .until(until) - .bookmark(bookmark) + .isBookmark(bookmark) .key(key) .size(size) .build(); @@ -185,7 +186,7 @@ public ResponseEntity> getFriendDiaries( @AuthenticationPrincipal MemberDetails memberDetails, @RequestParam(required = false) String keyword, @RequestParam(required = false) String emoji, - @RequestParam(required = false) Long category, + @RequestParam(required = false) String category, @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date, @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate from, @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate until, @@ -194,6 +195,7 @@ public ResponseEntity> getFriendDiaries( @RequestParam(defaultValue = "10") int size ) { Long memberId = memberDetails.getId(); + DiaryFilterRequest diaryFilterRequest = DiaryFilterRequest.builder() .keyword(keyword) .emoji(emoji) @@ -201,7 +203,7 @@ public ResponseEntity> getFriendDiaries( .date(date) .from(from) .until(until) - .bookmark(bookmark) + .isBookmark(bookmark) .key(key) .size(size) .build(); diff --git a/src/main/java/com/potatocake/everymoment/dto/request/DiaryFilterRequest.java b/src/main/java/com/potatocake/everymoment/dto/request/DiaryFilterRequest.java index 7675f25..720a07e 100644 --- a/src/main/java/com/potatocake/everymoment/dto/request/DiaryFilterRequest.java +++ b/src/main/java/com/potatocake/everymoment/dto/request/DiaryFilterRequest.java @@ -1,6 +1,10 @@ package com.potatocake.everymoment.dto.request; import java.time.LocalDate; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; import lombok.Builder; import lombok.Getter; @@ -9,11 +13,26 @@ public class DiaryFilterRequest { private String keyword; private String emoji; - private Long category; + private String category; private LocalDate date; private LocalDate from; private LocalDate until; - private Boolean bookmark; + private Boolean isBookmark; private int key; private int size; + + public List getEmojis() { + return (emoji != null && !emoji.isEmpty()) + ? Arrays.asList(emoji.split(",")) + : Collections.emptyList(); + } + + public List getCategories() { + return (category != null && !category.isEmpty()) + ? Arrays.stream(category.split(",")) + .map(Long::parseLong) + .collect(Collectors.toList()) + : Collections.emptyList(); + } + } diff --git a/src/main/java/com/potatocake/everymoment/service/DiaryService.java b/src/main/java/com/potatocake/everymoment/service/DiaryService.java index c6779fa..b32e6a4 100644 --- a/src/main/java/com/potatocake/everymoment/service/DiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/DiaryService.java @@ -146,38 +146,27 @@ public void createDiaryManual(Long memberId, DiaryManualCreateRequest diaryManua // 내 일기 전체 조회 (타임라인) @Transactional(readOnly = true) public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFilterRequest) { - //member 가져옴 + // member 가져옴 Member currentMember = memberRepository.findById(memberId) .orElseThrow(() -> new GlobalException(ErrorCode.MEMBER_NOT_FOUND)); Page diaryPage; - if (diaryFilterRequest.getCategory() == null) { - // category가 null인 경우 - Specification spec = DiarySpecification.filterDiaries(diaryFilterRequest.getKeyword(), - diaryFilterRequest.getEmoji(), diaryFilterRequest.getDate(), diaryFilterRequest.getFrom(), - diaryFilterRequest.getUntil(), diaryFilterRequest.getBookmark()) - .and((root, query, builder) -> builder.equal(root.get("memberId"), currentMember)); - - diaryPage = diaryRepository.findAll(spec, - PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize())); - } else { - // category가 있는 경우 - DiaryCategory에서 category 같은 것 찾음 - List diaryCategoryList = diaryCategoryRepository.findByCategoryId( - diaryFilterRequest.getCategory()); - - // Diary 중에 memberId같은 것 가져옴 - List DiaryIdList = diaryCategoryList.stream() - .filter(diaryCategory -> diaryCategory.getDiary().getMember() - .equals(currentMember)) // memberId가 일치하는 경우 필터링 - .map(diaryCategory -> diaryCategory.getDiary().getId()) - .collect(Collectors.toList()); - - // 가져온 DiaryId로 일기 찾음 - Specification spec = (root, query, builder) -> root.get("id").in(DiaryIdList); - diaryPage = diaryRepository.findAll(spec, - PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize())); - } + List categoryIds = diaryFilterRequest.getCategories(); + List emojis = diaryFilterRequest.getEmojis(); + + Specification spec = DiarySpecification.filterDiaries( + diaryFilterRequest.getKeyword(), + emojis, + categoryIds, + diaryFilterRequest.getDate(), + diaryFilterRequest.getFrom(), + diaryFilterRequest.getUntil(), + diaryFilterRequest.getIsBookmark()) + .and((root, query, builder) -> builder.equal(root.get("member"), currentMember)); + + diaryPage = diaryRepository.findAll(spec, PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize())); + List diaryDTOs = diaryPage.getContent().stream() .map(this::convertToMyDiarySimpleResponseDto) diff --git a/src/main/java/com/potatocake/everymoment/service/DiarySpecification.java b/src/main/java/com/potatocake/everymoment/service/DiarySpecification.java index 7554307..3a9826d 100644 --- a/src/main/java/com/potatocake/everymoment/service/DiarySpecification.java +++ b/src/main/java/com/potatocake/everymoment/service/DiarySpecification.java @@ -2,28 +2,37 @@ import com.potatocake.everymoment.entity.Diary; +import com.potatocake.everymoment.entity.DiaryCategory; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Join; import jakarta.persistence.criteria.Predicate; import jakarta.persistence.criteria.Root; +import java.util.List; import org.springframework.data.jpa.domain.Specification; import java.time.LocalDate; public class DiarySpecification { - public static Specification filterDiaries(String keyword, String emoji, LocalDate date, LocalDate from, LocalDate until, Boolean isBookmark) { + public static Specification filterDiaries( + String keyword, List emojis, List categories, + LocalDate date, LocalDate from, LocalDate until, Boolean isBookmark) { return (Root root, CriteriaQuery query, CriteriaBuilder builder) -> { Predicate predicate = builder.conjunction(); if (keyword != null) { predicate = builder.and(predicate, builder.like(root.get("content"), "%" + keyword + "%")); } - if (emoji != null) { - predicate = builder.and(predicate, builder.equal(root.get("emoji"), emoji)); + if (emojis != null && !emojis.isEmpty()) { + predicate = builder.and(predicate, root.get("emoji").in(emojis)); + } + + if (categories != null && !categories.isEmpty()) { + Join diaryCategoryJoin = root.join("diaryCategories"); + predicate = builder.and(predicate, diaryCategoryJoin.get("category").get("id").in(categories)); } - // 날짜 필터링 (null일 경우 오늘 날짜로 기본값 설정) LocalDate filterDate = (date != null) ? date : LocalDate.now(); predicate = builder.and(predicate, builder.equal(root.get("createAt").as(LocalDate.class), filterDate)); diff --git a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java index 9284931..2df6e97 100644 --- a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java @@ -51,32 +51,21 @@ public FriendDiariesResponse getFriendDiaries(Long memberId, DiaryFilterRequest Page diaryPage; - if (diaryFilterRequest.getCategory() == null) { - // category가 null인 경우 - Specification spec = DiarySpecification.filterDiaries(diaryFilterRequest.getKeyword(), - diaryFilterRequest.getEmoji(), diaryFilterRequest.getDate(), diaryFilterRequest.getFrom(), - diaryFilterRequest.getUntil(), diaryFilterRequest.getBookmark()) - .and((root, query, builder) -> root.get("member").in(friendIdList)); // memberIds 목록에서 검색 - - diaryPage = diaryRepository.findAll(spec, - PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize())); - } else { - // category가 있는 경우 - DiaryCategory에서 category 같은 것 찾음 - List diaryCategories = diaryCategoryRepository.findByCategoryId( - diaryFilterRequest.getCategory()); - - // Diary중에 memberId같은 것 가져옴 - List filteredDiaryIds = diaryCategories.stream() - .filter(diaryCategory -> friendIdList.contains( - diaryCategory.getDiary().getMember())) // memberIds 목록에서 필터링 - .map(diaryCategory -> diaryCategory.getDiary().getId()) - .collect(Collectors.toList()); - - // 가져온 diaryId로 일기 찾음 - Specification spec = (root, query, builder) -> root.get("id").in(filteredDiaryIds); - diaryPage = diaryRepository.findAll(spec, - PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize())); - } + // 카테고리와 이모지가 여러 개 전달될 수 있으므로 이를 리스트로 변환 + List categoryIds = diaryFilterRequest.getCategories(); + List emojis = diaryFilterRequest.getEmojis(); + + Specification spec = DiarySpecification.filterDiaries( + diaryFilterRequest.getKeyword(), + emojis, + categoryIds, + diaryFilterRequest.getDate(), + diaryFilterRequest.getFrom(), + diaryFilterRequest.getUntil(), + diaryFilterRequest.getIsBookmark()) + .and((root, query, builder) -> root.get("member").in(friendIdList)); + + diaryPage = diaryRepository.findAll(spec, PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize())); List friendDiarySimpleResponseList = diaryPage.getContent().stream() .map(this::convertToFriendDiariesResponseDTO) @@ -84,12 +73,10 @@ public FriendDiariesResponse getFriendDiaries(Long memberId, DiaryFilterRequest Integer nextPage = diaryPage.hasNext() ? diaryFilterRequest.getKey() + 1 : null; - FriendDiariesResponse friendDiariesResponse = FriendDiariesResponse.builder() + return FriendDiariesResponse.builder() .diaries(friendDiarySimpleResponseList) .next(nextPage) .build(); - - return friendDiariesResponse; } // 친구 다이어리 하나 조회 diff --git a/src/main/java/com/potatocake/everymoment/service/FriendService.java b/src/main/java/com/potatocake/everymoment/service/FriendService.java index d134bae..fe2e327 100644 --- a/src/main/java/com/potatocake/everymoment/service/FriendService.java +++ b/src/main/java/com/potatocake/everymoment/service/FriendService.java @@ -52,7 +52,7 @@ public OneFriendDiariesResponse OneFriendDiariesResponse(Long memberid, Long fri Pageable pageable = PageRequest.of(key, size); Page diaries = diaryRepository.findAll( - DiarySpecification.filterDiaries(null, null, date, null, null, null) + DiarySpecification.filterDiaries(null, null, null, date, null, null, null) .and((root, query, builder) -> builder.equal(root.get("member").get("id"), friendId)), pageable); From 6e9832c0693925c5691dcd2cea7c12f73186a171 Mon Sep 17 00:00:00 2001 From: HyeJiJUN Date: Tue, 15 Oct 2024 23:53:50 +0900 Subject: [PATCH 4/9] =?UTF-8?q?chore:=20GIS=EA=B4=80=EB=A0=A8=20=EC=9D=98?= =?UTF-8?q?=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index 2d19b2c..8b03ef5 100644 --- a/build.gradle +++ b/build.gradle @@ -38,6 +38,7 @@ dependencies { compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' + implementation 'org.hibernate:hibernate-spatial:6.6.1.Final' runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' From fca491f1954989d3cd0286b0b2557bb9049796b9 Mon Sep 17 00:00:00 2001 From: HyeJiJUN Date: Tue, 15 Oct 2024 23:55:16 +0900 Subject: [PATCH 5/9] =?UTF-8?q?refactor:=20=EC=9C=84=EA=B2=BD=EB=8F=84=20P?= =?UTF-8?q?oint=ED=83=80=EC=9E=85=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../everymoment/config/GeometryConfig.java | 14 ++++++++ .../controller/DiaryController.java | 9 ++++- .../everymoment/dto/LocationPoint.java | 5 --- .../potatocake/everymoment/entity/Diary.java | 9 ++--- .../everymoment/service/DiaryService.java | 36 +++++++++++++++---- 5 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/potatocake/everymoment/config/GeometryConfig.java diff --git a/src/main/java/com/potatocake/everymoment/config/GeometryConfig.java b/src/main/java/com/potatocake/everymoment/config/GeometryConfig.java new file mode 100644 index 0000000..d9b6d39 --- /dev/null +++ b/src/main/java/com/potatocake/everymoment/config/GeometryConfig.java @@ -0,0 +1,14 @@ +package com.potatocake.everymoment.config; + +import org.locationtech.jts.geom.GeometryFactory; +import org.locationtech.jts.geom.PrecisionModel; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class GeometryConfig { + @Bean + public GeometryFactory geometryFactory() { + return new GeometryFactory(new PrecisionModel(), 4326); + } +} diff --git a/src/main/java/com/potatocake/everymoment/controller/DiaryController.java b/src/main/java/com/potatocake/everymoment/controller/DiaryController.java index 3d3e598..bf0f7d2 100644 --- a/src/main/java/com/potatocake/everymoment/controller/DiaryController.java +++ b/src/main/java/com/potatocake/everymoment/controller/DiaryController.java @@ -17,6 +17,9 @@ import com.potatocake.everymoment.service.CommentService; import com.potatocake.everymoment.service.DiaryService; import com.potatocake.everymoment.service.FriendDiaryService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.responses.ApiResponse; import java.time.LocalDate; import lombok.RequiredArgsConstructor; import org.springframework.format.annotation.DateTimeFormat; @@ -114,9 +117,13 @@ public ResponseEntity> getMyDiary( .body(SuccessResponse.ok(response)); } - @GetMapping("/location/{diaryId}") + @Operation(summary = "특정 일기 위경도 조회", description = "특정 일기에 있는 위경도를 조회합니다.") + @ApiResponse(responseCode = "200", description = "위경도 조회 성공") + @GetMapping("/{diaryId}/location") public ResponseEntity> getLocation( + @Parameter(description = "인증된 사용자 정보", hidden = true) @AuthenticationPrincipal MemberDetails memberDetails, + @Parameter(description = "조회할 일기 ID", required = true) @PathVariable Long diaryId) { Long memberId = memberDetails.getId(); diff --git a/src/main/java/com/potatocake/everymoment/dto/LocationPoint.java b/src/main/java/com/potatocake/everymoment/dto/LocationPoint.java index 6210f07..a9ab297 100644 --- a/src/main/java/com/potatocake/everymoment/dto/LocationPoint.java +++ b/src/main/java/com/potatocake/everymoment/dto/LocationPoint.java @@ -10,9 +10,4 @@ public class LocationPoint { private double latitude; private double longitude; - - @Override - public String toString() { - return latitude + "/" + longitude; - } } diff --git a/src/main/java/com/potatocake/everymoment/entity/Diary.java b/src/main/java/com/potatocake/everymoment/entity/Diary.java index a8abca1..50dc3fa 100644 --- a/src/main/java/com/potatocake/everymoment/entity/Diary.java +++ b/src/main/java/com/potatocake/everymoment/entity/Diary.java @@ -1,5 +1,6 @@ package com.potatocake.everymoment.entity; +import com.potatocake.everymoment.dto.LocationPoint; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -14,6 +15,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.locationtech.jts.geom.Point; @Entity @NoArgsConstructor(access = AccessLevel.PROTECTED) @@ -32,9 +34,8 @@ public class Diary extends BaseTimeEntity { @Lob private String content; - //point는 mysql연결 뒤에 - @Column(length = 250, nullable = false) - private String locationPoint; // Java에서는 문자열로 처리 + @Column(nullable = false) + private Point locationPoint; @Column(length = 50, nullable = false) private String locationName; @@ -59,7 +60,7 @@ public void updateContent(String content) { } } - public void updateLocationPoint(String locationPoint) { + public void updateLocationPoint(Point locationPoint) { if (locationPoint != null) { this.locationPoint = locationPoint; } diff --git a/src/main/java/com/potatocake/everymoment/service/DiaryService.java b/src/main/java/com/potatocake/everymoment/service/DiaryService.java index b32e6a4..b44f006 100644 --- a/src/main/java/com/potatocake/everymoment/service/DiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/DiaryService.java @@ -32,6 +32,8 @@ import java.util.Objects; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; +import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.GeometryFactory; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.domain.Specification; @@ -40,6 +42,7 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.locationtech.jts.geom.Point; @RequiredArgsConstructor @Transactional @@ -52,6 +55,7 @@ public class DiaryService { private final MemberRepository memberRepository; private final CategoryRepository categoryRepository; private final FileRepository fileRepository; + private final GeometryFactory geometryFactory; // 자동 일기 저장 (LocationPoint, Name, Adress 만 저장) public NotificationResponse createDiaryAuto(Long memberId, DiaryAutoCreateRequest diaryAutoCreateRequest) { @@ -59,9 +63,14 @@ public NotificationResponse createDiaryAuto(Long memberId, DiaryAutoCreateReques Member currentMember = memberRepository.findById(memberId) .orElseThrow(() -> new GlobalException(ErrorCode.MEMBER_NOT_FOUND)); + double longitude = diaryAutoCreateRequest.getLocationPoint().getLongitude(); + double latitude = diaryAutoCreateRequest.getLocationPoint().getLatitude(); + + Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude)); + Diary diary = Diary.builder() .member(currentMember) - .locationPoint(diaryAutoCreateRequest.getLocationPoint().toString()) + .locationPoint(point) .locationName(diaryAutoCreateRequest.getLocationName()) .address(diaryAutoCreateRequest.getAddress()) .build(); @@ -98,10 +107,16 @@ public void createDiaryManual(Long memberId, DiaryManualCreateRequest diaryManua Member currentMember = memberRepository.findById(memberId) .orElseThrow(() -> new GlobalException(ErrorCode.MEMBER_NOT_FOUND)); + + double longitude = diaryManualCreateRequest.getLocationPoint().getLongitude(); + double latitude = diaryManualCreateRequest.getLocationPoint().getLatitude(); + + Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude)); + Diary diary = Diary.builder() .member(currentMember) .content(diaryManualCreateRequest.getContent()) - .locationPoint(diaryManualCreateRequest.getLocationPoint().toString()) + .locationPoint(point) .locationName(diaryManualCreateRequest.getLocationName()) .address(diaryManualCreateRequest.getAddress()) .emoji(diaryManualCreateRequest.getEmoji()) @@ -190,11 +205,11 @@ public MyDiaryResponse getMyDiary(Long memberId, Long diaryId) { // 내 일기 위치 조회 public LocationPoint getDiaryLocation(Long memberId, Long diaryId) { Diary diary = getExistDiary(memberId, diaryId); - List locations = List.of(diary.getLocationPoint().split("/")); + Point point = diary.getLocationPoint(); return LocationPoint.builder() - .latitude(Double.parseDouble(locations.get(0))) - .longitude(Double.parseDouble(locations.get(1))) + .latitude(point.getX()) + .longitude(point.getY()) .build(); } @@ -240,10 +255,17 @@ public void updateDiary(Long memberId, Long diaryId, DiaryManualCreateRequest di } } + if (diaryManualCreateRequest.getLocationPoint() != null) { + double longitude = diaryManualCreateRequest.getLocationPoint().getLongitude(); + double latitude = diaryManualCreateRequest.getLocationPoint().getLatitude(); + + Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude)); + + existingDiary.updateLocationPoint(point); + } + //다이어리 업데이트 existingDiary.updateContent(diaryManualCreateRequest.getContent()); - existingDiary.updateLocationPoint(diaryManualCreateRequest.getLocationPoint() != null - ? diaryManualCreateRequest.getLocationPoint().toString() : null); existingDiary.updateLocationName(diaryManualCreateRequest.getLocationName()); existingDiary.updateAddress(diaryManualCreateRequest.getAddress()); existingDiary.updateEmoji(diaryManualCreateRequest.getEmoji()); From 51b64fafce2c9eb34a72a8dbd7b06686be768c78 Mon Sep 17 00:00:00 2001 From: HyeJiJUN Date: Wed, 16 Oct 2024 00:00:55 +0900 Subject: [PATCH 6/9] =?UTF-8?q?refactor:=20like=EB=8D=94=EB=AF=B8=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=EC=97=90=EC=84=9C=20=EC=8B=A4?= =?UTF-8?q?=EC=A0=9C=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../everymoment/dto/response/FriendDiaryResponse.java | 2 +- .../everymoment/service/FriendDiaryService.java | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/potatocake/everymoment/dto/response/FriendDiaryResponse.java b/src/main/java/com/potatocake/everymoment/dto/response/FriendDiaryResponse.java index b442af3..6372ed5 100644 --- a/src/main/java/com/potatocake/everymoment/dto/response/FriendDiaryResponse.java +++ b/src/main/java/com/potatocake/everymoment/dto/response/FriendDiaryResponse.java @@ -18,6 +18,6 @@ public class FriendDiaryResponse { private String emoji; private List file; private String content; - private Integer likeCount; + private LikeCountResponse likeCount; private LocalDateTime createAt; } diff --git a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java index 2df6e97..0a5792f 100644 --- a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java @@ -6,6 +6,7 @@ import com.potatocake.everymoment.dto.response.FriendDiariesResponse; import com.potatocake.everymoment.dto.response.FriendDiaryResponse; import com.potatocake.everymoment.dto.response.FriendDiarySimpleResponse; +import com.potatocake.everymoment.dto.response.LikeCountResponse; import com.potatocake.everymoment.dto.response.ThumbnailResponse; import com.potatocake.everymoment.entity.Diary; import com.potatocake.everymoment.entity.DiaryCategory; @@ -18,6 +19,7 @@ import com.potatocake.everymoment.repository.DiaryRepository; import com.potatocake.everymoment.repository.FileRepository; import com.potatocake.everymoment.repository.FriendRepository; +import com.potatocake.everymoment.repository.LikeRepository; import com.potatocake.everymoment.repository.MemberRepository; import java.util.ArrayList; import java.util.List; @@ -38,6 +40,7 @@ public class FriendDiaryService { private final FriendRepository friendRepository; private final MemberRepository memberRepository; private final FileRepository fileRepository; + private final LikeRepository likeRepository; //친구 일기 조회 public FriendDiariesResponse getFriendDiaries(Long memberId, DiaryFilterRequest diaryFilterRequest) { @@ -117,7 +120,11 @@ public FriendDiaryResponse getFriendDiary(Long memberId, Long diaryId) { .collect(Collectors.toList()); //like 갯수 반환 - Integer likeCount = 11; + Long likeCount = likeRepository.countByDiary(diary); + + LikeCountResponse count = LikeCountResponse.builder() + .likeCount(likeCount) + .build(); FriendDiaryResponse diaryResponseDTO = FriendDiaryResponse.builder() .id(diary.getId()) @@ -126,7 +133,7 @@ public FriendDiaryResponse getFriendDiary(Long memberId, Long diaryId) { .emoji(diary.getEmoji()) .file(fileResponseList) .content(diary.getContent()) - .likeCount(likeCount) + .likeCount(count) .createAt(diary.getCreateAt()) .build(); From 3199f019d9473bccd6ad3c7bea0b1e5e5c0b1d76 Mon Sep 17 00:00:00 2001 From: HyeJiJUN Date: Wed, 16 Oct 2024 00:08:40 +0900 Subject: [PATCH 7/9] =?UTF-8?q?feat:=20=EB=8B=A4=EC=9D=B4=EC=96=B4?= =?UTF-8?q?=EB=A6=AC=20=EC=A0=80=EC=9E=A5,=20=EC=A1=B0=ED=9A=8C=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=ED=8C=8C=EC=9D=BC=20=EB=B6=80=EB=B6=84=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - thumbnail은 남겨둠 --- .../dto/response/FriendDiaryResponse.java | 1 - .../dto/response/MyDiaryResponse.java | 1 - .../everymoment/service/DiaryService.java | 38 ------------------- .../service/FriendDiaryService.java | 11 ------ 4 files changed, 51 deletions(-) diff --git a/src/main/java/com/potatocake/everymoment/dto/response/FriendDiaryResponse.java b/src/main/java/com/potatocake/everymoment/dto/response/FriendDiaryResponse.java index 6372ed5..47ea8da 100644 --- a/src/main/java/com/potatocake/everymoment/dto/response/FriendDiaryResponse.java +++ b/src/main/java/com/potatocake/everymoment/dto/response/FriendDiaryResponse.java @@ -16,7 +16,6 @@ public class FriendDiaryResponse { private List categories; private String locationName; private String emoji; - private List file; private String content; private LikeCountResponse likeCount; private LocalDateTime createAt; diff --git a/src/main/java/com/potatocake/everymoment/dto/response/MyDiaryResponse.java b/src/main/java/com/potatocake/everymoment/dto/response/MyDiaryResponse.java index 2b98a84..a92bc41 100644 --- a/src/main/java/com/potatocake/everymoment/dto/response/MyDiaryResponse.java +++ b/src/main/java/com/potatocake/everymoment/dto/response/MyDiaryResponse.java @@ -14,7 +14,6 @@ public class MyDiaryResponse { private String locationName; private boolean isBookmark; private String emoji; - private List file; private String content; private LocalDateTime createAt; } diff --git a/src/main/java/com/potatocake/everymoment/service/DiaryService.java b/src/main/java/com/potatocake/everymoment/service/DiaryService.java index b44f006..7b9218b 100644 --- a/src/main/java/com/potatocake/everymoment/service/DiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/DiaryService.java @@ -145,17 +145,6 @@ public void createDiaryManual(Long memberId, DiaryManualCreateRequest diaryManua 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); - } } // 내 일기 전체 조회 (타임라인) @@ -239,22 +228,6 @@ public void updateDiary(Long memberId, Long diaryId, DiaryManualCreateRequest di } } - // 파일 업데이트 - 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); - } - } - if (diaryManualCreateRequest.getLocationPoint() != null) { double longitude = diaryManualCreateRequest.getLocationPoint().getLongitude(); double latitude = diaryManualCreateRequest.getLocationPoint().getLatitude(); @@ -317,16 +290,6 @@ private MyDiaryResponse convertToMyDiaryResponseDto(Diary savedDiary) { .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()) .categories(categoryResponseList) @@ -334,7 +297,6 @@ private MyDiaryResponse convertToMyDiaryResponseDto(Diary savedDiary) { .locationName(savedDiary.getLocationName()) .isBookmark(savedDiary.isBookmark()) .emoji(savedDiary.getEmoji()) - .file(fileResponseList) .content(savedDiary.getContent()) .createAt(savedDiary.getCreateAt()) .build(); diff --git a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java index 0a5792f..bb5d207 100644 --- a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java @@ -109,16 +109,6 @@ public FriendDiaryResponse getFriendDiary(Long memberId, Long diaryId) { .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 갯수 반환 Long likeCount = likeRepository.countByDiary(diary); @@ -131,7 +121,6 @@ public FriendDiaryResponse getFriendDiary(Long memberId, Long diaryId) { .categories(categoryResponseList) .locationName(diary.getLocationName()) .emoji(diary.getEmoji()) - .file(fileResponseList) .content(diary.getContent()) .likeCount(count) .createAt(diary.getCreateAt()) From 2b9808ce423a54a4e9d0962d8760c4079796edcc Mon Sep 17 00:00:00 2001 From: HyeJiJUN Date: Wed, 16 Oct 2024 00:12:21 +0900 Subject: [PATCH 8/9] =?UTF-8?q?refactor:=20imageUrl=20->=20filename?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=8B=A4=EC=8B=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/potatocake/everymoment/dto/request/FileRequest.java | 2 +- .../java/com/potatocake/everymoment/service/FileService.java | 2 +- 2 files changed, 2 insertions(+), 2 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 8c2b494..9d22d27 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 imageUrl; + private String filename; private int order; } diff --git a/src/main/java/com/potatocake/everymoment/service/FileService.java b/src/main/java/com/potatocake/everymoment/service/FileService.java index 5ab7bb6..771129e 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.getImageUrl()); + MultipartFile file = fileMap.get(info.getFilename()); if (file == null) { throw new GlobalException(ErrorCode.FILE_NOT_FOUND); } From 69fe67facaa502a9811a2aa9abc35bd2df12ae5a Mon Sep 17 00:00:00 2001 From: HyeJiJUN Date: Wed, 16 Oct 2024 00:33:30 +0900 Subject: [PATCH 9/9] style: --- .../everymoment/controller/CommentController.java | 1 - .../everymoment/controller/DiaryController.java | 2 -- .../everymoment/controller/FriendController.java | 2 -- .../java/com/potatocake/everymoment/entity/Diary.java | 1 - .../potatocake/everymoment/exception/ErrorResponse.java | 6 +++--- .../potatocake/everymoment/security/MemberDetails.java | 2 +- .../everymoment/security/filter/LoginFilter.java | 2 +- .../potatocake/everymoment/service/CommentService.java | 3 --- .../com/potatocake/everymoment/service/DiaryService.java | 9 +-------- .../everymoment/service/FriendDiaryService.java | 2 -- 10 files changed, 6 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/potatocake/everymoment/controller/CommentController.java b/src/main/java/com/potatocake/everymoment/controller/CommentController.java index f462e1d..cb86c22 100644 --- a/src/main/java/com/potatocake/everymoment/controller/CommentController.java +++ b/src/main/java/com/potatocake/everymoment/controller/CommentController.java @@ -5,7 +5,6 @@ import com.potatocake.everymoment.security.MemberDetails; import com.potatocake.everymoment.service.CommentService; import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.DeleteMapping; diff --git a/src/main/java/com/potatocake/everymoment/controller/DiaryController.java b/src/main/java/com/potatocake/everymoment/controller/DiaryController.java index bf0f7d2..48bfedf 100644 --- a/src/main/java/com/potatocake/everymoment/controller/DiaryController.java +++ b/src/main/java/com/potatocake/everymoment/controller/DiaryController.java @@ -9,7 +9,6 @@ import com.potatocake.everymoment.dto.response.CommentsResponse; import com.potatocake.everymoment.dto.response.FriendDiariesResponse; import com.potatocake.everymoment.dto.response.FriendDiaryResponse; -import com.potatocake.everymoment.dto.response.MemberDetailResponse; import com.potatocake.everymoment.dto.response.MyDiariesResponse; import com.potatocake.everymoment.dto.response.MyDiaryResponse; import com.potatocake.everymoment.dto.response.NotificationResponse; @@ -23,7 +22,6 @@ import java.time.LocalDate; import lombok.RequiredArgsConstructor; import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.DeleteMapping; diff --git a/src/main/java/com/potatocake/everymoment/controller/FriendController.java b/src/main/java/com/potatocake/everymoment/controller/FriendController.java index 79772c9..35c80ba 100644 --- a/src/main/java/com/potatocake/everymoment/controller/FriendController.java +++ b/src/main/java/com/potatocake/everymoment/controller/FriendController.java @@ -3,12 +3,10 @@ import com.potatocake.everymoment.dto.SuccessResponse; import com.potatocake.everymoment.dto.response.FriendListResponse; import com.potatocake.everymoment.dto.response.OneFriendDiariesResponse; -import com.potatocake.everymoment.entity.Member; import com.potatocake.everymoment.security.MemberDetails; import com.potatocake.everymoment.service.FriendService; import java.time.LocalDate; import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.DeleteMapping; diff --git a/src/main/java/com/potatocake/everymoment/entity/Diary.java b/src/main/java/com/potatocake/everymoment/entity/Diary.java index 50dc3fa..23d7583 100644 --- a/src/main/java/com/potatocake/everymoment/entity/Diary.java +++ b/src/main/java/com/potatocake/everymoment/entity/Diary.java @@ -1,6 +1,5 @@ package com.potatocake.everymoment.entity; -import com.potatocake.everymoment.dto.LocationPoint; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; diff --git a/src/main/java/com/potatocake/everymoment/exception/ErrorResponse.java b/src/main/java/com/potatocake/everymoment/exception/ErrorResponse.java index 54b7db8..0358ebf 100644 --- a/src/main/java/com/potatocake/everymoment/exception/ErrorResponse.java +++ b/src/main/java/com/potatocake/everymoment/exception/ErrorResponse.java @@ -11,9 +11,9 @@ @JsonInclude(Include.NON_NULL) public class ErrorResponse { - private int code; - private String message; - private Map validation; + private final int code; + private final String message; + private final Map validation; @Builder public ErrorResponse(int code, String message) { diff --git a/src/main/java/com/potatocake/everymoment/security/MemberDetails.java b/src/main/java/com/potatocake/everymoment/security/MemberDetails.java index b4fedca..3a5dd31 100644 --- a/src/main/java/com/potatocake/everymoment/security/MemberDetails.java +++ b/src/main/java/com/potatocake/everymoment/security/MemberDetails.java @@ -17,7 +17,7 @@ public class MemberDetails implements UserDetails { private final Member member; - private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); + private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); @Override public Collection getAuthorities() { diff --git a/src/main/java/com/potatocake/everymoment/security/filter/LoginFilter.java b/src/main/java/com/potatocake/everymoment/security/filter/LoginFilter.java index 53a2951..63c7d5a 100644 --- a/src/main/java/com/potatocake/everymoment/security/filter/LoginFilter.java +++ b/src/main/java/com/potatocake/everymoment/security/filter/LoginFilter.java @@ -33,7 +33,7 @@ public class LoginFilter extends UsernamePasswordAuthenticationFilter { private final AuthenticationManager authenticationManager; private final MemberRepository memberRepository; - private boolean postOnly = true; + private final boolean postOnly = true; public LoginFilter(String filterProcessesUrl, ObjectMapper objectMapper, JwtUtil jwtUtil, MemberRepository memberRepository, AuthenticationManager authenticationManager) { diff --git a/src/main/java/com/potatocake/everymoment/service/CommentService.java b/src/main/java/com/potatocake/everymoment/service/CommentService.java index b0d7663..085ed0c 100644 --- a/src/main/java/com/potatocake/everymoment/service/CommentService.java +++ b/src/main/java/com/potatocake/everymoment/service/CommentService.java @@ -12,7 +12,6 @@ import com.potatocake.everymoment.repository.CommentRepository; import com.potatocake.everymoment.repository.DiaryRepository; import com.potatocake.everymoment.repository.MemberRepository; -import com.potatocake.everymoment.security.MemberDetails; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -20,8 +19,6 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/src/main/java/com/potatocake/everymoment/service/DiaryService.java b/src/main/java/com/potatocake/everymoment/service/DiaryService.java index 7b9218b..8293530 100644 --- a/src/main/java/com/potatocake/everymoment/service/DiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/DiaryService.java @@ -5,9 +5,7 @@ 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; import com.potatocake.everymoment.dto.response.MyDiaryResponse; import com.potatocake.everymoment.dto.response.MyDiarySimpleResponse; @@ -26,23 +24,18 @@ 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; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.GeometryFactory; +import org.locationtech.jts.geom.Point; 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; -import org.locationtech.jts.geom.Point; @RequiredArgsConstructor @Transactional diff --git a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java index bb5d207..fb83595 100644 --- a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java @@ -2,7 +2,6 @@ import com.potatocake.everymoment.dto.request.DiaryFilterRequest; import com.potatocake.everymoment.dto.response.CategoryResponse; -import com.potatocake.everymoment.dto.response.FileResponse; import com.potatocake.everymoment.dto.response.FriendDiariesResponse; import com.potatocake.everymoment.dto.response.FriendDiaryResponse; import com.potatocake.everymoment.dto.response.FriendDiarySimpleResponse; @@ -21,7 +20,6 @@ import com.potatocake.everymoment.repository.FriendRepository; import com.potatocake.everymoment.repository.LikeRepository; import com.potatocake.everymoment.repository.MemberRepository; -import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor;