diff --git a/src/main/java/com/finalproject/kdiary/controller/chart/ChartController.java b/src/main/java/com/finalproject/kdiary/controller/chart/ChartController.java new file mode 100644 index 0000000..d2080ea --- /dev/null +++ b/src/main/java/com/finalproject/kdiary/controller/chart/ChartController.java @@ -0,0 +1,26 @@ +package com.finalproject.kdiary.controller.chart; + +import com.finalproject.kdiary.common.dto.ApiResponse; +import com.finalproject.kdiary.config.resolver.UserId; +import com.finalproject.kdiary.controller.chart.dto.ChartDto; +import com.finalproject.kdiary.exception.SuccessStatus; +import com.finalproject.kdiary.service.ChartService; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +public class ChartController { + + private final ChartService chartService; + + @GetMapping("/chart") + public ApiResponse> getScore(@UserId String userId, @PageableDefault(size = 7) Pageable pageable) { + return ApiResponse.success(SuccessStatus.GET_USER_SCORE, chartService.getUserScore(userId, pageable)); + } +} diff --git a/src/main/java/com/finalproject/kdiary/controller/chart/dto/ChartDto.java b/src/main/java/com/finalproject/kdiary/controller/chart/dto/ChartDto.java new file mode 100644 index 0000000..54007ca --- /dev/null +++ b/src/main/java/com/finalproject/kdiary/controller/chart/dto/ChartDto.java @@ -0,0 +1,16 @@ +package com.finalproject.kdiary.controller.chart.dto; + +import lombok.Data; + +import java.util.List; + +@Data +public class ChartDto { + private final String id; // label + private final String color; + private final List data; // scores + + public static ChartDto of(String id, String color, List data) { + return new ChartDto(id, color, data); + } +} diff --git a/src/main/java/com/finalproject/kdiary/controller/chart/dto/ChartScoreDto.java b/src/main/java/com/finalproject/kdiary/controller/chart/dto/ChartScoreDto.java new file mode 100644 index 0000000..4f3e1e4 --- /dev/null +++ b/src/main/java/com/finalproject/kdiary/controller/chart/dto/ChartScoreDto.java @@ -0,0 +1,14 @@ +package com.finalproject.kdiary.controller.chart.dto; + +import lombok.Data; +import org.checkerframework.checker.units.qual.C; + +@Data +public class ChartScoreDto { + private final String x; // date + private final Double y; // score + + public static ChartScoreDto of(String date, Double score) { + return new ChartScoreDto(date, score); + } +} diff --git a/src/main/java/com/finalproject/kdiary/exception/SuccessStatus.java b/src/main/java/com/finalproject/kdiary/exception/SuccessStatus.java index fc93a85..406c465 100644 --- a/src/main/java/com/finalproject/kdiary/exception/SuccessStatus.java +++ b/src/main/java/com/finalproject/kdiary/exception/SuccessStatus.java @@ -30,7 +30,12 @@ public enum SuccessStatus { LOGIN_SUCCESS(HttpStatus.OK, "로그인 성공"), TOKEN_REFRESH_SUCCESS(HttpStatus.OK, "토큰 리프레시 성공"), CREATE_USER_SUCCESS(HttpStatus.CREATED, "유저 생성 성공"), - GET_USER_DETAIL(HttpStatus.OK, "유저 정보 조회 성공"); + GET_USER_DETAIL(HttpStatus.OK, "유저 정보 조회 성공"), + + /** + * Chart api + */ + GET_USER_SCORE(HttpStatus.OK, "유저 점수 조회 성공"); private final HttpStatus httpStatus; private final String message; diff --git a/src/main/java/com/finalproject/kdiary/infrastructure/DiaryRepository.java b/src/main/java/com/finalproject/kdiary/infrastructure/DiaryRepository.java index 3f54e0c..06ef6d2 100644 --- a/src/main/java/com/finalproject/kdiary/infrastructure/DiaryRepository.java +++ b/src/main/java/com/finalproject/kdiary/infrastructure/DiaryRepository.java @@ -1,6 +1,8 @@ package com.finalproject.kdiary.infrastructure; import com.finalproject.kdiary.domain.Diary; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.query.Param; @@ -11,5 +13,7 @@ public interface DiaryRepository extends JpaRepository { List findByUserId(@Param("userId") String userId); + Page findByUserIdOrderByDateDesc(@Param("userId") String userId, Pageable pageable); + Optional findByUserIdAndDate(@Param("userId") String userId, Date date); } diff --git a/src/main/java/com/finalproject/kdiary/service/ChartService.java b/src/main/java/com/finalproject/kdiary/service/ChartService.java new file mode 100644 index 0000000..fe5533d --- /dev/null +++ b/src/main/java/com/finalproject/kdiary/service/ChartService.java @@ -0,0 +1,50 @@ +package com.finalproject.kdiary.service; + +import com.finalproject.kdiary.controller.chart.dto.ChartDto; +import com.finalproject.kdiary.controller.chart.dto.ChartScoreDto; +import com.finalproject.kdiary.domain.Diary; +import com.finalproject.kdiary.infrastructure.DiaryRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.text.SimpleDateFormat; +import java.util.*; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class ChartService { + + private final DiaryRepository diaryRepository; + + public List getUserScore(String userId, Pageable pageable) { + Page diaryList = diaryRepository.findByUserIdOrderByDateDesc(userId, pageable); + List speakingScores = new ArrayList<>(); + List writingScores = new ArrayList<>(); + List totalScores = new ArrayList<>(); + ListIterator li = diaryList.toList().listIterator(diaryList.getSize()); + + /* score data */ + SimpleDateFormat format = new SimpleDateFormat("M/d"); + while (li.hasPrevious()) { + Diary diary = (Diary) li.previous(); + String date = format.format(diary.getDate()); + speakingScores.add(ChartScoreDto.of(date, (double) diary.getSpeaking())); + writingScores.add(ChartScoreDto.of(date, (double) diary.getWriting())); + + Double avg = (diary.getSpeaking() + diary.getWriting()) / 2.0; + totalScores.add(ChartScoreDto.of(date, avg)); + } + + /* chart response data */ + List chartList = new ArrayList<>(); + chartList.add(ChartDto.of("말하기", "hsl(102, 70%, 50%)", speakingScores)); + chartList.add(ChartDto.of("쓰기", "hsl(225, 70%, 50%)", writingScores)); + chartList.add(ChartDto.of("최종 점수", "hsl(152, 70%, 50%)", totalScores)); + + return chartList; + } +}