Skip to content
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

커뮤니티 챌린지 관련 API #24

Merged
merged 8 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Backend CD # actions 이름

on:
push:
branches: [ feature/#18 ]
branches: [ feature/#19 ]

jobs:
deploy:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.foodgo.apimodule.community.application;

import com.foodgo.apimodule.community.dto.ChallengeList;
import com.foodgo.apimodule.community.mapper.ChallengeMapper;
import com.foodgo.coremodule.community.domain.Challenge;
import com.foodgo.coremodule.community.exception.ChallengeErrorCode;
import com.foodgo.coremodule.community.exception.ChallengeException;
import com.foodgo.coremodule.community.service.ChallengeQueryService;
import com.foodgo.coremodule.report.domain.Report;
import com.foodgo.coremodule.report.service.ReportQueryService;
import com.foodgo.coremodule.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ChallengeFindUseCase {

private final ChallengeQueryService challengeQueryService;
private final ReportQueryService reportQueryService;

public ChallengeList findChallengeList(User user, Long challengeId) {

Challenge challenge = challengeQueryService.findChallengeById(challengeId);

LocalDateTime startDate = challenge.getCreatedAt();
LocalDate endDate = toLocalDate(challenge.getYear(), challenge.getMonth(), challenge.getDate());
LocalDateTime endDateTimeEndOfDay = endDate.atTime(LocalTime.MAX);

List<Report> myReports = reportQueryService.findReportByDate(
user.getId(), startDate, endDateTimeEndOfDay);
List<Report> friendReports = reportQueryService.findReportByDate(
challenge.getFriendship().getFriend().getId(), startDate, endDateTimeEndOfDay);

// 챌린지 타입에 따른 달성률 계산
double myRate = calculateRate(myReports, challenge);
double friendRate = calculateRate(friendReports, challenge);

return ChallengeMapper.toDto(challenge, myRate, friendRate);
}

private double calculateRate(List<Report> reports, Challenge challenge) {
int totalSum = 0;

switch (challenge.getType()) {
case CALORIE:
totalSum = reports.stream().mapToInt(Report::getTotal).sum();
break;
case CARB:
totalSum = reports.stream().mapToInt(Report::getCarb).sum();
break;
case PROTEIN:
totalSum = reports.stream().mapToInt(Report::getProtein).sum();
break;
case FAT:
totalSum = reports.stream().mapToInt(Report::getFat).sum();
break;
case FREQUENCY:
totalSum = reports.size();
break;
default:
throw new ChallengeException(ChallengeErrorCode.NO_CHALLENGE_TYPE);
}

return (double) totalSum / challenge.getValue();
}

private LocalDate toLocalDate(Integer year, Integer month, Integer date) {
return LocalDate.of(year, month, date);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.foodgo.apimodule.community.application;

import com.foodgo.apimodule.community.dto.MakeChallenge;
import com.foodgo.apimodule.community.mapper.ChallengeMapper;
import com.foodgo.coremodule.community.domain.Challenge;
import com.foodgo.coremodule.community.domain.Friendship;
import com.foodgo.coremodule.community.exception.ChallengeErrorCode;
import com.foodgo.coremodule.community.exception.ChallengeException;
import com.foodgo.coremodule.community.service.ChallengeQueryService;
import com.foodgo.coremodule.community.service.FriendQueryService;
import com.foodgo.coremodule.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class ChallengeSaveUseCase {

private final ChallengeQueryService challengeQueryService;
private final FriendQueryService friendQueryService;

public void saveChallenge(MakeChallenge makeChallenge, User user) {

Friendship friendship = friendQueryService.findByUserIdAndFriendId(user.getId(), makeChallenge.friendId());
final Challenge challenge = ChallengeMapper.toEntity(makeChallenge, friendship);

challengeQueryService.saveChallenge(challenge);
}

public void deleteChallenge(User user, Long challengeId) {

Challenge challenge = challengeQueryService.findChallengeById(challengeId);
if (challenge.getFriendship().getUser().getId() != user.getId()) {
throw new ChallengeException(ChallengeErrorCode.NOT_CHALLENGE_USER);
} else {
challengeQueryService.deleteChallenge(challengeId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.foodgo.apimodule.community.dto.FriendRequestList;
import com.foodgo.apimodule.community.dto.FriendSearchList;
import com.foodgo.coremodule.community.domain.Challenge;
import com.foodgo.coremodule.community.domain.ChallengeType;
import com.foodgo.coremodule.community.domain.Friendship;
import com.foodgo.coremodule.community.service.ChallengeQueryService;
import com.foodgo.coremodule.community.service.FriendQueryService;
Expand All @@ -12,7 +13,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -26,68 +26,22 @@ public class FriendFindUseCase {
private final ChallengeQueryService challengeQueryService;

public List<FriendSearchList> findFriendList(User myUser) {

// 전체 친구 목록을 가져옴
List<User> users = userQueryService.findAllUsers();

return users.stream()
.filter(user -> friendQueryService.checkFriendShipYn(myUser.getId(), user.getId()))
.map(user -> {

Challenge challenge = challengeQueryService.findRecentChallenge(user.getId());
if (challenge == null) {
challenge = Challenge.builder()
.totalCalorie(0)
.carbRate(0)
.proteinRate(0)
.fatRate(0)
.year(0)
.month(0)
.date(0)
.build();
}

Friendship friendship = friendQueryService.findByUserIdAndFriendId(myUser.getId(), user.getId());
if (friendship == null) {
friendship = Friendship.createFriendship(myUser, user);
}

Boolean challengeYn = challengeQueryService.checkChallengeYn(friendship.getId());

return new FriendSearchList(
user.getId(),
user.getUsername(),
user.getImageUrl(),
challenge.getTotalCalorie(),
challenge.getCarbRate(),
challenge.getProteinRate(),
challenge.getFatRate(),
true,
friendship.getIsMutual(),
challengeYn
);
}).toList();
return processFriendSearchList(users, myUser);
}

public List<FriendSearchList> findFriendName(User myUser, String nickname) {

List<User> users = friendQueryService.findFriendWithNickname(nickname);
return processFriendSearchList(users, myUser);
}

private List<FriendSearchList> processFriendSearchList(List<User> users, User myUser) {
return users.stream()
.filter(user -> friendQueryService.checkFriendShipYn(myUser.getId(), user.getId()))
.map(user -> {

Challenge challenge = challengeQueryService.findRecentChallenge(user.getId());
if (challenge == null) {
challenge = Challenge.builder()
.totalCalorie(0)
.carbRate(0)
.proteinRate(0)
.fatRate(0)
.year(0)
.month(0)
.date(0)
.build();
challenge = createDefaultChallenge();
}

Friendship friendship = friendQueryService.findByUserIdAndFriendId(myUser.getId(), user.getId());
Expand All @@ -101,17 +55,25 @@ public List<FriendSearchList> findFriendName(User myUser, String nickname) {
user.getId(),
user.getUsername(),
user.getImageUrl(),
challenge.getTotalCalorie(),
challenge.getCarbRate(),
challenge.getProteinRate(),
challenge.getFatRate(),
challenge.getType(),
challenge.getValue(),
true,
friendship.getIsMutual(),
challengeYn
);
}).toList();
}

private Challenge createDefaultChallenge() {
return Challenge.builder()
.type(ChallengeType.NONE)
.value(0)
.year(0)
.month(0)
.date(0)
.build();
}

public List<FriendRequestList> findFriendRequestList(Long userId) {

return friendQueryService.findFriendRequestList(userId).stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.foodgo.apimodule.community.dto;

import com.foodgo.coremodule.community.domain.ChallengeType;

public record ChallengeList(
Long challengeId,
ChallengeType type,
Integer value,
Integer year,
Integer month,
Integer date,
Double myAchieveRate,
Double friendAchieveRate
) {
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.foodgo.apimodule.community.dto;

import com.foodgo.coremodule.community.domain.ChallengeType;

public record FriendSearchList(
Long userId,
String name,
String profileImg,
Integer recentTotalCalorie,
Integer recentCarbRate,
Integer recentProteinRate,
Integer recentFatRate,
ChallengeType challengeType,
Integer challengeValue,
Boolean friendYn,
Boolean friendRequestYn,
Boolean challengeYn
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.foodgo.apimodule.community.dto;

import com.foodgo.coremodule.community.domain.ChallengeType;

public record MakeChallenge(
Long friendId,
ChallengeType type,
Integer value,
Integer year,
Integer month,
Integer date
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.foodgo.apimodule.community.mapper;

import com.foodgo.apimodule.community.dto.ChallengeList;
import com.foodgo.apimodule.community.dto.MakeChallenge;
import com.foodgo.coremodule.community.domain.Challenge;
import com.foodgo.coremodule.community.domain.Friendship;

public class ChallengeMapper {

public static Challenge toEntity(MakeChallenge dto, Friendship friendship) {
if (dto == null) {
return null;
}

return Challenge.builder()
.type(dto.type())
.value(dto.value())
.year(dto.year())
.month(dto.month())
.date(dto.date())
.friendship(friendship)
.build();
}

public static ChallengeList toDto(Challenge entity, Double myRate, Double friendRate) {
if (entity == null) {
return null;
}

return new ChallengeList(
entity.getId(),
entity.getType(),
entity.getValue(),
entity.getYear(),
entity.getMonth(),
entity.getDate(),
myRate,
friendRate
);
}
}

Loading
Loading