Skip to content

Commit

Permalink
Merge pull request #22 from Food-GO/feature/#18
Browse files Browse the repository at this point in the history
Feature/#18
  • Loading branch information
Jeongh00 authored Aug 21, 2024
2 parents 55440fa + 8ceca36 commit 25f616a
Show file tree
Hide file tree
Showing 37 changed files with 737 additions and 251 deletions.
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/#8 ]
branches: [ feature/#18 ]

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

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.Friendship;
import com.foodgo.coremodule.community.service.ChallengeQueryService;
import com.foodgo.coremodule.community.service.FriendQueryService;
import com.foodgo.coremodule.user.domain.User;
import com.foodgo.coremodule.user.service.UserQueryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

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

private final UserQueryService userQueryService;
private final FriendQueryService friendQueryService;
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();
}

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

List<User> users = friendQueryService.findFriendWithNickname(nickname);

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();
}

public List<FriendRequestList> findFriendRequestList(Long userId) {

return friendQueryService.findFriendRequestList(userId).stream()
.map(user -> new FriendRequestList(user.getId(), user.getUsername()))
.collect(Collectors.toList());
}

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

import com.foodgo.commonmodule.user.exception.UserErrorCode;
import com.foodgo.commonmodule.user.exception.UserExceptionHandler;
import com.foodgo.coremodule.community.domain.Friendship;
import com.foodgo.coremodule.community.service.FriendQueryService;
import com.foodgo.coremodule.user.domain.User;
import com.foodgo.coremodule.user.service.UserQueryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class FriendSaveUseCase {

private final FriendQueryService friendQueryService;
private final UserQueryService userQueryService;

public void acceptFriendRequest(Long userId, Long friendId) {

Friendship friendship = friendQueryService.findByUserIdAndFriendId(friendId, userId);
if (friendship == null) {
throw new UserExceptionHandler(UserErrorCode.FRIEND_NOT_FOUND);
}

friendship.markAsMutual();
friendQueryService.save(friendship);
}

public Boolean requestFriend(User user, Long friendId) {

if (friendQueryService.checkFriendShipYn(user.getId(), friendId)) {
return false;
} else {

User friend = userQueryService.findByUserId(friendId);
final Friendship friendship = Friendship.builder().user(user).friend(friend).isMutual(false).build();
friendQueryService.save(friendship);

return true;
}
}

public void deleteFriend(User user, Long friendId) {

Friendship friendship = friendQueryService.findByUserIdAndFriendId(user.getId(), friendId);

if (friendship != null) {
friendQueryService.delete(friendship);
} else {
throw new UserExceptionHandler(UserErrorCode.FRIEND_NOT_FOUND);
}
}

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

public record FriendRequestList(
Long friendId,
String name
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.foodgo.apimodule.community.dto;

public record FriendSearchList(
Long userId,
String name,
String profileImg,
Integer recentTotalCalorie,
Integer recentCarbRate,
Integer recentProteinRate,
Integer recentFatRate,
Boolean friendYn,
Boolean friendRequestYn,
Boolean challengeYn
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.foodgo.apimodule.community.presentation;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/community/challenge")
@Validated
@Tag(name = "community - challenge", description = "커뮤니티 챌린지 관련 API")
public class CommunityChallengeController {

// 챌린지 생성

// 챌린지 삭제

// 챌린지 조회 (달성률 포함)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package com.foodgo.apimodule.community.presentation;

import com.foodgo.apimodule.community.application.FriendFindUseCase;
import com.foodgo.apimodule.community.application.FriendSaveUseCase;
import com.foodgo.apimodule.community.dto.FriendRequestList;
import com.foodgo.apimodule.community.dto.FriendSearchList;
import com.foodgo.commonmodule.common.ApplicationResponse;
import com.foodgo.coremodule.security.annotation.UserResolver;
import com.foodgo.coremodule.user.domain.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/community/friend")
@Validated
@Tag(name = "community - friend", description = "커뮤니티 친구 관련 API")
public class CommunityFriendController {

private final FriendFindUseCase friendFindUseCase;
private final FriendSaveUseCase friendSaveUseCase;

// 친구 목록 조회 - (공동 목표까지 포함)
@GetMapping
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "친구 목록 조회 - 공동 목표까지 포함",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "친구 목록 조회 API", description = "친구 목록 조회 API 입니다.")
public ApplicationResponse<List<FriendSearchList>> findFriendList(
@UserResolver User user
) {

List<FriendSearchList> friendSearchLists = friendFindUseCase.findFriendList(user);
return ApplicationResponse.onSuccess(friendSearchLists);
}

// 닉네임으로 친구 검색
@GetMapping("/search/{nickname}")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "친구 검색 - 닉네임",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "친구 검색 API", description = "친구 검색 API 입니다. - 닉네임")
public ApplicationResponse<List<FriendSearchList>> findFriendName(
@UserResolver User user,
@PathVariable String nickname
) {

List<FriendSearchList> searchLists = friendFindUseCase.findFriendName(user, nickname);
return ApplicationResponse.onSuccess(searchLists);
}

// 친구 신청하기
@PostMapping("/request/{friendId}")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "친구 요청",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "친구 요청 API", description = "친구 요청 API 입니다.")
public ApplicationResponse<String> requestFriend(
@UserResolver User user,
@PathVariable Long friendId
) {

Boolean result = friendSaveUseCase.requestFriend(user, friendId);
if (result) {
return ApplicationResponse.onSuccess("친구 신청하였습니다.");
} else {
return ApplicationResponse.onFailure("400", "이미 신청한 친구입니다.");
}
}

// 친구 삭제하기
@DeleteMapping("/request/{friendId}")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "친구 삭제",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "친구 삭제 API", description = "친구 삭제 API 입니다.")
public ApplicationResponse<String> deleteFriendship(
@UserResolver User user,
@PathVariable Long friendId
) {

friendSaveUseCase.deleteFriend(user, friendId);
return ApplicationResponse.onSuccess("친구 관계가 삭제되었습니다.");
}

// 친구 요청 리스트
@GetMapping("/request")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "친구 요청 리스트",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "친구 요청 리스트 API", description = "친구 요청 리스트 API 입니다.")
public ApplicationResponse<List<FriendRequestList>> findFriendRequestList(
@UserResolver User user
) {

List<FriendRequestList> requestLists = friendFindUseCase.findFriendRequestList(user.getId());
return ApplicationResponse.onSuccess(requestLists);
}

// 친구 수락하기
@PatchMapping("/accept/{friendId}")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "친구 수락하기",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "친구 수락 API", description = "친구 수락 API 입니다.")
public ApplicationResponse<String> acceptFriend(
@UserResolver User user,
@PathVariable Long friendId
) {

friendSaveUseCase.acceptFriendRequest(user.getId(), friendId);
return ApplicationResponse.onSuccess("친구 수락하였습니다.");
}

}
Loading

0 comments on commit 25f616a

Please sign in to comment.