Skip to content
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
@@ -1,4 +1,4 @@
package Devroup.hidaddy.controller.community;
package Devroup.hidaddy.controller;

import Devroup.hidaddy.dto.community.*;
import Devroup.hidaddy.entity.User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import Devroup.hidaddy.dto.emotionDiary.*;
import Devroup.hidaddy.entity.User;
import Devroup.hidaddy.service.EmotionDiaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.format.annotation.DateTimeFormat;
Expand All @@ -17,10 +19,12 @@
@RestController
@RequestMapping("/api/emotion-diaries")
@RequiredArgsConstructor
@Tag(name = "Emotion-Diary", description = "감정일기 API")
public class EmotionDiaryController {
private final EmotionDiaryService emotionDiaryService;

// 감정일기 생성 (create)
@Operation(summary = "감정일기 생성", description = "페이징 처리된 게시글 목록을 내림차순으로 조회합니다.")
@PostMapping
public ResponseEntity<Void> createEmotionDiary(
@AuthenticationPrincipal User currentUser,
Expand All @@ -32,6 +36,7 @@ public ResponseEntity<Void> createEmotionDiary(

// 감정일기 조회 (read)
// 감정일기 목록 조회
@Operation(summary = "감정일기 목록 조회", description = "캘린더에 사용할 감정일기 목록을 조회합니다.")
@GetMapping
public ResponseEntity<List<EmotionDiaryResponse>> readEmotionDiary(
// 범위 지정하여 범위 내의 감정일기 조회
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/Devroup/hidaddy/controller/WeeklyContent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package Devroup.hidaddy.controller;

import Devroup.hidaddy.dto.weeklycontent.WeeklyContentResponse;
import Devroup.hidaddy.dto.weeklycontent.WeeklyContentSimpleResponse;
import Devroup.hidaddy.entity.User;
import Devroup.hidaddy.service.WeeklyContentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/weekly")
@RequiredArgsConstructor
@Tag(name = "WeeklyContent", description = "주차별 정보 조회 API")
public class WeeklyContent {
private final WeeklyContentService service;
private final WeeklyContentService weeklyContentService;

@Operation(summary = "현재 주차의 정보 조회", description = "아이의 출산 예정일로 현재 주차를 계산하고 정보를 조회합니다.")
@GetMapping("/current")
public ResponseEntity<WeeklyContentResponse> getCurrentWeeklyContent(
@RequestParam Long babyId,
@AuthenticationPrincipal User user
) {
return ResponseEntity.ok(weeklyContentService.getWeeklyContent(babyId));
}

@Operation(summary = "특정 주차의 정보 조회", description = "원하는 특정 주차의 정보를 조회합니다.")
@GetMapping("/{week}")
public ResponseEntity<WeeklyContentSimpleResponse> getWeeklyContent(
@PathVariable int week
) {
return ResponseEntity.ok(weeklyContentService.getWeeklyContentSimple(week));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package Devroup.hidaddy.dto.weeklycontent;

import Devroup.hidaddy.entity.User;
import Devroup.hidaddy.entity.WeeklyContent;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor
public class WeeklyContentResponse {
private int currentWeek;
private String babyContent;
private String momContent;
private String healthContent;

public static WeeklyContentResponse from(int currentWeek, WeeklyContent weeklyContent) {
return WeeklyContentResponse.builder()
.currentWeek(currentWeek)
.babyContent(weeklyContent.getBabyContent())
.momContent(weeklyContent.getMomContent())
.healthContent(weeklyContent.getHealthContent())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package Devroup.hidaddy.dto.weeklycontent;

import Devroup.hidaddy.entity.WeeklyContent;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
@AllArgsConstructor
public class WeeklyContentSimpleResponse {
private int week;
private String babyContent;
private String momContent;
private String healthContent;

public static WeeklyContentSimpleResponse from(WeeklyContent weeklyContent) {
return WeeklyContentSimpleResponse.builder()
.week(weeklyContent.getWeek())
.babyContent(weeklyContent.getBabyContent())
.momContent(weeklyContent.getMomContent())
.healthContent(weeklyContent.getHealthContent())
.build();
}
}
8 changes: 6 additions & 2 deletions src/main/java/Devroup/hidaddy/entity/WeeklyContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ public class WeeklyContent {

private Integer week;

private String title;
@Lob
private String babyContent;

@Lob
private String momContent;

@Lob
private String content;
private String healthContent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface BabyRepository extends JpaRepository<Baby, Long> {
void deleteAllByUser(User user);

Optional<Baby> findByIdAndUserId(Long babyId, Long userId);

Optional<Baby> findById(Long babyId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package Devroup.hidaddy.repository.weeklycontent;

import Devroup.hidaddy.entity.WeeklyContent;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface WeeklyContentRepository extends JpaRepository<WeeklyContent, Long> {

Optional<WeeklyContent> findByWeek(int week);
}
50 changes: 50 additions & 0 deletions src/main/java/Devroup/hidaddy/service/WeeklyContentService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package Devroup.hidaddy.service;

import Devroup.hidaddy.dto.weeklycontent.WeeklyContentResponse;
import Devroup.hidaddy.dto.weeklycontent.WeeklyContentSimpleResponse;
import Devroup.hidaddy.entity.Baby;
import Devroup.hidaddy.entity.WeeklyContent;
import Devroup.hidaddy.repository.user.BabyRepository;
import Devroup.hidaddy.repository.weeklycontent.WeeklyContentRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class WeeklyContentService {
private final BabyRepository babyRepository;
private final WeeklyContentRepository weeklyContentRepository;

// 출산 예정일로부터 현재 날짜 계산하여 해당 주차 컨텐츠 출력
public WeeklyContentResponse getWeeklyContent(Long babyId) {
Baby baby = babyRepository.findById(babyId)
.orElseThrow(() -> new IllegalArgumentException("아이를 찾을 수 없습니다."));

int currentweek = caculateCurrentWeek(baby.getDueDate().toLocalDate());

WeeklyContent weeklyContent = weeklyContentRepository.findByWeek(currentweek)
.orElseThrow(() -> new IllegalArgumentException(currentweek + "주차 데이터가 없습니다."));

return WeeklyContentResponse.from(currentweek, weeklyContent);
}

// 화살표로 특정 주차 넘어갈 경우 특정 주차의 컨텐츠 출력
public WeeklyContentSimpleResponse getWeeklyContentSimple(int week) {
WeeklyContent weeklyContent = weeklyContentRepository.findByWeek(week)
.orElseThrow(() -> new IllegalArgumentException(week + "주차 데이터가 없습니다."));

return WeeklyContentSimpleResponse.from(weeklyContent);
}

// 현재 날짜를 기준으로 출산 예정일로부터 주차 계산
private int caculateCurrentWeek(LocalDate dueDate) {
long daysLeft = ChronoUnit.DAYS.between(LocalDate.now(), dueDate);
long weeksLeft = daysLeft / 7;
return (int) (40 - weeksLeft);
}
}