-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from 2023-FINAL-PROJECT/chart
feat: add chart api
Showing
6 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/com/finalproject/kdiary/controller/chart/ChartController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/finalproject/kdiary/controller/chart/dto/ChartDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/finalproject/kdiary/controller/chart/dto/ChartScoreDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/finalproject/kdiary/service/ChartService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |