diff --git a/yakssok/src/main/java/server/yakssok/domain/friend/application/service/FriendService.java b/yakssok/src/main/java/server/yakssok/domain/friend/application/service/FriendService.java index bbbc766..a62e2ee 100644 --- a/yakssok/src/main/java/server/yakssok/domain/friend/application/service/FriendService.java +++ b/yakssok/src/main/java/server/yakssok/domain/friend/application/service/FriendService.java @@ -9,6 +9,7 @@ import server.yakssok.domain.friend.domain.entity.Friend; import server.yakssok.domain.friend.domain.repository.FriendRepository; import server.yakssok.domain.friend.presentation.dto.request.FollowFriendRequest; +import server.yakssok.domain.friend.presentation.dto.response.FollowFriendResponse; import server.yakssok.domain.friend.presentation.dto.response.FollowerInfoGroupResponse; import server.yakssok.domain.friend.presentation.dto.response.FollowerInfoResponse; import server.yakssok.domain.friend.presentation.dto.response.FollowingInfoGroupResponse; @@ -33,6 +34,16 @@ public void followFriendByInviteCode(Long userId, FollowFriendRequest followFrie followEachOther(followFriendRequest, user, following); } + @Transactional + public FollowFriendResponse followFriendByInviteCodeV2(Long userId, FollowFriendRequest followFriendRequest) { + String inviteCode = followFriendRequest.inviteCode(); + User following = userService.getUserIdByInviteCode(inviteCode); + User user = userService.getActiveUser(userId); + relationshipService.validateCanFollow(user.getId(), following.getId()); + followEachOther(followFriendRequest, user, following); + return FollowFriendResponse.of(following); + } + private void followEachOther(FollowFriendRequest followFriendRequest, User user, User following) { Friend userToFollowing = followFriendRequest.toFriend(user, following); Friend followingToUser = followFriendRequest.toFriend(following, user); diff --git a/yakssok/src/main/java/server/yakssok/domain/friend/presentation/controller/FriendController.java b/yakssok/src/main/java/server/yakssok/domain/friend/presentation/controller/FriendController.java index 08b1cc6..e0af768 100644 --- a/yakssok/src/main/java/server/yakssok/domain/friend/presentation/controller/FriendController.java +++ b/yakssok/src/main/java/server/yakssok/domain/friend/presentation/controller/FriendController.java @@ -3,7 +3,6 @@ import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -25,7 +24,7 @@ import server.yakssok.global.common.swagger.ApiErrorResponses; import server.yakssok.global.exception.ErrorCode; -@Tag(name = "Friend", description = "지인 API") +@Tag(name = "Friend", description = "친구 API") @RestController @RequiredArgsConstructor @RequestMapping("/api/friends") @@ -34,7 +33,6 @@ public class FriendController { private final FriendService friendService; private final FollowingMedicationStatusService followingMedicationStatusService; - @Operation(summary = "지인 팔로우") @ApiErrorResponses(value = { @ApiErrorResponse(ErrorCode.ALREADY_FRIEND), diff --git a/yakssok/src/main/java/server/yakssok/domain/friend/presentation/controller/FriendControllerV2.java b/yakssok/src/main/java/server/yakssok/domain/friend/presentation/controller/FriendControllerV2.java new file mode 100644 index 0000000..1a8a387 --- /dev/null +++ b/yakssok/src/main/java/server/yakssok/domain/friend/presentation/controller/FriendControllerV2.java @@ -0,0 +1,44 @@ +package server.yakssok.domain.friend.presentation.controller; + +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import server.yakssok.domain.friend.application.service.FriendService; +import server.yakssok.domain.friend.presentation.dto.request.FollowFriendRequest; +import server.yakssok.domain.friend.presentation.dto.response.FollowFriendResponse; +import server.yakssok.global.common.reponse.ApiResponse; +import server.yakssok.global.common.security.YakssokUserDetails; +import server.yakssok.global.common.swagger.ApiErrorResponse; +import server.yakssok.global.common.swagger.ApiErrorResponses; +import server.yakssok.global.exception.ErrorCode; + +@Tag(name = "Friend", description = "친구 API V2") +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v2/friends") +public class FriendControllerV2 { + private final FriendService friendService; + + @Operation(summary = "지인 팔로우") + @ApiErrorResponses(value = { + @ApiErrorResponse(ErrorCode.ALREADY_FRIEND), + @ApiErrorResponse(ErrorCode.INVALID_INVITE_CODE), + }) + @PostMapping + public ApiResponse followByInviteCode( + @RequestBody @Valid FollowFriendRequest followRequest, + @AuthenticationPrincipal YakssokUserDetails userDetails + ) { + Long userId = userDetails.getUserId(); + FollowFriendResponse followFriendResponse = friendService.followFriendByInviteCodeV2(userId, followRequest); + return ApiResponse.success(followFriendResponse); + } + +} diff --git a/yakssok/src/main/java/server/yakssok/domain/friend/presentation/dto/response/FollowFriendResponse.java b/yakssok/src/main/java/server/yakssok/domain/friend/presentation/dto/response/FollowFriendResponse.java new file mode 100644 index 0000000..dfb7cb1 --- /dev/null +++ b/yakssok/src/main/java/server/yakssok/domain/friend/presentation/dto/response/FollowFriendResponse.java @@ -0,0 +1,16 @@ +package server.yakssok.domain.friend.presentation.dto.response; + +import io.swagger.v3.oas.annotations.media.Schema; +import server.yakssok.domain.user.domain.entity.User; + +public record FollowFriendResponse( + @Schema(description = "닉네임", example = "노을") + String nickname, + @Schema(description = "프로필 이미지 URL", example = "https://example.com/profile.jpg") + String profileImageUrl +) { + + public static FollowFriendResponse of(User following) { + return new FollowFriendResponse(following.getNickName(), following.getProfileImageUrl()); + } +}