Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 요청 목록 조회 API 생성 #65

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/kotlin/com/psr/psr/cs/service/CsService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.psr.psr.cs.dto.assembler.CsAssembler
import com.psr.psr.cs.entity.FaqType
import com.psr.psr.cs.repository.FaqRepository
import com.psr.psr.cs.repository.NoticeRepository
import com.psr.psr.global.Constant.USER_STATUS.USER_STATUS.ACTIVE_STATUS
import com.psr.psr.global.Constant.UserStatus.UserStatus.ACTIVE_STATUS
import com.psr.psr.global.exception.BaseException
import com.psr.psr.global.exception.BaseResponseCode
import org.springframework.stereotype.Service
Expand Down
15 changes: 11 additions & 4 deletions src/main/kotlin/com/psr/psr/global/Constant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ class Constant {
const val BEARER_PREFIX: String = "Bearer "
}
}
class USER_STATUS {
companion object USER_STATUS {
class UserStatus {
companion object UserStatus {
const val LOGOUT = "logout"
const val ACTIVE_STATUS = "active"
const val INACTIVE_STATUS = "inactive"
}
}

class USER_EID{
companion object USER_EID{
class UserEID{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고냥 QA로 취직하자 ㅎㅅㅎ

companion object UserEID{
const val EID_URL = "https://api.odcloud.kr/api/nts-businessman/v1/validate?serviceKey="
const val PAY_STATUS = "01"
}
}

class OrderType{
companion object OrderType{
const val SELL = "sell"
const val ORDER = "order"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ enum class BaseResponseCode(status: HttpStatus, message: String) {

// order
NOT_FOUND_ORDER(HttpStatus.NOT_FOUND, "해당 요청을 찾을 수 없습니다."),
INVALID_ORDER_STATUS(HttpStatus.BAD_REQUEST, "올바르지 않은 요청 상태입니다."),
INVALID_ORDER_TYPE(HttpStatus.BAD_REQUEST, "올바르지 않은 요청 타입입니다."),

// product
NOT_FOUND_PRODUCT(HttpStatus.NOT_FOUND, "해당 상품을 찾을 수 없습니다.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.psr.psr.global.jwt

import com.psr.psr.global.Constant.USER_STATUS.USER_STATUS.ACTIVE_STATUS
import com.psr.psr.global.Constant.UserStatus.UserStatus.ACTIVE_STATUS
import com.psr.psr.global.exception.BaseException
import com.psr.psr.global.exception.BaseResponseCode
import com.psr.psr.global.exception.BaseResponseCode.NOT_FOUND_USER
import com.psr.psr.user.entity.User
import com.psr.psr.user.repository.UserRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

Expand Down
19 changes: 10 additions & 9 deletions src/main/kotlin/com/psr/psr/inquiry/service/InquiryService.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.psr.psr.inquiry.service

import com.psr.psr.global.Constant.USER_STATUS.USER_STATUS.ACTIVE_STATUS
import com.psr.psr.global.Constant.UserStatus.UserStatus.ACTIVE_STATUS
import com.psr.psr.global.exception.BaseException
import com.psr.psr.global.exception.BaseResponseCode
import com.psr.psr.inquiry.dto.*
Expand All @@ -13,8 +13,8 @@ import org.springframework.stereotype.Service

@Service
class InquiryService(
private val inquiryRepository: InquiryRepository,
private val inquiryAssembler: InquiryAssembler
private val inquiryRepository: InquiryRepository,
private val inquiryAssembler: InquiryAssembler
) {
// 문의 등록
fun makeInquiry(user: User, inquiryReq: InquiryReq) {
Expand All @@ -31,12 +31,13 @@ class InquiryService(
// 문의 목록 조회
fun getInquiryList(user: User, status: String): InquiryListRes {
val inquiryStatus: InquiryStatus = InquiryStatus.findByName(status)
val inquiries: List<InquiryRes> =
(if (user.type == Type.MANAGER)
inquiryRepository.findByInquiryStatusAndStatus(inquiryStatus, ACTIVE_STATUS)
else
inquiryRepository.findByUserAndInquiryStatusAndStatus(user, inquiryStatus, ACTIVE_STATUS))
.map { inquiry: Inquiry -> inquiryAssembler.toPrepareListDto(inquiry) }
val inquiries: List<InquiryRes> = (
if (user.type == Type.MANAGER)
inquiryRepository.findByInquiryStatusAndStatus(inquiryStatus, ACTIVE_STATUS)
else
inquiryRepository.findByUserAndInquiryStatusAndStatus(user, inquiryStatus, ACTIVE_STATUS)
)
.map { inquiry: Inquiry -> inquiryAssembler.toPrepareListDto(inquiry) }

return inquiryAssembler.toListDto(inquiries)
}
Expand Down
35 changes: 23 additions & 12 deletions src/main/kotlin/com/psr/psr/order/controller/OrderController.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.psr.psr.order.controller

import com.psr.psr.global.Constant.OrderType.OrderType.ORDER
import com.psr.psr.global.Constant.OrderType.OrderType.SELL
import com.psr.psr.global.dto.BaseResponse
import com.psr.psr.global.exception.BaseResponseCode
import com.psr.psr.global.jwt.UserAccount
import com.psr.psr.order.dto.OrderListRes
import com.psr.psr.order.dto.OrderReq
import com.psr.psr.order.dto.OrderRes
import com.psr.psr.order.service.OrderService
Expand All @@ -12,18 +16,25 @@ import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("/orders")
class OrderController(
private val orderService: OrderService
private val orderService: OrderService
) {
// 요청하기
@PostMapping
fun makeOrder (@AuthenticationPrincipal userAccount: UserAccount, @RequestBody @Valid orderReq: OrderReq) : BaseResponse<Unit> {
if (orderReq.websiteUrl.isNullOrBlank()) orderReq.websiteUrl = null
return BaseResponse(orderService.makeOrder(userAccount.getUser(), orderReq))
}
// 요청하기
@PostMapping
fun makeOrder(@AuthenticationPrincipal userAccount: UserAccount, @RequestBody @Valid orderReq: OrderReq): BaseResponse<Unit> {
if (orderReq.websiteUrl.isNullOrBlank()) orderReq.websiteUrl = null
return BaseResponse(orderService.makeOrder(userAccount.getUser(), orderReq))
}

// 요청 상세 조회
@GetMapping("/{orderId}")
fun getOrderDetail (@AuthenticationPrincipal userAccount: UserAccount, @PathVariable orderId: Long) : BaseResponse<OrderRes> {
return BaseResponse(orderService.getOrderDetail(userAccount.getUser(), orderId))
}
// 요청 상세 조회
@GetMapping("/{orderId}")
fun getOrderDetail(@AuthenticationPrincipal userAccount: UserAccount,@PathVariable orderId: Long): BaseResponse<OrderRes> {
return BaseResponse(orderService.getOrderDetail(userAccount.getUser(), orderId))
}

// 요청 목록 조회
@GetMapping
fun getOrderList(@AuthenticationPrincipal userAccount: UserAccount, type: String, status: String): BaseResponse<OrderListRes> {
if (type !in listOf(SELL, ORDER)) return BaseResponse(BaseResponseCode.INVALID_ORDER_TYPE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

옹 이렇게 간편하게 예외처리 할 수 잇군여 !!!!

return BaseResponse(orderService.getOrderList(userAccount.getUser(), type, status))
}
}
22 changes: 21 additions & 1 deletion src/main/kotlin/com/psr/psr/order/dto/OrderAssembler.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.psr.psr.order.dto

import com.psr.psr.global.Constant.OrderType.OrderType.SELL
import com.psr.psr.order.entity.Order
import com.psr.psr.product.entity.product.Product
import com.psr.psr.user.entity.User
Expand All @@ -22,7 +23,7 @@ class OrderAssembler {
fun toOrderResDTO(order: Order, isSeller: Boolean): OrderRes {
return OrderRes(
isSeller = isSeller,
status = order.orderStatus.value,
status = order.orderStatus.statusName,
orderUserId = order.user.id!!,
orderDate = order.createdAt.format(DateTimeFormatter.ISO_DATE),
productId = order.product.id,
Expand All @@ -33,4 +34,23 @@ class OrderAssembler {
description = order.description
)
}

fun toPrepareListDto(order: Order, type: String): OrderListComp {
val userName: String =
if (type == SELL) order.ordererName
else order.product.user.nickname
return OrderListComp(
orderId = order.id!!,
orderDate = order.createdAt.format(DateTimeFormatter.ISO_DATE),
userName = userName,
productId = order.product.id,
productName = order.product.name,
isReviewed = order.isReviewed
)
}

fun toListDto(orderList: List<OrderListComp>): OrderListRes {
if (orderList.isEmpty()) return OrderListRes(null)
return OrderListRes(orderList)
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/com/psr/psr/order/dto/OrderListComp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.psr.psr.order.dto

data class OrderListComp (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comp는 어떤 줄임말인가용

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

component인데 orderRes가 이미 있어서 이렇게 지어봤습니당

val orderId: Long,
val orderDate: String,
val userName: String,
val productId: Long,
val productName: String,
val isReviewed: Boolean
)
5 changes: 5 additions & 0 deletions src/main/kotlin/com/psr/psr/order/dto/OrderListRes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.psr.psr.order.dto

data class OrderListRes (
val orders: List<OrderListComp>?
)
43 changes: 23 additions & 20 deletions src/main/kotlin/com/psr/psr/order/entity/Order.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,34 @@ import org.jetbrains.annotations.NotNull
@Entity
@Table(name = "orders")
data class Order(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null,
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null,

@ManyToOne
@JoinColumn(nullable = false, name = "product_id")
var product: Product,
@ManyToOne
@JoinColumn(nullable = false, name = "product_id")
var product: Product,

@ManyToOne
@JoinColumn(nullable = false, name = "user_id")
var user: User,
@ManyToOne
@JoinColumn(nullable = false, name = "user_id")
var user: User,

@NotNull
@Column(length = 100)
var ordererName: String,
@NotNull
@Column(length = 100)
var ordererName: String,

var websiteUrl: String?,
var websiteUrl: String?,

@NotNull
@Enumerated(EnumType.STRING)
var orderStatus: OrderStatus = OrderStatus.ORDER_WAITING,
@NotNull
@Enumerated(EnumType.STRING)
var orderStatus: OrderStatus = OrderStatus.ORDER_WAITING,

@NotNull
var inquiry: String,
@NotNull
var inquiry: String,

@NotNull
var description: String
@NotNull
var description: String,

): BaseEntity()
@NotNull
var isReviewed: Boolean = false

) : BaseEntity()
14 changes: 12 additions & 2 deletions src/main/kotlin/com/psr/psr/order/entity/OrderStatus.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package com.psr.psr.order.entity

enum class OrderStatus(val value: String) {
import com.psr.psr.global.exception.BaseException
import com.psr.psr.global.exception.BaseResponseCode

enum class OrderStatus(val statusName: String) {
ORDER_WAITING("요청대기"),
PROGRESSING("진행중"),
COMPLETED("진행완료"),
CANCELED("요청취소")
CANCELED("요청취소");

companion object {
fun findByName(statusName: String): OrderStatus {
return OrderStatus.values().find { it.statusName == statusName }
?: throw BaseException(BaseResponseCode.INVALID_ORDER_STATUS)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
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.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>
}
56 changes: 35 additions & 21 deletions src/main/kotlin/com/psr/psr/order/service/OrderService.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.psr.psr.order.service

import com.psr.psr.global.Constant.USER_STATUS.USER_STATUS.ACTIVE_STATUS
import com.psr.psr.global.Constant.OrderType.OrderType.SELL
import com.psr.psr.global.Constant.UserStatus.UserStatus.ACTIVE_STATUS
import com.psr.psr.global.exception.BaseException
import com.psr.psr.global.exception.BaseResponseCode
import com.psr.psr.order.dto.OrderAssembler
import com.psr.psr.order.dto.OrderReq
import com.psr.psr.order.dto.OrderRes
import com.psr.psr.order.dto.*
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.ProductRepository
Expand All @@ -15,23 +15,37 @@ import org.springframework.stereotype.Service

@Service
class OrderService(
private val orderRepository: OrderRepository,
private val productRepository: ProductRepository,
private val orderAssembler: OrderAssembler
private val orderRepository: OrderRepository,
private val productRepository: ProductRepository,
private val orderAssembler: OrderAssembler
) {
// 요청하기
fun makeOrder(user: User, orderReq: OrderReq) {
val product: Product = productRepository.findByIdAndStatus(orderReq.productId, ACTIVE_STATUS)
?: throw BaseException(BaseResponseCode.NOT_FOUND_PRODUCT)
orderRepository.save(orderAssembler.toEntity(user, orderReq, product))
}
// 요청하기
fun makeOrder(user: User, orderReq: OrderReq) {
val product: Product = productRepository.findByIdAndStatus(orderReq.productId, ACTIVE_STATUS)
?: throw BaseException(BaseResponseCode.NOT_FOUND_PRODUCT)
orderRepository.save(orderAssembler.toEntity(user, orderReq, product))
}

// 요청 상세 조회
fun getOrderDetail(user: User, orderId: Long): OrderRes {
val order: Order = orderRepository.findByIdAndStatus(orderId, ACTIVE_STATUS)
?: throw BaseException(BaseResponseCode.NOT_FOUND_ORDER)
val isSeller = order.product.user.id == user.id
if (order.user.id != user.id && !isSeller) throw BaseException(BaseResponseCode.NO_PERMISSION)
return orderAssembler.toOrderResDTO(order, isSeller)
}
// 요청 상세 조회
fun getOrderDetail(user: User, orderId: Long): OrderRes {
val order: Order = orderRepository.findByIdAndStatus(orderId, ACTIVE_STATUS)
?: throw BaseException(BaseResponseCode.NOT_FOUND_ORDER)
val isSeller = order.product.user.id == user.id
if (order.user.id != user.id && !isSeller) throw BaseException(BaseResponseCode.NO_PERMISSION)
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) }

return orderAssembler.toListDto(orders)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.psr.psr.product.service

import com.psr.psr.global.Constant.USER_STATUS.USER_STATUS.ACTIVE_STATUS
import com.psr.psr.global.Constant.UserStatus.UserStatus.ACTIVE_STATUS
import com.psr.psr.global.exception.BaseException
import com.psr.psr.global.exception.BaseResponseCode
import com.psr.psr.product.dto.assembler.ProductAssembler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.psr.psr.user.controller

import com.psr.psr.global.Constant.USER_STATUS.USER_STATUS.INACTIVE_STATUS
import com.psr.psr.global.Constant.USER_STATUS.USER_STATUS.LOGOUT
import com.psr.psr.global.Constant.UserStatus.UserStatus.INACTIVE_STATUS
import com.psr.psr.global.Constant.UserStatus.UserStatus.LOGOUT
import com.psr.psr.global.dto.BaseResponse
import com.psr.psr.global.exception.BaseResponseCode
import com.psr.psr.global.jwt.UserAccount
Expand Down
Loading
Loading