Skip to content

Commit

Permalink
feat: 친구 요청 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongh00 committed Aug 20, 2024
1 parent 6342aaa commit e901bb0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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;
Expand All @@ -12,11 +14,26 @@
public class FriendSaveUseCase {

private final FriendQueryService friendQueryService;
private final UserQueryService userQueryService;

public void acceptFriendRequest(Long userId, Long friendId) {

Friendship friendship = friendQueryService.findByUserIdAndFriendId(userId, friendId);
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ public ApplicationResponse<List<FriendSearchList>> findFriendName(
}

// 친구 신청하기
@PostMapping("/friend/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", "이미 신청한 친구입니다.");
}
}

// 친구 삭제하기

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public User findByUserName(String username) {
.orElseThrow(() -> new UserExceptionHandler(UserErrorCode.USER_NOT_FOUND));
}

public User findByUserId(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new UserExceptionHandler(UserErrorCode.USER_NOT_FOUND));
}

public List<User> findAllUsers() {
return userRepository.findAll();
}
Expand Down

0 comments on commit e901bb0

Please sign in to comment.