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

Feature/#18 #22

Merged
merged 14 commits into from
Aug 21, 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/#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
Loading