Skip to content

Commit

Permalink
add : 팔로우 생성 API
Browse files Browse the repository at this point in the history
  • Loading branch information
xonmin committed Aug 14, 2024
1 parent a0d3f19 commit 5937da7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
20 changes: 19 additions & 1 deletion api/src/main/kotlin/com/mashup/dojo/MemberController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.mashup.dojo.common.DojoApiResponse
import com.mashup.dojo.config.security.JwtTokenService
import com.mashup.dojo.domain.MemberId
import com.mashup.dojo.domain.MemberRelationId
import com.mashup.dojo.dto.MemberCreateFriendRelationRequest
import com.mashup.dojo.dto.MemberCreateRequest
import com.mashup.dojo.dto.MemberLoginRequest
import com.mashup.dojo.dto.MemberProfileResponse
Expand Down Expand Up @@ -155,10 +156,27 @@ class MemberController(

// todo : temp api for insert relationship data
@PostMapping("/public/member/relation/{id}")
fun createRelationShip(@PathVariable id: MemberId): DojoApiResponse<List<MemberRelationId>> {
fun createRelationShip(
@PathVariable id: MemberId,
): DojoApiResponse<List<MemberRelationId>> {
return DojoApiResponse.success(memberUseCase.createDefaultMemberRelation(id))
}

// follow 생성 API - todo : mashup 내 인원들 follow 생성을 위해 url public 으로 시작 (이후 변경)
@PostMapping("/public/member/relation")
@Operation(
summary = "팔로우 생성 API",
description = "팔로우 생성 API, 팔로우 기능에 대해서 from 이 to 를 follow 합니다. 이미 follow가 존재한다면 예외를 반환해요",
responses = [
ApiResponse(responseCode = "200", description = "생성된 관계 id")
]
)
fun createFriend(
@RequestBody request: MemberCreateFriendRelationRequest,
): DojoApiResponse<MemberRelationId> {
return DojoApiResponse.success(memberUseCase.updateToFollowRelation(MemberUseCase.CreateFollowCommand(request.fromMemberId, request.toMemberId)))
}

data class MemberCreateResponse(
val id: MemberId,
)
Expand Down
11 changes: 11 additions & 0 deletions api/src/main/kotlin/com/mashup/dojo/dto/MemberCreateRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mashup.dojo.dto

import com.mashup.dojo.domain.ImageId
import com.mashup.dojo.domain.MemberGender
import com.mashup.dojo.domain.MemberId
import com.mashup.dojo.domain.MemberPlatform
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.NotBlank
Expand All @@ -24,3 +25,13 @@ data class MemberCreateRequest(
data class MemberUpdateRequest(
val profileImageId: ImageId?,
)

@Schema(description = "팔로우 생성 요청")
data class MemberCreateFriendRelationRequest(
@field:NotBlank
@Schema(description = "팔로우 요청한 유저 id")
val fromMemberId: MemberId,
@field:NotBlank
@Schema(description = "팔로우 대상 유저 id")
val toMemberId: MemberId,
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ interface MemberRelationService {
): List<MemberRelationId>

fun updateRelationToFriend(
fromId: String,
toId: String,
)
fromId: MemberId,
toId: MemberId,
): MemberRelationId

fun isFriend(
fromId: MemberId,
Expand Down Expand Up @@ -81,15 +81,15 @@ class DefaultMemberRelationService(

@Transactional
override fun updateRelationToFriend(
fromId: String,
toId: String,
) {
val toDomain = memberRelationRepository.findByFromIdAndToId(fromId, toId)?.toDomain() ?: throw DojoException.of(DojoExceptionType.FRIEND_NOT_FOUND)
fromId: MemberId,
toId: MemberId,
): MemberRelationId {
val toDomain = memberRelationRepository.findByFromIdAndToId(fromId.value, toId.value)?.toDomain() ?: throw DojoException.of(DojoExceptionType.FRIEND_NOT_FOUND)
if (toDomain.relation == RelationType.FRIEND) {
throw DojoException.of(DojoExceptionType.ALREADY_FRIEND)
}
val updatedRelation = toDomain.updateToFriend()
memberRelationRepository.save(updatedRelation.toEntity())
return MemberRelationId(memberRelationRepository.save(updatedRelation.toEntity()).id)
}

override fun isFriend(
Expand Down
12 changes: 12 additions & 0 deletions service/src/main/kotlin/com/mashup/dojo/usecase/MemberUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ interface MemberUseCase {
val friendCount: Int,
)

data class CreateFollowCommand(
val fromId: MemberId,
val toId: MemberId,
)

fun create(command: CreateCommand): MemberId

fun update(command: UpdateCommand): MemberId
Expand All @@ -50,6 +55,8 @@ interface MemberUseCase {
fun findMemberByIdMock(targetMemberId: MemberId): ProfileResponse

fun createDefaultMemberRelation(newMemberId: MemberId): List<MemberRelationId>

fun updateToFollowRelation(command: CreateFollowCommand): MemberRelationId
}

@Component
Expand Down Expand Up @@ -145,4 +152,9 @@ class DefaultMemberUseCase(

return memberRelationService.bulkCreateRelation(newMemberId, allMemberIds)
}

@Transactional
override fun updateToFollowRelation(command: MemberUseCase.CreateFollowCommand): MemberRelationId {
return memberRelationService.updateRelationToFriend(command.fromId, command.toId)
}
}

0 comments on commit 5937da7

Please sign in to comment.