Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 일기에 대한 좋아요 상태 응답 필드 추가 #90

Merged
merged 6 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ResponseEntity<SuccessResponse<Void>> createDiaryManual(
@Parameter(description = "인증된 사용자 정보", hidden = true)
@AuthenticationPrincipal MemberDetails memberDetails,
@Parameter(description = "수기 일기 작성 정보", required = true)
@RequestBody DiaryManualCreateRequest diaryManualCreateRequest) {
@RequestBody @Valid DiaryManualCreateRequest diaryManualCreateRequest) {
Long memberId = memberDetails.getId();
diaryService.createDiaryManual(memberId, diaryManualCreateRequest);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.potatocake.everymoment.dto.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Getter
public class CategoryRequest {
private Long categoryId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class CommentRequest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.potatocake.everymoment.dto.request;

import com.potatocake.everymoment.dto.LocationPoint;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class DiaryAutoCreateRequest {
private LocationPoint locationPoint;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.potatocake.everymoment.dto.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.potatocake.everymoment.dto.LocationPoint;
import jakarta.validation.constraints.Size;
import java.util.List;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class DiaryManualCreateRequest {

Expand All @@ -18,7 +21,10 @@ public class DiaryManualCreateRequest {
@Size(max = 250, message = "주소는 250자를 초과할 수 없습니다")
private String address;

@JsonProperty("bookmark")
private boolean isBookmark;

@JsonProperty("public")
private boolean isPublic;

@Size(max = 10, message = "이모지는 10자를 초과할 수 없습니다")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import jakarta.validation.constraints.Size;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class DiaryPatchRequest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public class FriendDiaryResponse {
private String emoji;
private String content;
private LikeCountResponse likeCount;
private boolean isLiked;
private LocalDateTime createAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public class MyDiaryResponse {
private boolean isBookmark;
private String emoji;
private String content;
private boolean isLiked;
private LocalDateTime createAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface LikeRepository extends JpaRepository<Like, Long> {

Long countByDiary(Diary diary);

boolean existsByMemberIdAndDiaryId(Long memberId, Long diaryId);

}
18 changes: 11 additions & 7 deletions src/main/java/com/potatocake/everymoment/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.potatocake.everymoment.repository.DiaryCategoryRepository;
import com.potatocake.everymoment.repository.DiaryRepository;
import com.potatocake.everymoment.repository.FileRepository;
import com.potatocake.everymoment.repository.LikeRepository;
import com.potatocake.everymoment.repository.MemberRepository;
import java.time.LocalDate;
import java.util.List;
Expand All @@ -48,6 +49,7 @@ public class DiaryService {
private final MemberRepository memberRepository;
private final CategoryRepository categoryRepository;
private final FileRepository fileRepository;
private final LikeRepository likeRepository;
private final GeometryFactory geometryFactory;
private final NotificationService notificationService;

Expand Down Expand Up @@ -138,7 +140,7 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil

Specification<Diary> spec;

if(!diaryFilterRequest.hasFilter()){
if (!diaryFilterRequest.hasFilter()) {
LocalDate today = LocalDate.now();

spec = DiarySpecification.filterDiaries(
Expand All @@ -153,9 +155,7 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil

diaryPage = diaryRepository.findAll(spec,
PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize()));
}

else{
} else {
spec = DiarySpecification.filterDiaries(
diaryFilterRequest.getKeyword(),
emojis,
Expand All @@ -167,7 +167,8 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil
.and((root, query, builder) -> builder.equal(root.get("member"), currentMember));

diaryPage = diaryRepository.findAll(spec,
PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize(), Sort.by(Sort.Direction.DESC, "createAt")));
PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize(),
Sort.by(Sort.Direction.DESC, "createAt")));
}

List<MyDiarySimpleResponse> diaryDTOs = diaryPage.getContent().stream()
Expand All @@ -186,7 +187,7 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil
@Transactional(readOnly = true)
public MyDiaryResponse getMyDiary(Long memberId, Long diaryId) {
Diary diary = getExistDiary(memberId, diaryId);
return convertToMyDiaryResponseDto(diary);
return convertToMyDiaryResponseDto(diary, memberId);
}

// 내 일기 위치 조회
Expand Down Expand Up @@ -282,7 +283,7 @@ private Diary getExistDiary(Long memberId, Long diaryId) {
}

//상세 조회시 일기DTO 변환
private MyDiaryResponse convertToMyDiaryResponseDto(Diary savedDiary) {
private MyDiaryResponse convertToMyDiaryResponseDto(Diary savedDiary, Long memberId) {
// 카테고리 찾음
List<DiaryCategory> diaryCategories = diaryCategoryRepository.findByDiary(savedDiary);
List<CategoryResponse> categoryResponseList = diaryCategories.stream()
Expand All @@ -292,6 +293,8 @@ private MyDiaryResponse convertToMyDiaryResponseDto(Diary savedDiary) {
.build())
.collect(Collectors.toList());

boolean isLiked = likeRepository.existsByMemberIdAndDiaryId(memberId, savedDiary.getId());

return MyDiaryResponse.builder()
.id(savedDiary.getId())
.categories(categoryResponseList)
Expand All @@ -300,6 +303,7 @@ private MyDiaryResponse convertToMyDiaryResponseDto(Diary savedDiary) {
.isBookmark(savedDiary.isBookmark())
.emoji(savedDiary.getEmoji())
.content(savedDiary.getContent())
.isLiked(isLiked)
.createAt(savedDiary.getCreateAt())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.potatocake.everymoment.repository.FriendRepository;
import com.potatocake.everymoment.repository.LikeRepository;
import com.potatocake.everymoment.repository.MemberRepository;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -59,16 +58,17 @@ public FriendDiariesResponse getFriendDiaries(Long memberId, DiaryFilterRequest
List<String> emojis = diaryFilterRequest.getEmojis();

Specification<Diary> spec = FriendDiarySpecification.filterDiaries(
diaryFilterRequest.getKeyword(),
emojis,
categories,
diaryFilterRequest.getDate(),
diaryFilterRequest.getFrom(),
diaryFilterRequest.getUntil())
.and((root, query, builder) -> root.get("member").get("id").in(friendIdList));
diaryFilterRequest.getKeyword(),
emojis,
categories,
diaryFilterRequest.getDate(),
diaryFilterRequest.getFrom(),
diaryFilterRequest.getUntil())
.and((root, query, builder) -> root.get("member").get("id").in(friendIdList));

diaryPage = diaryRepository.findAll(spec,
PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize(), Sort.by(Sort.Direction.DESC, "createAt")));
PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize(),
Sort.by(Sort.Direction.DESC, "createAt")));

List<FriendDiarySimpleResponse> friendDiarySimpleResponseList = diaryPage.getContent().stream()
.map(this::convertToFriendDiariesResponseDTO)
Expand All @@ -87,7 +87,7 @@ public FriendDiaryResponse getFriendDiary(Long memberId, Long diaryId) {
Diary diary = diaryRepository.findById(diaryId)
.orElseThrow(() -> new GlobalException(ErrorCode.DIARY_NOT_FOUND));

if(!diary.isPublic()){
if (!diary.isPublic()) {
throw new GlobalException(ErrorCode.DIARY_NOT_PUBLIC);
}

Expand Down Expand Up @@ -115,6 +115,7 @@ public FriendDiaryResponse getFriendDiary(Long memberId, Long diaryId) {

//like 갯수 반환
Long likeCount = likeRepository.countByDiary(diary);
boolean isLiked = likeRepository.existsByMemberIdAndDiaryId(memberId, diary.getId());

LikeCountResponse count = LikeCountResponse.builder()
.likeCount(likeCount)
Expand All @@ -127,6 +128,7 @@ public FriendDiaryResponse getFriendDiary(Long memberId, Long diaryId) {
.emoji(diary.getEmoji())
.content(diary.getContent())
.likeCount(count)
.isLiked(isLiked)
.createAt(diary.getCreateAt())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void toggleLike(Long memberId, Long diaryId) {
Diary diary = diaryRepository.findById(diaryId)
.orElseThrow(() -> new GlobalException(ErrorCode.DIARY_NOT_FOUND));

if (!diary.isPublic()) {
if (!diary.isPublic() && !diary.checkOwner(memberId)) {
throw new GlobalException(ErrorCode.DIARY_NOT_PUBLIC);
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ spring:
url: ${RDS_URL}
username: ${RDS_USERNAME}
password: ${RDS_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver

jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
jdbc:
time_zone: Asia/Seoul
hibernate:
ddl-auto: update
ddl-auto: none

h2:
console:
enabled: false
enabled: true

servlet:
multipart:
Expand Down
Loading
Loading