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

상품 생성 API 구현 #17

Merged
merged 12 commits into from
Jan 8, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kr.co.fastcampus.yanabada.common.exception;

import static kr.co.fastcampus.yanabada.common.response.ErrorCode.ACCESS_FORBIDDEN;

public class AccessForbiddenException extends BaseException {
public AccessForbiddenException() {
super(ACCESS_FORBIDDEN.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package kr.co.fastcampus.yanabada.common.exception;

import kr.co.fastcampus.yanabada.common.response.ErrorCode;
import static kr.co.fastcampus.yanabada.common.response.ErrorCode.ACCOMMODATION_NOT_FOUND;

public class AccommodationNotFoundException extends BaseException {
public AccommodationNotFoundException() {
super(ErrorCode.ACCOMMODATION_NOT_FOUND.getMessage());
super(ACCOMMODATION_NOT_FOUND.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package kr.co.fastcampus.yanabada.common.exception;

import kr.co.fastcampus.yanabada.common.response.ErrorCode;
import static kr.co.fastcampus.yanabada.common.response.ErrorCode.COMMON_ENTITY_NOT_FOUND;

public class CommonEntityNotFoundException extends BaseException {
public CommonEntityNotFoundException() {
super(ErrorCode.COMMON_ENTITY_NOT_FOUND.getMessage());
super(COMMON_ENTITY_NOT_FOUND.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package kr.co.fastcampus.yanabada.common.exception;

import kr.co.fastcampus.yanabada.common.response.ErrorCode;
import static kr.co.fastcampus.yanabada.common.response.ErrorCode.MEMBER_NOT_FOUND;
Hwang-Kyu-Cheol marked this conversation as resolved.
Show resolved Hide resolved

public class MemberNotFoundException extends BaseException {

public MemberNotFoundException() {
super(ErrorCode.MEMBER_NOT_FOUND.getMessage());
super(MEMBER_NOT_FOUND.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kr.co.fastcampus.yanabada.common.exception;

import static kr.co.fastcampus.yanabada.common.response.ErrorCode.ACCOMMODATION_NOT_FOUND;

public class OrderNotFoundException extends BaseException {
public OrderNotFoundException() {
super(ACCOMMODATION_NOT_FOUND.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kr.co.fastcampus.yanabada.common.exception;

import static kr.co.fastcampus.yanabada.common.response.ErrorCode.ORDER_NOT_SELLABLE;

public class OrderNotSellableException extends BaseException {
public OrderNotSellableException() {
super(ORDER_NOT_SELLABLE.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package kr.co.fastcampus.yanabada.common.exception;

import kr.co.fastcampus.yanabada.common.response.ErrorCode;
import static kr.co.fastcampus.yanabada.common.response.ErrorCode.ROOM_NOT_FOUND;

public class RoomNotFoundException extends BaseException {
public RoomNotFoundException() {
super(ErrorCode.ROOM_NOT_FOUND.getMessage());
super(ROOM_NOT_FOUND.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kr.co.fastcampus.yanabada.common.exception;

import static kr.co.fastcampus.yanabada.common.response.ErrorCode.INVALID_SALE_END_DATE_RANGE;

public class SaleEndDateRangeException extends BaseException {
public SaleEndDateRangeException() {
super(INVALID_SALE_END_DATE_RANGE.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kr.co.fastcampus.yanabada.common.exception;

import static kr.co.fastcampus.yanabada.common.response.ErrorCode.INVALID_SELLING_PRICE_RANGE;

public class SellingPriceRangeException extends BaseException {
public SellingPriceRangeException() {
super(INVALID_SELLING_PRICE_RANGE.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public enum ErrorCode {
ACCOMMODATION_NOT_FOUND("존재하지 않는 숙소입니다."),
ROOM_NOT_FOUND("존재하지 않는 객실입니다."),
MEMBER_NOT_FOUND("존재하지 않는 회원입니다."),
ORDER_NOT_FOUND("존재하지 않는 예약입니다."),
ACCESS_FORBIDDEN("권한이 없습니다."),
ORDER_NOT_SELLABLE("판매할 수 없는 예약입니다."),
INVALID_SELLING_PRICE_RANGE("판매가는 구매가보다 클 수 없습니다."),
INVALID_SALE_END_DATE_RANGE("판매 중단 날짜는 현재 날짜 이상 체크인 날짜 이하여야 합니다."),
;

private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.time.LocalDate;
import kr.co.fastcampus.yanabada.domain.accommodation.entity.Room;
import kr.co.fastcampus.yanabada.domain.member.entity.Member;
import kr.co.fastcampus.yanabada.domain.order.entity.RoomOrder;
import kr.co.fastcampus.yanabada.domain.order.entity.Order;
import kr.co.fastcampus.yanabada.domain.order.entity.enums.OrderStatus;
import kr.co.fastcampus.yanabada.domain.order.entity.enums.PaymentType;

Expand All @@ -23,8 +23,8 @@ public record OrderSaveRequest(
Integer childCount
) {

public RoomOrder toEntity(Room room, Member member) {
return RoomOrder.create(
public Order toEntity(Room room, Member member) {
return Order.create(
room,
member,
checkInDate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "room_order")
@Entity
public class RoomOrder extends BaseEntity {
public class Order extends BaseEntity {
Hwang-Kyu-Cheol marked this conversation as resolved.
Show resolved Hide resolved

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down Expand Up @@ -74,7 +74,7 @@ public class RoomOrder extends BaseEntity {
@Column(nullable = false)
private Integer childCount;

private RoomOrder(
private Order(
Room room,
Member member,
LocalDate checkInDate,
Expand Down Expand Up @@ -104,7 +104,7 @@ private RoomOrder(
this.childCount = childCount;
}

public static RoomOrder create(
public static Order create(
Room room,
Member member,
LocalDate checkInDate,
Expand All @@ -119,7 +119,7 @@ public static RoomOrder create(
Integer adultCount,
Integer childCount
) {
return new RoomOrder(
return new Order(
room,
member,
checkInDate,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kr.co.fastcampus.yanabada.domain.order.repository;

import kr.co.fastcampus.yanabada.domain.order.entity.RoomOrder;
import kr.co.fastcampus.yanabada.domain.order.entity.Order;
import org.springframework.data.jpa.repository.JpaRepository;

public interface OrderRepository extends JpaRepository<RoomOrder, Long> {
public interface OrderRepository extends JpaRepository<Order, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import kr.co.fastcampus.yanabada.domain.member.entity.Member;
import kr.co.fastcampus.yanabada.domain.member.repository.MemberRepository;
import kr.co.fastcampus.yanabada.domain.order.dto.request.OrderSaveRequest;
import kr.co.fastcampus.yanabada.domain.order.entity.RoomOrder;
import kr.co.fastcampus.yanabada.domain.order.entity.Order;
import kr.co.fastcampus.yanabada.domain.order.repository.OrderRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -25,6 +25,6 @@ public class OrderService {
public void saveOrder(OrderSaveRequest request) {
Room room = roomRepository.getRoom(request.roomId());
Member member = memberRepository.getMember(request.memberId());
RoomOrder roomOrder = orderRepository.save(request.toEntity(room, member));
Order order = orderRepository.save(request.toEntity(room, member));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kr.co.fastcampus.yanabada.domain.product.controller;

import jakarta.validation.Valid;
import kr.co.fastcampus.yanabada.common.response.ResponseBody;
import kr.co.fastcampus.yanabada.domain.product.dto.request.ProductSaveRequest;
import kr.co.fastcampus.yanabada.domain.product.dto.response.ProductIdResponse;
import kr.co.fastcampus.yanabada.domain.product.service.ProductService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/products")
public class ProductController {

private final ProductService productService;

@PostMapping
public ResponseBody<ProductIdResponse> addProduct(
@RequestBody @Valid ProductSaveRequest request
) {
return ResponseBody.ok(
productService.saveProduct(1L, request)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package kr.co.fastcampus.yanabada.domain.product.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;
import kr.co.fastcampus.yanabada.domain.order.entity.Order;
import kr.co.fastcampus.yanabada.domain.product.entity.Product;
import kr.co.fastcampus.yanabada.domain.product.entity.enums.ProductStatus;

public record ProductSaveRequest(

@NotNull
Long orderId,

@NotNull
Integer price,

@NotBlank
String description,

@NotNull
Boolean canNegotiate,

@NotNull
LocalDate saleEndDate,

@NotNull
Boolean isAutoCancel
) {

public Product toEntity(
Order order
) {
return Product.create(
order,
price,
description,
canNegotiate,
saleEndDate,
isAutoCancel,
ProductStatus.ON_SALE
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package kr.co.fastcampus.yanabada.domain.product.dto.response;

import kr.co.fastcampus.yanabada.domain.product.entity.Product;
import lombok.Builder;

@Builder
public record ProductIdResponse(
Long productId
) {

public static ProductIdResponse from(Product product) {
return ProductIdResponse.builder()
.productId(product.getId())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.time.LocalDate;
import kr.co.fastcampus.yanabada.domain.order.entity.RoomOrder;
import kr.co.fastcampus.yanabada.domain.order.entity.Order;
import kr.co.fastcampus.yanabada.domain.product.entity.enums.ProductStatus;
import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -30,7 +30,7 @@ public class Product {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "room_order_id")
private RoomOrder roomOrder;
private Order order;

@Column(nullable = false)
private Integer price;
Expand All @@ -52,15 +52,15 @@ public class Product {
private ProductStatus status;
Hwang-Kyu-Cheol marked this conversation as resolved.
Show resolved Hide resolved

private Product(
RoomOrder roomOrder,
Order order,
Integer price,
String description,
Boolean canNegotiate,
LocalDate saleEndDate,
Boolean isAutoCancel,
ProductStatus status
) {
this.roomOrder = roomOrder;
this.order = order;
this.price = price;
this.description = description;
this.canNegotiate = canNegotiate;
Expand All @@ -70,7 +70,7 @@ private Product(
}

public static Product create(
RoomOrder roomOrder,
Order order,
Integer price,
String description,
Boolean canNegotiate,
Expand All @@ -79,7 +79,7 @@ public static Product create(
ProductStatus status
) {
return new Product(
roomOrder,
order,
price,
description,
canNegotiate,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kr.co.fastcampus.yanabada.domain.product.repository;

import kr.co.fastcampus.yanabada.domain.product.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ProductRepository extends JpaRepository<Product, Long> {

}
Loading
Loading