Skip to content

Commit

Permalink
Merge pull request #77 from PSR-Co/refactor/#74-order-list-paging
Browse files Browse the repository at this point in the history
[refactor] 요청 목록 조회 API 페이징 처리 및 요청 상태별 조회 조건 추가
  • Loading branch information
psyeon1120 authored Aug 7, 2023
2 parents 7cf5949 + 83315b7 commit 6496f37
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 40 deletions.
17 changes: 13 additions & 4 deletions src/main/kotlin/com/psr/psr/order/controller/OrderController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import com.psr.psr.order.dto.OrderReq
import com.psr.psr.order.dto.OrderRes
import com.psr.psr.order.service.OrderService
import jakarta.validation.Valid
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.web.PageableDefault
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*

Expand Down Expand Up @@ -42,11 +46,16 @@ class OrderController(
@GetMapping
fun getOrderList(
@AuthenticationPrincipal userAccount: UserAccount,
type: String,
status: String
): BaseResponse<OrderListRes> {
@RequestParam type: String,
@RequestParam(required = false) status: String?,
@PageableDefault(size = 10, sort = ["id"], direction = Sort.Direction.DESC) pageable: Pageable
): BaseResponse<Page<OrderListRes>> {
if (type !in listOf(SELL, ORDER)) return BaseResponse(BaseResponseCode.INVALID_ORDER_TYPE)
return BaseResponse(orderService.getOrderList(userAccount.getUser(), type, status))

// 전체 요청 상태 조회
return if (status == null) BaseResponse(orderService.getOrderList(userAccount.getUser(), type, pageable))
// 요청 상태별 조회
else BaseResponse(orderService.getOrderListByOrderStatus(userAccount.getUser(), type, status, pageable))
}

// 요청 수정
Expand Down
34 changes: 27 additions & 7 deletions src/main/kotlin/com/psr/psr/order/dto/OrderAssembler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,42 @@ class OrderAssembler {
)
}

