-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
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
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
47 changes: 47 additions & 0 deletions
47
src/main/java/org/sopt/app/application/home/ActivityDurationCalculator.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,47 @@ | ||
package org.sopt.app.application.home; | ||
|
||
import java.time.*; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.sopt.app.common.exception.BadRequestException; | ||
import org.sopt.app.common.response.ErrorCode; | ||
|
||
@RequiredArgsConstructor | ||
public class ActivityDurationCalculator { | ||
private final Long firstActivityGeneration; | ||
|
||
public static ActivityDurationCalculator of(final List<Long> generations) { | ||
if (generations == null || generations.isEmpty()) { | ||
throw new BadRequestException(ErrorCode.USER_GENERATION_INFO_NOT_FOUND); | ||
} | ||
|
||
Long firstGeneration = Long.MAX_VALUE; | ||
for (Long generation : generations) { | ||
if (generation < firstGeneration) { | ||
firstGeneration = generation; | ||
} | ||
} | ||
return new ActivityDurationCalculator(firstGeneration); | ||
} | ||
|
||
public int getActivityDuration() { | ||
LocalDate startDate = calculateStartDate(); | ||
return calculateMonthDifference(startDate); | ||
} | ||
|
||
private LocalDate calculateStartDate() { | ||
final int SOPT_START_YEAR = 2007; | ||
final int EVEN_GENERATION_START_MONTH = 3; | ||
final int ODD_GENERATION_START_MONTH = 9; | ||
int startMonth = (firstActivityGeneration % 2 == 0) ? EVEN_GENERATION_START_MONTH : ODD_GENERATION_START_MONTH; | ||
int startYear = SOPT_START_YEAR + (int) (firstActivityGeneration / 2); | ||
return LocalDate.of(startYear, startMonth, 1); | ||
} | ||
|
||
private int calculateMonthDifference(LocalDate startDate) { | ||
LocalDate currentDate = ZonedDateTime.now(ZoneId.of("Asia/Seoul")).toLocalDate(); | ||
Period period = Period.between(startDate, currentDate); | ||
int monthDifference = period.getYears() * 12 + period.getMonths(); | ||
return monthDifference + 1; | ||
} | ||
} |
17 changes: 16 additions & 1 deletion
17
...rg/sopt/app/facade/DescriptionFacade.java → .../java/org/sopt/app/facade/HomeFacade.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 |
---|---|---|
@@ -1,24 +1,39 @@ | ||
package org.sopt.app.facade; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.val; | ||
import org.sopt.app.application.home.ActivityDurationCalculator; | ||
import org.sopt.app.application.playground.PlaygroundAuthService; | ||
import org.sopt.app.application.description.DescriptionInfo.MainDescription; | ||
import org.sopt.app.application.description.DescriptionService; | ||
import org.sopt.app.domain.entity.User; | ||
import org.sopt.app.presentation.home.HomeDescriptionResponse; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class DescriptionFacade { | ||
public class HomeFacade { | ||
|
||
private final DescriptionService descriptionService; | ||
private final PlaygroundAuthService playgroundAuthService; | ||
|
||
@Transactional(readOnly = true) | ||
@Deprecated | ||
public MainDescription getMainDescriptionForUser(User user) { | ||
val userActiveInfo = playgroundAuthService.getPlaygroundUserActiveInfo(user.getPlaygroundToken(), user.getPlaygroundId()); | ||
return descriptionService.getMainDescription(userActiveInfo.status()); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public HomeDescriptionResponse getHomeMainDescription(User user) { | ||
List<Long> ownGenerations = playgroundAuthService.getOwnPlaygroundProfile(user.getPlaygroundToken()) | ||
.getAllGenerations(); | ||
ActivityDurationCalculator calculator = ActivityDurationCalculator.of(ownGenerations); | ||
return HomeDescriptionResponse.of( | ||
user.getUsername(), | ||
calculator.getActivityDuration() | ||
); | ||
} | ||
} |
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
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
37 changes: 37 additions & 0 deletions
37
src/main/java/org/sopt/app/presentation/home/HomeController.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,37 @@ | ||
package org.sopt.app.presentation.home; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import lombok.RequiredArgsConstructor; | ||
import org.sopt.app.domain.entity.User; | ||
import org.sopt.app.facade.HomeFacade; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v2/home") | ||
@SecurityRequirement(name = "Authorization") | ||
public class HomeController { | ||
|
||
private final HomeFacade homeFacade; | ||
|
||
@Operation(summary = "홈 메인 문구 조회") | ||
@ApiResponses({ | ||
@ApiResponse(responseCode = "200", description = "success"), | ||
@ApiResponse(responseCode = "401", description = "token error", content = @Content), | ||
@ApiResponse(responseCode = "500", description = "server error", content = @Content) | ||
}) | ||
@GetMapping("/description") | ||
public ResponseEntity<HomeDescriptionResponse> getHomeMainDescription( | ||
@AuthenticationPrincipal User user | ||
) { | ||
return ResponseEntity.ok( | ||
homeFacade.getHomeMainDescription(user) | ||
); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/org/sopt/app/presentation/home/HomeDescriptionResponse.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,14 @@ | ||
package org.sopt.app.presentation.home; | ||
|
||
import lombok.*; | ||
|
||
@Getter | ||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class HomeDescriptionResponse { | ||
private final String activityDescription; | ||
public static HomeDescriptionResponse of(String userName, int totalActivityMonths) { | ||
return new HomeDescriptionResponse( | ||
userName + "님은\nSOPT와 " + totalActivityMonths + "개월째" | ||
); | ||
} | ||
} |
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