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: monthly Diary Lookup API #16

Merged
merged 2 commits into from
Dec 11, 2023
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
6 changes: 6 additions & 0 deletions src/main/java/com/hadam/hadam/controller/DiaryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ public ResponseEntity<BaseResponse<?>> updateIsLiked(@PathVariable Long diaryId)
.body(BaseResponse.of(SuccessCode.OK));
}

@GetMapping("/monthly")
public ResponseEntity<BaseResponse<?>> getMonthlyDiary(@RequestParam Long memberId, @RequestParam int year, @RequestParam int month){
return ResponseEntity.status(HttpStatus.OK)
.body(BaseResponse.of(SuccessCode.OK, diaryService.getMonthlyDiary(memberId, year, month)));
}

}
4 changes: 4 additions & 0 deletions src/main/java/com/hadam/hadam/domain/Diary.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -16,6 +18,8 @@ public class Diary extends BaseTimeEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private LocalDateTime date;

@Column(columnDefinition = "LONGTEXT")
private String img;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.hadam.hadam.dto.response;

public record MonthlyRepresentRes(
Long id,
String img,
String yearMonth
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public enum ErrorCode {
* 404 Not Found
*/
ENTITY_NOT_FOUND(HttpStatus.NOT_FOUND, "엔티티를 찾을 수 없습니다."),
MONTHLY_NOT_FOUND(HttpStatus.NOT_FOUND, "해당하는 달의 일기가 없습니다."),

/**
* 405 Method Not Allowed
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/hadam/hadam/repository/DiaryRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@
import com.hadam.hadam.global.error.exception.EntityNotFoundException;
import com.hadam.hadam.global.error.exception.ErrorCode;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface DiaryRepository extends JpaRepository<Diary, Long> {

default Diary findByIdOrThrow(Long diaryId){
return findById(diaryId)
.orElseThrow(()-> new EntityNotFoundException(ErrorCode.ENTITY_NOT_FOUND));
}

@Query("SELECT d FROM Diary d " +
"WHERE d.member.id = :memberId " +
"AND YEAR(d.date) = :year AND MONTH(d.date) = :month")
List<Diary> findDiariesByMemberIdAndYearMonth(@Param("memberId") Long memberId,
@Param("year") int year,
@Param("month") int month);

}
41 changes: 38 additions & 3 deletions src/main/java/com/hadam/hadam/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@
import com.hadam.hadam.domain.DiaryInfo;
import com.hadam.hadam.domain.Member;
import com.hadam.hadam.dto.request.UpdateDiaryReq;
import com.hadam.hadam.dto.response.MonthlyRepresentRes;
import com.hadam.hadam.global.error.exception.EntityNotFoundException;
import com.hadam.hadam.global.error.exception.ErrorCode;
import com.hadam.hadam.repository.DiaryInfoRepository;
import com.hadam.hadam.repository.DiaryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

@Service
@RequiredArgsConstructor
@Transactional
public class DiaryService {

private final DiaryRepository diaryRepository;
private final DiaryInfoRepository diaryInfoRepository;

@Transactional
public Long updateDiary(Long diaryId, UpdateDiaryReq updateDiaryReq){
Diary diary = diaryRepository.findByIdOrThrow(diaryId);
diary.setDiaryContent(updateDiaryReq.content());
return diaryId;
}

@Transactional
public void deleteDiary(Long diaryId){
Diary diary = diaryRepository.findByIdOrThrow(diaryId);
diaryRepository.delete(diary);
Expand All @@ -36,13 +45,39 @@ public DiaryInfo getDiaryInfoByDiary(Diary diary) {
return diaryInfoRepository.findDiaryInfoByDiary(diary);
}

@Transactional
public void updateIsLiked(Long diaryId){
Diary diary = diaryRepository.findByIdOrThrow(diaryId);
diary.setIsLiked(!diary.isLiked());
diaryRepository.save(diary);
}

@Transactional(readOnly = true)
public MonthlyRepresentRes getMonthlyDiary(Long memberId, int year, int month){
List<Diary> diaries = diaryRepository.findDiariesByMemberIdAndYearMonth(
memberId,
year,
month
);

if (diaries.isEmpty()) {
throw new EntityNotFoundException(ErrorCode.MONTHLY_NOT_FOUND);
}

// 랜덤으로 선택
Random random = new Random();
Diary randomDiary = diaries.get(random.nextInt(diaries.size()));

return new MonthlyRepresentRes(
randomDiary.getId(),
randomDiary.getImg(),
formatYearMonth(year, month)
);
}

public static String formatYearMonth(int year, int month) {
return year + " " + month + "월";
}



}