Skip to content

Commit

Permalink
Merge pull request #70 from Team-HMH/fix/#68-post-delete-app-api
Browse files Browse the repository at this point in the history
fix - ์•ฑ ์ถ”๊ฐ€ ์‚ญ์ œ ์˜ค๋ฅ˜ ์ˆ˜์ •
  • Loading branch information
kseysh authored Jan 17, 2024
2 parents 2ff2024 + 1f9f0d6 commit 6010e37
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ResponseEntity<ApiResponse<?>> orderRemoveApp(
@RequestHeader("OS") final String os,
@RequestBody final AppDeleteRequest request
) {
appService.removeApp(userId, request, os);
appService.removeAppAndUpdateRemainingDailyChallenge(userId, request, os);
return ResponseEntity
.status(AppSuccess.DELETE_APP_SUCCESS.getHttpStatus())
.body(ApiResponse.success(AppSuccess.DELETE_APP_SUCCESS, new EmptyJsonResponse()));
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/sopt/org/HMH/domain/app/service/AppService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,24 @@ public class AppService {
private final DailyChallengeService dailyChallengeService;

@Transactional
public void removeApp(Long userId, AppDeleteRequest request, String os) {
App app = appRepository.findByDailyChallengeIdAndAppCodeAndOs(
dailyChallengeService.getTodayDailyChallengeByUserId(userId).getId(),
request.appCode(),
os);

appRepository.deleteById(app.getId());
public void addAppsAndUpdateRemainingDailyChallenge(Long userId, List<AppGoalTimeRequest> requests, String os) {
dailyChallengeService.getRemainingDailyChallengesByUserId(userId).stream()
.forEach(dailyChallenge -> addApps(dailyChallenge, requests, os));
}

@Transactional
public void addAppsAndUpdateRemainingDailyChallenge(Long userId, List<AppGoalTimeRequest> requests, String os) {
public void removeAppAndUpdateRemainingDailyChallenge(Long userId, AppDeleteRequest request, String os) {
dailyChallengeService.getRemainingDailyChallengesByUserId(userId).stream()
.map((dailyChallenge -> addApps(dailyChallenge, requests, os)));
.forEach(dailyChallenge -> removeApp(dailyChallenge, request, os));
}


@Transactional
public void removeApp(DailyChallenge dailyChallenge, AppDeleteRequest request, String os) {
appRepository.delete(appRepository.findByDailyChallengeIdAndAppCodeAndOs(
dailyChallenge.getId(),
request.appCode(),
os));
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sopt.org.HMH.domain.challenge.service;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -13,6 +14,8 @@

import java.util.List;

import static java.util.Objects.nonNull;

@Service
@RequiredArgsConstructor
public class ChallengeService {
Expand All @@ -33,15 +36,18 @@ public Challenge addChallenge(Long userId, Integer period, Long goalTime) {
public Challenge updateChallengeForPeriodWithInfo(Challenge challenge, List<AppGoalTimeRequest> apps, String os) {
for (int count = 0; count < challenge.getPeriod(); count++) {
DailyChallenge dailyChallenge = dailyChallengeService.addDailyChallenge(challenge);
appService.addApps(dailyChallenge, apps, os);
if (nonNull(apps)) {
appService.addApps(dailyChallenge, apps, os);
}
}

return challenge;
}

public List<AppGoalTimeRequest> getLastApps(Long userId) {
List<DailyChallenge> lastDailyChallenges = challengeRepository.findFirstByUserIdOrderByCreatedAtDesc(userId).getDailyChallenges();
List<AppGoalTimeRequest> lastApps = lastDailyChallenges.get(lastDailyChallenges.size()-1)
if (lastDailyChallenges.size() == 0) { return null; }
List<AppGoalTimeRequest> lastApps = lastDailyChallenges.get(lastDailyChallenges.size() - 1)
.getApps()
.stream()
.map(app -> new AppGoalTimeRequest(app.getAppCode(), app.getGoalTime()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public class UserService {
private final ChallengeService challengeService;
private final TokenService tokenService;
private final AppleOAuthProvider appleOAuthProvider;
private final DailyChallengeService dailyChallengeService;
private final AppService appService;

@Transactional
public LoginResponse login(String socialAccessToken, SocialPlatformRequest request) {
Expand Down

0 comments on commit 6010e37

Please sign in to comment.