diff --git a/src/main/kotlin/mara/server/domain/share/Share.kt b/src/main/kotlin/mara/server/domain/share/Share.kt index 88f61fb..e57f8ca 100644 --- a/src/main/kotlin/mara/server/domain/share/Share.kt +++ b/src/main/kotlin/mara/server/domain/share/Share.kt @@ -36,13 +36,13 @@ data class Share( var location: String, @Enumerated(EnumType.STRING) var status: ShareStatus, - var thumbNailImage: String + var thumbnailImage: String ) : BaseEntity() { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val id: Long = 0 - var peopleCnt: Int = 0 + var peopleCount: Int = 0 @OneToMany(mappedBy = "share", cascade = [CascadeType.ALL], orphanRemoval = true) protected val applyShareMutableList: MutableList = mutableListOf() @@ -52,12 +52,12 @@ data class Share( this.applyShareMutableList.add(applyShare) } - fun plusPeopleCnt() { - this.peopleCnt += 1 + fun plusPeopleCount() { + this.peopleCount += 1 } - fun minusPeopleCnt() { - this.peopleCnt -= 1 + fun minusPeopleCount() { + this.peopleCount -= 1 } fun updateIngredientDetail(ingredientDetail: IngredientDetail) { this.ingredientDetail = ingredientDetail @@ -74,7 +74,7 @@ data class Share( this.shareDatetime = updateShareRequest.shareDate.atTime(updateShareRequest.shareTime) this.limitPerson = updateShareRequest.limitPerson this.location = updateShareRequest.location - this.thumbNailImage = updateShareRequest.thumbNailImage + this.thumbnailImage = updateShareRequest.thumbNailImage } } diff --git a/src/main/kotlin/mara/server/domain/share/ShareController.kt b/src/main/kotlin/mara/server/domain/share/ShareController.kt index 973809f..cc083b9 100644 --- a/src/main/kotlin/mara/server/domain/share/ShareController.kt +++ b/src/main/kotlin/mara/server/domain/share/ShareController.kt @@ -34,7 +34,7 @@ class ShareController(private val shareService: ShareService) { @GetMapping("/{id}") @Operation(summary = "나눔 상세 조회 API") - fun getShareInfo(@PathVariable(name = "id") id: Long): CommonResponse { + fun getShareInfo(@PathVariable(name = "id") id: Long): CommonResponse { return success(shareService.getShareInfo(id)) } @@ -46,7 +46,7 @@ class ShareController(private val shareService: ShareService) { @GetMapping("/{id}/applies") @Operation(summary = "나눔 신청 사용자 이름 조회 API") - fun getAllApplyUserList(@PathVariable(name = "id") shareId: Long): CommonResponse?> { + fun getAllApplyUserList(@PathVariable(name = "id") shareId: Long): CommonResponse?> { return success(shareService.getAllApplyUserList(shareId)) } diff --git a/src/main/kotlin/mara/server/domain/share/ShareDto.kt b/src/main/kotlin/mara/server/domain/share/ShareDto.kt index 7d8a3a3..05a89dd 100644 --- a/src/main/kotlin/mara/server/domain/share/ShareDto.kt +++ b/src/main/kotlin/mara/server/domain/share/ShareDto.kt @@ -3,6 +3,7 @@ package mara.server.domain.share import mara.server.domain.user.ProfileImage import org.springframework.data.domain.Page import java.time.LocalDate +import java.time.LocalDateTime import java.time.LocalTime data class ShareRequest( @@ -14,7 +15,7 @@ data class ShareRequest( val limitPerson: Int, val location: String, val status: String, - val thumbNailImage: String + val thumbnailImage: String ) data class ApplyShareRequest( @@ -37,39 +38,70 @@ data class UpdateShareStatusRequest( ) data class ShareResponse( - val nickname: String, - val profileImage: ProfileImage, val shareId: Long, val title: String, - val itemName: String, - val content: String, val shareTime: LocalTime, val shareDate: LocalDate, // 식재료 소비 기한 - val limitDate: LocalDate, val limitPerson: Int, val location: String, + val peopleCount: Int, val status: String, - val thumbNailImage: String + val thumbnailImage: String, + val isApplied: Boolean? ) { - constructor(share: Share) : this( - nickname = share.user.nickname, - profileImage = share.user.profileImage, + constructor(share: Share, isApplied: Boolean?) : this( shareId = share.id, title = share.title, - content = share.content, shareTime = share.shareTime, shareDate = share.shareDate, - itemName = share.ingredientDetail.ingredient.name, - limitDate = share.ingredientDetail.expirationDate.toLocalDate(), limitPerson = share.limitPerson, location = share.location, + peopleCount = share.peopleCount, status = share.status.name, - thumbNailImage = share.thumbNailImage, + thumbnailImage = share.thumbnailImage, + isApplied = isApplied + ) +} + +data class ShareDetailResponse( + val nickname: String, + val profileImage: ProfileImage, + val title: String, + val shareTime: LocalTime, + val shareDate: LocalDate, + val content: String, + // 식재료 소비 기한 + val expirationDate: LocalDateTime, + val limitPerson: Int, + val location: String, + val peopleCount: Int, + val status: ShareStatus, + val thumbNailImage: String, + val itemName: String, + val shareId: Long, + val isCreatedCurrentLoginUser: Boolean, +) { + constructor(share: Share, isCreatedCurrentLoginUser: Boolean) : this( + nickname = share.user.nickname, + profileImage = share.user.profileImage, + title = share.title, + shareTime = share.shareTime, + shareDate = share.shareDate, + content = share.content, + expirationDate = share.ingredientDetail.expirationDate, + limitPerson = share.limitPerson, + location = share.location, + peopleCount = share.peopleCount, + status = share.status, + thumbNailImage = share.thumbnailImage, + itemName = share.ingredientDetail.name, + shareId = share.id, + isCreatedCurrentLoginUser = isCreatedCurrentLoginUser ) } -data class AppliedUserDto( +data class AppliedUserResponse( val nickname: String, val profileImage: ProfileImage, ) { @@ -80,9 +112,9 @@ data class AppliedUserDto( } fun Page.toShareResponseListPage(): Page { - return this.map { ShareResponse(it) } + return this.map { ShareResponse(it, true) } } -fun List.toApplyShareResponseList(): List { - return this.map { AppliedUserDto(it) } +fun List.toApplyShareResponseList(): List { + return this.map { AppliedUserResponse(it) } } diff --git a/src/main/kotlin/mara/server/domain/share/ShareService.kt b/src/main/kotlin/mara/server/domain/share/ShareService.kt index 67af063..22a623d 100644 --- a/src/main/kotlin/mara/server/domain/share/ShareService.kt +++ b/src/main/kotlin/mara/server/domain/share/ShareService.kt @@ -1,5 +1,6 @@ package mara.server.domain.share +import mara.server.auth.security.getCurrentLoginUserId import mara.server.domain.ingredient.IngredientDetailRepository import mara.server.domain.user.UserService import org.springframework.data.domain.Page @@ -37,7 +38,7 @@ class ShareService( limitPerson = shareRequest.limitPerson, location = shareRequest.location, status = ShareStatus.valueOf(shareRequest.status), - thumbNailImage = shareRequest.thumbNailImage, + thumbnailImage = shareRequest.thumbnailImage, ) return shareRepository.save(share).id @@ -59,7 +60,7 @@ class ShareService( 기획에 따로 해당 상황일 때 신청을 막는 기능은 없지만 혹시 모를 상황에 대비해 추가 함 **/ - share.plusPeopleCnt() + share.plusPeopleCount() return shareRepository.save(share).id } @@ -68,14 +69,20 @@ class ShareService( .orElseThrow { NoSuchElementException("해당 나눔 게시물이 존재하지 않습니다. ID: $shareId") } } - fun getShareInfo(shareId: Long): ShareResponse { - return ShareResponse(getShare(shareId)) + fun getShareInfo(shareId: Long): ShareDetailResponse { + val share = getShare(shareId) + return ShareDetailResponse(share, getCurrentLoginUserId() == share.user.userId) } fun getAllShareList(pageable: Pageable, status: String): Page { - val me = userService.getCurrentLoginUser() - val shareList = shareRepository.findAllMyFriendsShare(pageable, ShareStatus.valueOf(status), me) - return shareList.toShareResponseListPage() + val currentLoginUser = userService.getCurrentLoginUser() + val shareList = shareRepository.findAllMyFriendsShare(pageable, ShareStatus.valueOf(status), currentLoginUser).map { share -> + val hasMatchingUser = share.applyShareList.any { applyShare -> + applyShare.user.userId == currentLoginUser.userId + } + ShareResponse(share, hasMatchingUser) + } + return shareList } fun getAllMyCreatedShareList(pageable: Pageable, status: String): Page { @@ -83,7 +90,7 @@ class ShareService( .toShareResponseListPage() } - fun getAllApplyUserList(shareId: Long): List? { + fun getAllApplyUserList(shareId: Long): List? { val share = getShare(shareId) return share.applyShareList.toApplyShareResponseList() } @@ -143,7 +150,7 @@ class ShareService( /** 신청을 취소하면 사람 수 차감 **/ - applyShare.share.minusPeopleCnt() + applyShare.share.minusPeopleCount() applyShareRepository.deleteById(applyId) return deleted } diff --git a/src/main/kotlin/mara/server/domain/user/User.kt b/src/main/kotlin/mara/server/domain/user/User.kt index 4b4afc6..0bc0983 100644 --- a/src/main/kotlin/mara/server/domain/user/User.kt +++ b/src/main/kotlin/mara/server/domain/user/User.kt @@ -13,7 +13,7 @@ enum class Role { USER } @Entity @Table(name = "app_user") class User( - val nickname: String, + var nickname: String, val password: String, val kakaoId: Long?, val kakaoEmail: String?, @@ -29,6 +29,11 @@ class User( @Enumerated(EnumType.STRING) var role: Role = Role.USER protected set + + fun updateInfo(updateRequest: UserUpdateRequest) { + this.nickname = updateRequest.nickname + this.profileImage = updateRequest.profileImage + } } enum class ProfileImage { diff --git a/src/main/kotlin/mara/server/domain/user/UserController.kt b/src/main/kotlin/mara/server/domain/user/UserController.kt index 45ce532..9f56704 100644 --- a/src/main/kotlin/mara/server/domain/user/UserController.kt +++ b/src/main/kotlin/mara/server/domain/user/UserController.kt @@ -10,6 +10,7 @@ import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.PutMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam @@ -51,6 +52,12 @@ class UserController( return success(userService.getCurrentUserInfo()) } + @PutMapping + @Operation(summary = "유저 업데이트 API") + fun updateUser(@RequestBody userUpdateRequest: UserUpdateRequest): CommonResponse { + return success(userService.updateUser(userUpdateRequest)) + } + @GetMapping("/me/invite-code") @Operation(summary = "유저 초대 코드 조회 API") fun getCurrentLoginUserInviteCode(): CommonResponse { diff --git a/src/main/kotlin/mara/server/domain/user/UserDto.kt b/src/main/kotlin/mara/server/domain/user/UserDto.kt index adba124..3f8aa5c 100644 --- a/src/main/kotlin/mara/server/domain/user/UserDto.kt +++ b/src/main/kotlin/mara/server/domain/user/UserDto.kt @@ -10,7 +10,7 @@ data class UserRequest( data class CheckDuplicateResponse(val isDuplicated: Boolean) -data class RefreshAccessTokenRequest(val refreshToken: String) +data class UserUpdateRequest(val nickname: String, val profileImage: ProfileImage) class UserResponse( val nickname: String?, val kakaoId: Long?, diff --git a/src/main/kotlin/mara/server/domain/user/UserService.kt b/src/main/kotlin/mara/server/domain/user/UserService.kt index 4d287aa..9b86cd2 100644 --- a/src/main/kotlin/mara/server/domain/user/UserService.kt +++ b/src/main/kotlin/mara/server/domain/user/UserService.kt @@ -124,6 +124,13 @@ class UserService( ) } + fun updateUser(userUpdateRequest: UserUpdateRequest): Boolean { + val user = getCurrentLoginUser() + user.updateInfo(userUpdateRequest) + val updatedUser = userRepository.save(user) + + return updatedUser.userId == user.userId + } fun createRefreshToken(user: User): String { val refreshToken = refreshTokenRepository.save(RefreshToken(UUID.randomUUID().toString(), user.userId, refreshDurationMins)) return refreshToken.refreshToken