Skip to content

Commit

Permalink
feat: 나눔 paging 처리, 등록순, 마감 순 정렬, 나눔 내역 조회 개발 (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiwon-cho authored Feb 21, 2024
1 parent 1bcd93f commit 1c3f582
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ import mara.server.domain.user.User
import org.springframework.data.jpa.repository.JpaRepository

interface ApplyShareRepository : JpaRepository<ApplyShare, Long> {

fun findAllByUser(user: User): List<ApplyShare>?

fun existsByUserAndShare(user: User, share: Share): Boolean
}
18 changes: 16 additions & 2 deletions src/main/kotlin/mara/server/domain/share/ShareController.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package mara.server.domain.share

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import mara.server.common.CommonResponse
import mara.server.common.success
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
Expand All @@ -13,49 +17,59 @@ import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/shares")
@Tag(name = "나눔", description = "나눔 API")
class ShareController(private val shareService: ShareService) {

@PostMapping
@Operation(summary = "나눔 생성 API")
fun createShare(@RequestBody shareRequest: ShareRequest): CommonResponse<Long> {
return success(shareService.createShare(shareRequest))
}

@PostMapping("/applies")
@Operation(summary = "나눔 신청 API")
fun applyShare(@RequestBody applyShareRequest: ApplyShareRequest): CommonResponse<Long> {
return success(shareService.applyShare(applyShareRequest))
}

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

@GetMapping
fun getAllShareList(): CommonResponse<List<ShareResponse>?> {
return success(shareService.getAllShareList())
@Operation(summary = "모든 나눔 조회 API")
fun getAllShareList(pageable: Pageable, status: String): CommonResponse<Page<ShareResponse>> {
return success(shareService.getAllShareList(pageable, status))
}

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

@PutMapping("/{id}")
@Operation(summary = "나눔 업데이트 API")
fun updateShareInfo(@PathVariable(name = "id") shareId: Long, @RequestBody updateShareRequest: UpdateShareRequest): CommonResponse<Boolean> {
return success(shareService.updateShareInfo(shareId, updateShareRequest))
}

@PutMapping("/{id}/status")
@Operation(summary = "나눔 상태 변경 API")
fun changeShareStatus(@PathVariable(name = "id") shareId: Long, updateShareStatusRequest: UpdateShareStatusRequest): CommonResponse<Boolean> {
return success(shareService.changeShareStatus(shareId, updateShareStatusRequest))
}

@DeleteMapping("/{id}")
@Operation(summary = "나눔 삭제 API")
fun deleteShare(@PathVariable(name = "id") shareId: Long): CommonResponse<String> {
return success(shareService.deleteShare(shareId))
}

@DeleteMapping("/applies/{id}")
@Operation(summary = "나눔 신청 취소 API")
fun deleteApply(@PathVariable(name = "id") applyId: Long): CommonResponse<String> {
return success(shareService.deleteApplyShare(applyId))
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/mara/server/domain/share/ShareDto.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mara.server.domain.share

import org.springframework.data.domain.Page
import java.time.LocalDate
import java.time.LocalTime

Expand Down Expand Up @@ -63,6 +64,6 @@ data class ShareResponse(
)
}

fun List<Share>.toShareResponseList(): List<ShareResponse> {
fun Page<Share>.toShareResponseListPage(): Page<ShareResponse> {
return this.map { ShareResponse(it) }
}
112 changes: 110 additions & 2 deletions src/main/kotlin/mara/server/domain/share/ShareRepository.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,116 @@
package mara.server.domain.share

import com.querydsl.core.types.OrderSpecifier
import com.querydsl.jpa.impl.JPAQueryFactory
import mara.server.domain.friend.QFriendship.friendship
import mara.server.domain.share.QApplyShare.applyShare
import mara.server.domain.share.QShare.share
import mara.server.domain.user.User
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.support.PageableExecutionUtils

interface ShareRepository : JpaRepository<Share, Long> {
fun findAllByUser(user: User): List<Share>?
interface ShareRepository : JpaRepository<Share, Long>, CustomShareRepository

interface CustomShareRepository {
fun findAllMyFriendsShare(pageable: Pageable, status: ShareStatus, user: User): Page<Share>

fun findAllMyCreatedShare(pageable: Pageable, status: ShareStatus, user: User): Page<Share>

fun findAllMyAppliedShare(pageable: Pageable, status: ShareStatus, user: User): Page<Share>

fun findAllMyShare(pageable: Pageable, status: ShareStatus, user: User): Page<Share>
}

class CustomShareRepositoryImpl(
private val queryFactory: JPAQueryFactory,
) : CustomShareRepository {

private val registeredDate: String = "registeredDate"
private val dueDate: String = "dueDate"

override fun findAllMyFriendsShare(pageable: Pageable, status: ShareStatus, user: User): Page<Share> {
val friendsList = queryFactory.select(friendship.toUser.userId)
.from(friendship)
.where(friendship.fromUser.eq(user))

var sortBy = ""
pageable.sort.forEach { order ->
sortBy = order.property
}

val query = queryFactory.selectFrom(share)
.where(share.user.userId.`in`(friendsList).and(share.status.eq(status)))
.offset(pageable.offset)
.limit(pageable.pageSize.toLong())
.orderBy(getOrder(sortBy)).fetch()

val count = queryFactory.select(share.count()).from(share).where(share.user.userId.`in`(friendsList).and(share.status.eq(status))).offset(pageable.offset)
.limit(pageable.pageSize.toLong()).fetchOne()

return PageableExecutionUtils.getPage(query, pageable) { count!! }
}

override fun findAllMyCreatedShare(pageable: Pageable, status: ShareStatus, user: User): Page<Share> {
val query = queryFactory.selectFrom(share)
.where(share.user.eq(user).and(share.status.eq(status)))
.offset(pageable.offset)
.limit(pageable.pageSize.toLong())
.orderBy(share.createdAt.desc()).fetch()

val count = queryFactory.select(share.count()).from(share).where(share.user.eq(user).and(share.status.eq(status))).offset(pageable.offset)
.limit(pageable.pageSize.toLong()).fetchOne()

return PageableExecutionUtils.getPage(query, pageable) { count!! }
}

override fun findAllMyAppliedShare(
pageable: Pageable,

status: ShareStatus,
user: User
): Page<Share> {

val query = queryFactory.select(share).from(applyShare)
.join(applyShare.share, share)
.where(applyShare.user.eq(user).and(share.status.eq(status)))
.offset(pageable.offset)
.limit(pageable.pageSize.toLong())
.orderBy(share.createdAt.desc()).fetch()

val count = queryFactory.select(share.count()).from(applyShare)
.join(applyShare.share, share)
.where(applyShare.user.eq(user).and(share.status.eq(status))).offset(pageable.offset)
.limit(pageable.pageSize.toLong())
.fetchOne()

return PageableExecutionUtils.getPage(query, pageable) { count!! }
}

override fun findAllMyShare(pageable: Pageable, status: ShareStatus, user: User): Page<Share> {
val appliedShareIdList = queryFactory.select(share.id).from(applyShare)
.join(applyShare.share, share)
.where(applyShare.user.eq(user).and(share.status.eq(status)))
.fetch()

val query = queryFactory.selectFrom(share)
.where(share.id.`in`(appliedShareIdList).and(share.user.eq(user).and(share.status.eq(status)))).offset(pageable.offset)
.limit(pageable.pageSize.toLong())
.orderBy(share.createdAt.desc()).fetch()

val count = queryFactory.select(share.count()).from(share)
.where(share.id.`in`(appliedShareIdList).and(share.user.eq(user).and(share.status.eq(status)))).offset(pageable.offset)
.limit(pageable.pageSize.toLong()).fetchOne()

return PageableExecutionUtils.getPage(query, pageable) { count!! }
}
private fun getOrder(sortBy: String): OrderSpecifier<*> {
val orderSpecifier = when (sortBy) {
registeredDate -> share.createdAt.desc()
dueDate -> share.shareDatetime.asc()
else -> throw IllegalArgumentException("Invalid sortBy value")
}
return orderSpecifier
}
}
26 changes: 17 additions & 9 deletions src/main/kotlin/mara/server/domain/share/ShareService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package mara.server.domain.share

import mara.server.domain.ingredient.IngredientDetailRepository
import mara.server.domain.user.UserService
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.lang.RuntimeException
Expand Down Expand Up @@ -70,13 +72,15 @@ class ShareService(
return ShareResponse(getShare(shareId))
}

fun getAllShareList(): List<ShareResponse>? {
val shareList = shareRepository.findAll()
return shareList.toShareResponseList()
fun getAllShareList(pageable: Pageable, status: String): Page<ShareResponse> {
val me = userService.getCurrentLoginUser()
val shareList = shareRepository.findAllMyFriendsShare(pageable, ShareStatus.valueOf(status), me)
return shareList.toShareResponseListPage()
}

fun getAllMyShareList(): List<ShareResponse>? {
return shareRepository.findAllByUser(userService.getCurrentLoginUser())?.toShareResponseList()
fun getAllMyCreatedShareList(pageable: Pageable, status: String): Page<ShareResponse> {
return shareRepository.findAllMyCreatedShare(pageable, ShareStatus.valueOf(status), userService.getCurrentLoginUser())
.toShareResponseListPage()
}

fun getAllApplyUserList(shareId: Long): List<String>? {
Expand All @@ -85,10 +89,14 @@ class ShareService(
return applyShareList.map { it.user.nickName }.toList()
}

fun getAllMyApplyShareList(): List<ShareResponse>? {
return applyShareRepository.findAllByUser(userService.getCurrentLoginUser())
?.map { ShareResponse(it.share) }
?.toList()
fun getAllMyAppliedShareList(pageable: Pageable, status: String): Page<ShareResponse>? {
return shareRepository.findAllMyAppliedShare(pageable, ShareStatus.valueOf(status), userService.getCurrentLoginUser())
.toShareResponseListPage()
}

fun getAllMyShareList(pageable: Pageable, status: String): Page<ShareResponse>? {
return shareRepository.findAllMyShare(pageable, ShareStatus.valueOf(status), userService.getCurrentLoginUser())
.toShareResponseListPage()
}

@Transactional
Expand Down
31 changes: 25 additions & 6 deletions src/main/kotlin/mara/server/domain/user/UserController.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package mara.server.domain.user

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import mara.server.common.CommonResponse
import mara.server.common.success
import mara.server.domain.share.ShareResponse
import mara.server.domain.share.ShareService
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.RequestBody
Expand All @@ -13,46 +17,61 @@ import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/users")
@Tag(name = "유저", description = "유저 API")
class UserController(
private val userService: UserService,
private val shareService: ShareService,
) {

@PostMapping
@Operation(summary = "회원 가입 API")
fun createUser(@RequestBody userRequest: UserRequest): CommonResponse<JwtDto> {
return success(userService.singUp(userRequest))
}

@GetMapping("/nickname/check")
@Operation(summary = "닉네임 중복 체크 API")
fun checkNickname(@RequestParam("nickname") nickname: String): CommonResponse<CheckDuplicateResponse> = success(userService.checkNickName(nickname))

@GetMapping("/kakao-login")
@Operation(summary = "카카오 로그인 API")
fun kakaoLogin(@RequestParam(value = "code") authorizedCode: String): CommonResponse<AuthDto> {
return success(userService.kakaoLogin(authorizedCode))
}

@GetMapping("/google-login")
@Operation(summary = "구글 로그인 API")
fun googleLogin(@RequestParam(value = "code") authorizedCode: String): CommonResponse<AuthDto> {
return success(userService.googleLogin(authorizedCode))
}

@GetMapping("/me")
@Operation(summary = "로그인한 유저 조회 API")
fun getCurrentLoginUser(): CommonResponse<UserResponse> {
return success(userService.getCurrentUserInfo())
}

@GetMapping("/me/invite-code")
@Operation(summary = "유저 초대 코드 조회 API")
fun getCurrentLoginUserInviteCode(): CommonResponse<UserInviteCodeResponse> {
return success(userService.getCurrentLoginUserInviteCode())
}

@GetMapping("/me/shares")
fun getAllMyShareList(): CommonResponse<List<ShareResponse>?> {
return success(shareService.getAllMyShareList())
@GetMapping("/me/shares/created")
@Operation(summary = "유저가 올린 나눔 조회 API")
fun getAllMyCreatedShareList(pageable: Pageable, @RequestParam("status") status: String): CommonResponse<Page<ShareResponse>> {
return success(shareService.getAllMyCreatedShareList(pageable, status))
}

@GetMapping("/me/shares/applies")
fun getAllMyApplyShareList(): CommonResponse<List<ShareResponse>?> {
return success(shareService.getAllMyApplyShareList())
@GetMapping("/me/shares/applied")
@Operation(summary = "유저가 신청한 나눔 조회 API")
fun getAllMyAppliedShareList(pageable: Pageable, @RequestParam("status") status: String): CommonResponse<Page<ShareResponse>> {
return success(shareService.getAllMyAppliedShareList(pageable, status))
}

@GetMapping("/me/shares/all")
@Operation(summary = "유저가 관련된 모든 나눔 조회 API")
fun getAllMyShareList(pageable: Pageable, @RequestParam("status") status: String): CommonResponse<Page<ShareResponse>> {
return success(shareService.getAllMyShareList(pageable, status))
}
}

0 comments on commit 1c3f582

Please sign in to comment.