Skip to content

Commit

Permalink
Merge pull request #100 from Happy-HOBAK/feat/#98
Browse files Browse the repository at this point in the history
[#98] ๋Œ€ํ•œ๋ฏผ๊ตญ ํ‰๊ท  ํ–‰๋ณต์ง€์ˆ˜ API ๐ŸŽƒ
  • Loading branch information
yel-m authored May 29, 2024
2 parents e0e0e76 + 05edb66 commit 278abc1
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.hobak.happinessql.domain.report.api;

import com.hobak.happinessql.domain.report.application.TrendHappinessService;
import com.hobak.happinessql.domain.report.dto.TrendHappinessResponseDto;
import com.hobak.happinessql.global.response.DataResponseDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name="Trend", description = "ํ–‰๋ณต ํŠธ๋ Œ๋“œ ๊ด€๋ จ REST API์— ๋Œ€ํ•œ ๋ช…์„ธ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.")
@RestController
@RequiredArgsConstructor
@RequestMapping("api/trend")
public class TrendController {
private final TrendHappinessService trendHappinessService;
@Operation(summary = "๋Œ€ํ•œ๋ฏผ๊ตญ ํ‰๊ท  ํ–‰๋ณต์ง€์ˆ˜", description = "์ „์ฒด ์œ ์ €์˜ ํ‰๊ท  ํ–‰๋ณต์ง€์ˆ˜์™€ ๊ทธ์— ๋”ฐ๋ฅธ ์ˆ˜์ค€์„ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.")
@GetMapping("/happiness")
public DataResponseDto<TrendHappinessResponseDto> getHappiness() {
TrendHappinessResponseDto responseDto = trendHappinessService.getTrendHappiness();
return DataResponseDto.of(responseDto, "๋Œ€ํ•œ๋ฏผ๊ตญ ํ‰๊ท  ํ–‰๋ณต์ง€์ˆ˜๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ์กฐํšŒํ–ˆ์Šต๋‹ˆ๋‹ค.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.hobak.happinessql.domain.report.application;

import com.hobak.happinessql.domain.record.domain.Record;
import com.hobak.happinessql.domain.record.repository.RecordRepository;
import com.hobak.happinessql.domain.report.converter.TrendConverter;
import com.hobak.happinessql.domain.report.domain.HappinessLevel;
import com.hobak.happinessql.domain.report.dto.TrendHappinessResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class TrendHappinessService {
private final RecordRepository recordRepository;
public TrendHappinessResponseDto getTrendHappiness() {
double totalHappiness = recordRepository.findAll().stream().mapToInt(Record::getHappiness).sum();
double averageHappiness = totalHappiness / recordRepository.count();
averageHappiness = Math.round(averageHappiness * 100.0) / 100.0;
HappinessLevel level = HappinessLevel.of(averageHappiness);
String emoji;
if (averageHappiness >= 1 && averageHappiness < 2) {
emoji = "๐Ÿ˜ฑ";
} else if (averageHappiness >= 2 && averageHappiness < 3) {
emoji = "๐Ÿ™";
} else if (averageHappiness >= 3 && averageHappiness < 5) {
emoji = "๐Ÿ˜";
} else if (averageHappiness >= 5 && averageHappiness < 6) {
emoji = "๐Ÿ™‚";
} else {
emoji = "๐Ÿ˜„";
}
return TrendConverter.toTrendHappinessResponseDto(averageHappiness, level, emoji);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.hobak.happinessql.domain.report.converter;

import com.hobak.happinessql.domain.report.domain.HappinessLevel;
import com.hobak.happinessql.domain.report.dto.TrendHappinessResponseDto;

public class TrendConverter {
public static TrendHappinessResponseDto toTrendHappinessResponseDto(double happiness, HappinessLevel level, String emoji) {
return TrendHappinessResponseDto
.builder()
.happiness(happiness)
.level(level)
.emoji(emoji)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.hobak.happinessql.domain.report.domain;

import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public enum HappinessLevel {
VERY_LOW("๋งค์šฐ ๋‚ฎ์Œ"),
LOW("๋‚ฎ์Œ"),
MEDIUM("๋ณดํ†ต"),
HIGH("๋†’์Œ"),
VERY_HIGH("๋งค์šฐ ๋†’์Œ");
private final String viewName;
public static HappinessLevel of(double happiness) {
if (happiness >= 1 && happiness < 2) {
return VERY_LOW;
} else if (happiness >= 2 && happiness < 3) {
return LOW;
} else if (happiness >= 3 && happiness < 5) {
return MEDIUM;
} else if (happiness >= 5 && happiness < 6) {
return HIGH;
} else {
return VERY_HIGH;
}
}
@JsonValue
public String getViewName() {
return viewName;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.hobak.happinessql.domain.report.dto;

import com.hobak.happinessql.domain.report.domain.HappinessLevel;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class TrendHappinessResponseDto {
private double happiness;
private HappinessLevel level;
private String emoji;
@Builder
public TrendHappinessResponseDto(double happiness, HappinessLevel level, String emoji){
this.happiness = happiness;
this.level = level;
this.emoji = emoji;
}
}

0 comments on commit 278abc1

Please sign in to comment.