-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#98] 대한민국 평균 행복지수 API 🎃 #100
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/java/com/hobak/happinessql/domain/report/api/TrendController.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,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, "대한민국 평균 행복지수를 성공적으로 조회했습니다."); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/hobak/happinessql/domain/report/application/TrendHappinessService.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,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); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/hobak/happinessql/domain/report/converter/TrendConverter.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,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(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/hobak/happinessql/domain/report/domain/HappinessLevel.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,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; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/hobak/happinessql/domain/report/dto/TrendHappinessResponseDto.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,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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
머지를 먼저 눌렀지만..!! 리뷰 살짝쿵 남겨봅니다!! 👀 중요한 내용은 아니구 나~중에 편할 때 수정하면 좋을 것 같아효!
HappinessLevel
enum 생성해서 값 매핑한 뷰분 너무 깔끔해서 좋았습니다!!다만 enum에 emoji 인스턴스 변수를 추가한 다음 이모지도 다음과 같이 매핑하면 좋을 것 같다는 생각이 듭니다!
따라서 TrendHappinessService의 이 부분을
아래와 같이 수정하면 코드가 더 간결해질 것 같습니다!!
나중에 시간 되실 때 한 번 검토해보시면 좋을 것 같아용가리 🍀