Skip to content

Commit

Permalink
�MARA-84: 유저 프로필 수정, 나눔 목록 , 상세 dto 분리, 신청, 생성 유무 확인 파라미터 추가 (#47)
Browse files Browse the repository at this point in the history
* feat: 유저 프로필 수정, 나눔 목록 , 상세 dto 분리, 신청, 생성 유무 확인 파라미터 추가

* refactor: 변수명 변경

* refactor: isMine 변수면 isCreatedCurrentLoginUser 로 변경

* refactor: updateUserInfo 메소드명  updateInfo 로 변경

---------

Co-authored-by: gwon188 <[email protected]>
  • Loading branch information
Jiwon-cho and gwon188 authored Feb 26, 2024
1 parent fb718d1 commit abe0ebb
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 38 deletions.
14 changes: 7 additions & 7 deletions src/main/kotlin/mara/server/domain/share/Share.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApplyShare> = mutableListOf()
Expand All @@ -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
Expand All @@ -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
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/mara/server/domain/share/ShareController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ShareController(private val shareService: ShareService) {

@GetMapping("/{id}")
@Operation(summary = "나눔 상세 조회 API")
fun getShareInfo(@PathVariable(name = "id") id: Long): CommonResponse<ShareResponse> {
fun getShareInfo(@PathVariable(name = "id") id: Long): CommonResponse<ShareDetailResponse> {
return success(shareService.getShareInfo(id))
}

Expand All @@ -46,7 +46,7 @@ class ShareController(private val shareService: ShareService) {

@GetMapping("/{id}/applies")
@Operation(summary = "나눔 신청 사용자 이름 조회 API")
fun getAllApplyUserList(@PathVariable(name = "id") shareId: Long): CommonResponse<List<AppliedUserDto>?> {
fun getAllApplyUserList(@PathVariable(name = "id") shareId: Long): CommonResponse<List<AppliedUserResponse>?> {
return success(shareService.getAllApplyUserList(shareId))
}

Expand Down
68 changes: 50 additions & 18 deletions src/main/kotlin/mara/server/domain/share/ShareDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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,
) {
Expand All @@ -80,9 +112,9 @@ data class AppliedUserDto(
}

fun Page<Share>.toShareResponseListPage(): Page<ShareResponse> {
return this.map { ShareResponse(it) }
return this.map { ShareResponse(it, true) }
}

fun List<ApplyShare>.toApplyShareResponseList(): List<AppliedUserDto> {
return this.map { AppliedUserDto(it) }
fun List<ApplyShare>.toApplyShareResponseList(): List<AppliedUserResponse> {
return this.map { AppliedUserResponse(it) }
}
25 changes: 16 additions & 9 deletions src/main/kotlin/mara/server/domain/share/ShareService.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -59,7 +60,7 @@ class ShareService(
기획에 따로 해당 상황일 때 신청을 막는 기능은 없지만
혹시 모를 상황에 대비해 추가 함
**/
share.plusPeopleCnt()
share.plusPeopleCount()
return shareRepository.save(share).id
}

Expand All @@ -68,22 +69,28 @@ 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<ShareResponse> {
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<ShareResponse> {
return shareRepository.findAllMyCreatedShare(pageable, ShareStatus.valueOf(status), userService.getCurrentLoginUser())
.toShareResponseListPage()
}

fun getAllApplyUserList(shareId: Long): List<AppliedUserDto>? {
fun getAllApplyUserList(shareId: Long): List<AppliedUserResponse>? {
val share = getShare(shareId)
return share.applyShareList.toApplyShareResponseList()
}
Expand Down Expand Up @@ -143,7 +150,7 @@ class ShareService(
/**
신청을 취소하면 사람 수 차감
**/
applyShare.share.minusPeopleCnt()
applyShare.share.minusPeopleCount()
applyShareRepository.deleteById(applyId)
return deleted
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/mara/server/domain/user/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand All @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/mara/server/domain/user/UserController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,6 +52,12 @@ class UserController(
return success(userService.getCurrentUserInfo())
}

@PutMapping
@Operation(summary = "유저 업데이트 API")
fun updateUser(@RequestBody userUpdateRequest: UserUpdateRequest): CommonResponse<Boolean> {
return success(userService.updateUser(userUpdateRequest))
}

@GetMapping("/me/invite-code")
@Operation(summary = "유저 초대 코드 조회 API")
fun getCurrentLoginUserInviteCode(): CommonResponse<UserInviteCodeResponse> {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/mara/server/domain/user/UserDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/mara/server/domain/user/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit abe0ebb

Please sign in to comment.