-
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.
Browse files
Browse the repository at this point in the history
[#98] ๋ํ๋ฏผ๊ตญ ํ๊ท ํ๋ณต์ง์ API ๐
- Loading branch information
Showing
5 changed files
with
127 additions
and
0 deletions.
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; | ||
} | ||
} |