Skip to content

Commit

Permalink
Merge pull request #352 from SWM-NM/feat/#351
Browse files Browse the repository at this point in the history
✨ [FEAT] 히트맵 그래프 컨트롤러 수정 #351
  • Loading branch information
aj4941 authored Sep 23, 2023
2 parents 8df6d4b + fadcd7b commit f98bbf4
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 20 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ public class TestRecordController {

private final GetGraphService getGraphService;

private final GrassHeatMapService grassHeatMapService;

private final TestDetailsService testDetailsService;

private final RatingService ratingService;
@GetMapping("/heatmaps")
@Operation(summary = "사용자 히트맵 분석 데이터", description = "사용자가 테스트를 본 결과를 바탕으로 히트맵 분석 데이터를 보여줍니다.")
public ResponseEntity<List<GrassDto>> memberRecordGrass() {
return new ResponseEntity<>(getGrassService.getGrassDtos(), HttpStatus.OK);
public ResponseEntity<List<GrassHeatMapResponse>> memberRecordGrass() {
return new ResponseEntity<>(grassHeatMapService.getGrassHeatMap(), HttpStatus.OK);
}
@GetMapping("/graphs")
@Operation(summary = "사용자 그래프 분석 데이터", description = "사용자가 테스트를 본 결과를 바탕으로 그래프 분석 데이터를 보여줍니다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package swm_nm.morandi.domain.testRecord.dto;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;


@Builder
@Setter
@Getter
@AllArgsConstructor
public class GrassHeatMapResponse {

@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.class)
public LocalDate testDate;

public Long solvedCount;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package swm_nm.morandi.domain.testRecord.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import swm_nm.morandi.domain.testInfo.entity.Tests;
import swm_nm.morandi.domain.testInfo.entity.AttemptProblem;

import java.time.LocalDateTime;
import java.util.List;

public interface AttemptProblemRepository extends JpaRepository<AttemptProblem, Long> {
Expand All @@ -12,6 +15,26 @@ public interface AttemptProblemRepository extends JpaRepository<AttemptProblem,
List<AttemptProblem> findAllByTestOrderByAttemptProblemIdAsc(Tests test);
List<AttemptProblem> findAttemptProblemsByTest_TestId(Long testId);

//1년동안의 테스트 기록 히트맵 데이터를 가져온다.
// select test_date, count(*)
// from attempt_problem where test_id = (select test_id from tests t left join t.member m where t.member_id = m.member_id and between 1 year ago and now() and t.test_status = 'COMPLETED' order by t.test_date desc )
// is_solved = true
// group by test_date;
@Query("SELECT a.testDate, count(a.testDate) " +
"FROM AttemptProblem a " +
"LEFT JOIN a.test t " +
"WHERE t.testId IN " +
"(SELECT DISTINCT t.testId " +
"FROM Tests t LEFT JOIN t.member m " +
"WHERE m.memberId = :memberId " +
"AND (t.testDate BETWEEN :oneYearAgo AND CURRENT_TIMESTAMP) " +
"AND t.testStatus = 'COMPLETED') " +
"AND a.isSolved = true " +
"GROUP BY a.testDate " +
"ORDER BY a.testDate DESC")
List<Object[]> getHeatMapDataSinceOneYear(@Param("memberId") Long memberId, @Param("oneYearAgo") LocalDateTime oneYearAgo);


//List<AttemptProblem> findAttemptProblemsByTest_TestIdOrderByAttemptProblemIdAsc(Long testId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package swm_nm.morandi.domain.testRecord.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import swm_nm.morandi.domain.testRecord.dto.GrassHeatMapResponse;
import swm_nm.morandi.domain.testRecord.repository.AttemptProblemRepository;
import swm_nm.morandi.global.utils.SecurityUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
public class GrassHeatMapService {

private final AttemptProblemRepository attemptProblemRepository;

public List<GrassHeatMapResponse> getGrassHeatMap() {
Long memberId = SecurityUtils.getCurrentMemberId();
LocalDateTime oneYearAgo = LocalDateTime.now().minusYears(1);
List<Object[]> grassHeatMap = attemptProblemRepository.getHeatMapDataSinceOneYear(memberId, oneYearAgo);

return grassHeatMap.stream().map(objects ->
GrassHeatMapResponse.builder()
.testDate((LocalDate) objects[0])
.solvedCount((Long) objects[1])
.build()).collect(Collectors.toList());

}
}

0 comments on commit f98bbf4

Please sign in to comment.