Skip to content

Commit

Permalink
Merge pull request #24 from 2023-FINAL-PROJECT/chart
Browse files Browse the repository at this point in the history
feat: add chart api
SoleiI authored Sep 15, 2023
2 parents d2525e3 + c52ccf0 commit 569ed69
Showing 6 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<List<ChartDto>> getScore(@UserId String userId, @PageableDefault(size = 7) Pageable pageable) {
return ApiResponse.success(SuccessStatus.GET_USER_SCORE, chartService.getUserScore(userId, pageable));
}
}
Original file line number Diff line number Diff line change
@@ -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<ChartScoreDto> data; // scores

public static ChartDto of(String id, String color, List<ChartScoreDto> data) {
return new ChartDto(id, color, data);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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<Diary, Long> {
List<Diary> findByUserId(@Param("userId") String userId);

Page<Diary> findByUserIdOrderByDateDesc(@Param("userId") String userId, Pageable pageable);

Optional<Diary> findByUserIdAndDate(@Param("userId") String userId, Date date);
}
50 changes: 50 additions & 0 deletions src/main/java/com/finalproject/kdiary/service/ChartService.java
Original file line number Diff line number Diff line change
@@ -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<ChartDto> getUserScore(String userId, Pageable pageable) {
Page<Diary> diaryList = diaryRepository.findByUserIdOrderByDateDesc(userId, pageable);
List<ChartScoreDto> speakingScores = new ArrayList<>();
List<ChartScoreDto> writingScores = new ArrayList<>();
List<ChartScoreDto> 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<ChartDto> 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;
}
}

0 comments on commit 569ed69

Please sign in to comment.