Skip to content

Commit

Permalink
Merge pull request #97 from Next-Room/feature/analytics
Browse files Browse the repository at this point in the history
[FEAT] ํ†ต๊ณ„ ๋ฌธ์ œ๋ณ„ ํ‰๊ท  ํžŒํŠธ ์‚ฌ์šฉ ํšŸ์ˆ˜ API ๊ตฌํ˜„
  • Loading branch information
eunsol-an authored Dec 4, 2023
2 parents 94418f6 + 6a3cdbc commit 1e99b0b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import static com.nextroom.nextRoomServer.exceptions.StatusCode.*;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.nextroom.nextRoomServer.dto.BaseResponse;
import com.nextroom.nextRoomServer.dto.DataResponse;
import com.nextroom.nextRoomServer.dto.HistoryDto;
import com.nextroom.nextRoomServer.service.HistoryService;

Expand Down Expand Up @@ -39,4 +42,19 @@ public ResponseEntity<BaseResponse> addHistory(@RequestBody HistoryDto.AddPlayHi
historyService.addHistory(request);
return ResponseEntity.ok(new BaseResponse(OK));
}

@Operation(
summary = "๋ฌธ์ œ๋ณ„ ํ‰๊ท  ํžŒํŠธ ์‚ฌ์šฉ ํšŸ์ˆ˜",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "401", description = "TOKEN_UNAUTHORIZED"),
@ApiResponse(responseCode = "403", description = "NOT_PERMITTED"),
@ApiResponse(responseCode = "404", description = "TARGET_SHOP_NOT_FOUND"),
@ApiResponse(responseCode = "404", description = "TARGET_THEME_NOT_FOUND")
}
)
@GetMapping
public ResponseEntity<BaseResponse> getThemeAnalytics(@RequestParam("themeId") Long themeId) {
return ResponseEntity.ok(new DataResponse<>(OK, historyService.getThemeAnalytics(themeId)));
}
}
22 changes: 20 additions & 2 deletions src/main/java/com/nextroom/nextRoomServer/dto/HistoryDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

public class HistoryDto {
@Getter
@Builder
@RequiredArgsConstructor
@NoArgsConstructor(force = true)
public static class AddPlayHistoryRequest {
Expand All @@ -23,7 +22,6 @@ public static class AddPlayHistoryRequest {
}

@Getter
@Builder
@RequiredArgsConstructor
@NoArgsConstructor(force = true)
public static class AddHintHistoryRequest {
Expand All @@ -33,4 +31,24 @@ public static class AddHintHistoryRequest {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
private final LocalDateTime answerOpenTime;
}

@Getter
@Builder
@RequiredArgsConstructor
@NoArgsConstructor(force = true)
public static class ThemeAnalyticsResponse {
private final Long themeId;
private final Integer totalPlayCount;
private final List<HintAnalyticsListResponse> hint;
}

@Getter
@Builder
@RequiredArgsConstructor
@NoArgsConstructor(force = true)
public static class HintAnalyticsListResponse {
private final Long id;
private final Integer hintOpenCount;
private final Integer answerOpenCount;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
package com.nextroom.nextRoomServer.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.nextroom.nextRoomServer.domain.HintHistory;

public interface HintHistoryRepository extends JpaRepository<HintHistory, Long> {
@Query(value = "SELECT A.hint_id AS id, COUNT(A.hint_id) AS hintOpenCount, SUM(A.answer_count) AS answerOpenCount "
+
"FROM (SELECT hint_id, IF(answer_open_time IS NULL, 0, 1) AS answer_count " +
"FROM play_history PH " +
"INNER JOIN hint_history HH ON PH.play_history_id = HH.play_history_id " +
"WHERE PH.theme_id = :themeId) A " +
"GROUP BY A.hint_id", nativeQuery = true)
List<Object[]> findAnalyticsByThemeId(@Param("themeId") Long themeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
import com.nextroom.nextRoomServer.domain.PlayHistory;

public interface PlayHistoryRepository extends JpaRepository<PlayHistory, Long> {
Integer countByThemeId(Long themeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static com.nextroom.nextRoomServer.exceptions.StatusCode.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -59,4 +62,42 @@ public void addHistory(HistoryDto.AddPlayHistoryRequest request) {
hintHistoryRepository.save(hintHistory);
}
}

@Transactional(readOnly = true)
public HistoryDto.ThemeAnalyticsResponse getThemeAnalytics(Long themeId) {
Theme theme = themeRepository.findById(themeId)
.orElseThrow(() -> new CustomException(TARGET_THEME_NOT_FOUND));

if (!Objects.equals(theme.getShop().getId(), SecurityUtil.getRequestedShopId())) {
throw new CustomException(NOT_PERMITTED);
}

List<Object[]> hintAnalyticsList = hintHistoryRepository.findAnalyticsByThemeId(
theme.getId());

List<HistoryDto.HintAnalyticsListResponse> hintAnalyticsListResponses = new ArrayList<>();
for (Object[] objects : hintAnalyticsList) {
Long hintId = (Long)objects[0];
Long hintOpenCount = (Long)objects[1];
BigDecimal answerCount = (BigDecimal)objects[2];

Integer convertedHintOpenCount = hintOpenCount != null ? hintOpenCount.intValue() : 0;
Integer convertedAnswerCount = answerCount != null ? answerCount.intValue() : 0;

HistoryDto.HintAnalyticsListResponse hintAnalyticsDto = HistoryDto.HintAnalyticsListResponse.builder()
.id(hintId)
.hintOpenCount(convertedHintOpenCount)
.answerOpenCount(convertedAnswerCount)
.build();
hintAnalyticsListResponses.add(hintAnalyticsDto);
}

Integer totalPlayCount = playHistoryRepository.countByThemeId(theme.getId());

return HistoryDto.ThemeAnalyticsResponse.builder()
.themeId(theme.getId())
.totalPlayCount(totalPlayCount)
.hint(hintAnalyticsListResponses)
.build();
}
}

0 comments on commit 1e99b0b

Please sign in to comment.