fun toPrepareListDto(order: Order, type: String): OrderListComp {
val userName: String =
// 요청 목록 조회 시
fun toListDto(order: Order, type: String, productImgKey: String): OrderListRes {
val userName =
if (type == SELL) order.ordererName
else order.product.user.nickname
return OrderListComp(
val profileImg: String? =
if (type == SELL) order.user.imgKey
else order.product.user.imgKey

return OrderListRes(
orderId = order.id!!,
orderDate = order.createdAt.format(DateTimeFormatter.ISO_DATE),
userName = userName,
profileImgKey = profileImg,
productId = order.product.id,
productName = order.product.name,
isReviewed = order.isReviewed
productImgKey = productImgKey,
isReviewed = null
)
}

fun toListDto(orderList: List<OrderListComp>): OrderListRes {
if (orderList.isEmpty()) return OrderListRes(null)
return OrderListRes(orderList)
// 마이페이지 요청 목록 조회 시
fun toListDto(order: Order, type: String): OrderListRes {
val userName =
if (type == SELL) order.ordererName
else order.product.user.nickname

return OrderListRes(
orderId = order.id!!,
orderDate = order.createdAt.format(DateTimeFormatter.ISO_DATE),
userName = userName,
profileImgKey = null,
productId = order.product.id,
productName = order.product.name,
productImgKey = null,
isReviewed = order.isReviewed
)
}
}
10 changes: 0 additions & 10 deletions src/main/kotlin/com/psr/psr/order/dto/OrderListComp.kt

This file was deleted.

13 changes: 12 additions & 1 deletion src/main/kotlin/com/psr/psr/order/dto/OrderListRes.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package com.psr.psr.order.dto

import com.fasterxml.jackson.annotation.JsonInclude

data class OrderListRes (
val orders: List<OrderListComp>?
val orderId: Long,
val orderDate: String,
val userName: String,
val profileImgKey: String?,
val productId: Long,
val productName: String,
@JsonInclude(JsonInclude.Include.NON_NULL)
val productImgKey: String?,
@JsonInclude(JsonInclude.Include.NON_NULL)
val isReviewed: Boolean?
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package com.psr.psr.order.repository
import com.psr.psr.order.entity.Order
import com.psr.psr.order.entity.OrderStatus
import com.psr.psr.user.entity.User
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

@Repository
interface OrderRepository: JpaRepository<Order, Long> {
fun findByIdAndStatus(orderId: Long, status: String): Order?
fun findByUserAndOrderStatusAndStatus(orderer: User, orderStatus: OrderStatus, status: String): List<Order>
fun findByProductUserAndOrderStatusAndStatus(seller: User, orderStatus: OrderStatus, status: String): List<Order>
fun findByUserAndOrderStatusAndStatus(orderer: User, orderStatus: OrderStatus, status: String, pageable: Pageable): Page<Order>
fun findByProductUserAndOrderStatusAndStatus(seller: User, orderStatus: OrderStatus, status: String, pageable: Pageable): Page<Order>
fun findByUserAndStatus(orderer: User, status: String, pageable: Pageable): Page<Order>
fun findByProductUserAndStatus(seller: User, status: String, pageable: Pageable): Page<Order>
}
32 changes: 21 additions & 11 deletions src/main/kotlin/com/psr/psr/order/service/OrderService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import com.psr.psr.order.entity.Order
import com.psr.psr.order.entity.OrderStatus
import com.psr.psr.order.repository.OrderRepository
import com.psr.psr.product.entity.product.Product
import com.psr.psr.product.repository.product.ProductImgRepository
import com.psr.psr.product.repository.product.ProductRepository
import com.psr.psr.user.entity.User
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service

@Service
Expand All @@ -35,18 +38,25 @@ class OrderService(
return orderAssembler.toOrderResDTO(order, isSeller)
}

// 요청 목록 조회
fun getOrderList(user: User, type: String, status: String): OrderListRes {
val orderStatus: OrderStatus = OrderStatus.findByName(status)
val orders: List<OrderListComp> = (
if (type == SELL)
orderRepository.findByProductUserAndOrderStatusAndStatus(user, orderStatus, ACTIVE_STATUS)
else
orderRepository.findByUserAndOrderStatusAndStatus(user, orderStatus, ACTIVE_STATUS)
)
.map { order: Order -> orderAssembler.toPrepareListDto(order, type) }
// 요청 목록 조회(전체 상태)
fun getOrderList(user: User, type: String, pageable: Pageable): Page<OrderListRes> {
val orderList: Page<Order> =
if (type == SELL)
orderRepository.findByProductUserAndStatus(user, ACTIVE_STATUS, pageable)
else
orderRepository.findByUserAndStatus(user, ACTIVE_STATUS, pageable)
return orderList.map { order: Order -> orderAssembler.toListDto(order, type, order.product.imgs.get(0).imgKey) }
}

return orderAssembler.toListDto(orders)
// 요청 목록 조회(요청 상태별)
fun getOrderListByOrderStatus(user: User, type: String, status: String, pageable: Pageable): Page<OrderListRes> {
val orderStatus = OrderStatus.findByName(status)
val orderList: Page<Order> =
if (type == SELL)
orderRepository.findByProductUserAndOrderStatusAndStatus(user, orderStatus, ACTIVE_STATUS, pageable)
else
orderRepository.findByUserAndOrderStatusAndStatus(user, orderStatus, ACTIVE_STATUS, pageable)
return orderList.map { order: Order -> orderAssembler.toListDto(order, type) }
}

// 요청 수정
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.psr.psr.global.entity.BaseEntity
import com.psr.psr.user.entity.Category
import com.psr.psr.user.entity.User
import jakarta.persistence.*
import org.hibernate.annotations.Where
import org.jetbrains.annotations.NotNull

@Entity
Expand All @@ -29,6 +30,10 @@ data class Product(
@NotNull
var description: String,

var likeNum: Int = 0
var likeNum: Int = 0,

@OneToMany(mappedBy = "product")
@Where(clause = "status = 'active'")
var imgs: List<ProductImg>

): BaseEntity()
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ import org.springframework.stereotype.Repository

@Repository
interface ProductImgRepository: JpaRepository<ProductImg, Long> {
fun findTop1ByProductEqualsAndStatusEqualsOrderByCreatedAtDesc(product: Product, status: String): ProductImg

fun findTop1ByProductAndStatusOrderByCreatedAtDesc(product: Product, status: String): ProductImg
}
4 changes: 2 additions & 2 deletions src/main/kotlin/com/psr/psr/product/service/ProductService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ProductService(
val myProductList: List<Product>? = productRepository.findAllByUserAndStatusOrderByCreatedAtDesc(user, ACTIVE_STATUS)

return myProductList?.map { p: Product ->
val productImg = productImgRepository.findTop1ByProductEqualsAndStatusEqualsOrderByCreatedAtDesc(p, ACTIVE_STATUS)
val productImg = productImgRepository.findTop1ByProductAndStatusOrderByCreatedAtDesc(p, ACTIVE_STATUS)
productAssembler.toMyProductDto(p, productImg.imgKey)
}
}
Expand All @@ -54,7 +54,7 @@ class ProductService(
val products: List<Product>? = productRepository.findAllByUserAndStatusOrderByCreatedAtDesc(user, ACTIVE_STATUS)

val productList = products?.map { p: Product ->
val productImg = productImgRepository.findTop1ByProductEqualsAndStatusEqualsOrderByCreatedAtDesc(p, ACTIVE_STATUS)
val productImg = productImgRepository.findTop1ByProductAndStatusOrderByCreatedAtDesc(p, ACTIVE_STATUS)
productAssembler.toMyProductDto(p, productImg.imgKey)
}
return productAssembler.toGetProductsByUserResDto(user, productList)
Expand Down

0 comments on commit 6496f37

Please sign in to comment.