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/order #23

Merged
merged 3 commits into from
Nov 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public enum ErrorCode {
STORE_ALREADY_EXISTS_ERROR(false, HttpStatus.BAD_REQUEST.value(), "์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฐ€๊ฒŒ์ž…๋‹ˆ๋‹ค."),

//food
CATEGORY_NOT_FOUND_ERROR(false, HttpStatus.BAD_REQUEST.value(), "์กด์žฌํ•˜์ง€ ์•Š๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์ž…๋‹ˆ๋‹ค.")
CATEGORY_NOT_FOUND_ERROR(false, HttpStatus.BAD_REQUEST.value(), "์กด์žฌํ•˜์ง€ ์•Š๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์ž…๋‹ˆ๋‹ค."),
FOOD_NOT_FOUND_ERROR(false, HttpStatus.BAD_REQUEST.value(), "์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฉ”๋‰ด์ž…๋‹ˆ๋‹ค."),
;

private Boolean isSuccess;
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/com/kusitms/jipbap/food/FoodController.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.kusitms.jipbap.food;

import com.kusitms.jipbap.common.response.CommonResponse;
import com.kusitms.jipbap.food.dto.CategoryDto;
import com.kusitms.jipbap.food.dto.FoodDto;
import com.kusitms.jipbap.food.dto.RegisterCategoryRequestDto;
import com.kusitms.jipbap.food.dto.RegisterFoodRequestDto;
import com.kusitms.jipbap.security.Auth;
import com.kusitms.jipbap.security.AuthInfo;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/food")
Expand All @@ -18,10 +19,24 @@ public class FoodController {

private final FoodService foodService;

@Operation(summary = "์Œ์‹ ๋“ฑ๋กํ•˜๊ธฐ")
@Operation(summary = "์นดํ…Œ๊ณ ๋ฆฌ ๋“ฑ๋กํ•˜๊ธฐ")
@PostMapping("/category")
@ResponseStatus(HttpStatus.CREATED)
public CommonResponse<CategoryDto> registerCategory(@RequestBody RegisterCategoryRequestDto dto) {
return new CommonResponse<>(foodService.registerCategory(dto));
}

@Operation(summary = "๋ฉ”๋‰ด ๋“ฑ๋กํ•˜๊ธฐ")
@PostMapping
public CommonResponse<FoodDto> registerFood(@Auth AuthInfo authInfo, RegisterFoodRequestDto dto) {
@ResponseStatus(HttpStatus.CREATED)
public CommonResponse<FoodDto> registerFood(@Auth AuthInfo authInfo, @RequestBody RegisterFoodRequestDto dto) {
return new CommonResponse<>(foodService.registerFood(authInfo.getEmail(), dto));
}

@Operation(summary = "๋ฉ”๋‰ด ํ•˜๋‚˜ ์ƒ์„ธ์กฐํšŒ")
@GetMapping("/{foodId}")
@ResponseStatus(HttpStatus.OK)
public CommonResponse<FoodDto> getFoodDetail(@PathVariable Long foodId) {
return new CommonResponse<>(foodService.getFoodDetail(foodId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.kusitms.jipbap.common.response.CommonResponse;
import com.kusitms.jipbap.common.response.ErrorCode;
import com.kusitms.jipbap.food.exception.CategoryNotExistsException;
import com.kusitms.jipbap.food.exception.FoodNotExistsException;
import com.kusitms.jipbap.store.exception.StoreExistsException;
import com.kusitms.jipbap.store.exception.StoreNotExistsException;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -22,4 +23,11 @@ public CommonResponse<?> handleCategoryNotExistsException(CategoryNotExistsExcep
return new CommonResponse<>(ErrorCode.CATEGORY_NOT_FOUND_ERROR);
}

@ExceptionHandler(FoodNotExistsException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public CommonResponse<?> handelFoodNotExistsException(FoodNotExistsException e, HttpServletRequest request) {
log.warn("FOOD-002> ์š”์ฒญ URI: " + request.getRequestURI() + ", ์—๋Ÿฌ ๋ฉ”์„ธ์ง€: " + e.getMessage());
return new CommonResponse<>(ErrorCode.FOOD_NOT_FOUND_ERROR);
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/kusitms/jipbap/food/FoodService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.kusitms.jipbap.food;

import com.kusitms.jipbap.food.dto.CategoryDto;
import com.kusitms.jipbap.food.dto.FoodDto;
import com.kusitms.jipbap.food.dto.RegisterCategoryRequestDto;
import com.kusitms.jipbap.food.dto.RegisterFoodRequestDto;
import com.kusitms.jipbap.food.exception.CategoryNotExistsException;
import com.kusitms.jipbap.food.exception.FoodNotExistsException;
import com.kusitms.jipbap.store.Store;
import com.kusitms.jipbap.store.StoreRepository;
import com.kusitms.jipbap.store.exception.StoreNotExistsException;
Expand All @@ -24,6 +27,14 @@ public class FoodService {
private final FoodRepository foodRepository;
private final CategoryRepository categoryRepository;

@Transactional
public CategoryDto registerCategory(RegisterCategoryRequestDto dto) {
Category category = categoryRepository.save(
new Category(null, dto.getName(), dto.getImage())
);
return new CategoryDto(category.getId(), category.getName(), category.getImage());
}

@Transactional
public FoodDto registerFood(String email, RegisterFoodRequestDto dto) {
userRepository.findByEmail(email).orElseThrow(()-> new UserNotFoundException("์œ ์ € ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));
Expand All @@ -35,4 +46,9 @@ public FoodDto registerFood(String email, RegisterFoodRequestDto dto) {
);
return new FoodDto(food.getId(), store.getId(), category.getId(), food.getName(), food.getPrice(), food.getDescription());
}

public FoodDto getFoodDetail(Long foodId) {
Food food = foodRepository.findById(foodId).orElseThrow(()-> new FoodNotExistsException("ํ•ด๋‹น ์Œ์‹ Id๋Š” ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));
return new FoodDto(food.getId(), food.getStore().getId(), food.getCategory().getId(), food.getName(), food.getPrice(), food.getDescription());
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/kusitms/jipbap/food/dto/CategoryDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.kusitms.jipbap.food.dto;

import jakarta.persistence.Column;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class CategoryDto {

private Long id;
private String name;
private String image;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kusitms.jipbap.food.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class RegisterCategoryRequestDto {
private String name;
private String image;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.kusitms.jipbap.food.exception;

public class FoodNotExistsException extends RuntimeException {
public FoodNotExistsException(String message) {
super(message);
}
}
2 changes: 0 additions & 2 deletions src/main/java/com/kusitms/jipbap/order/Order.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.kusitms.jipbap.order;

import com.kusitms.jipbap.common.entity.DateEntity;
import com.kusitms.jipbap.food.Category;
import com.kusitms.jipbap.food.Food;
import com.kusitms.jipbap.store.Store;
import com.kusitms.jipbap.user.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/kusitms/jipbap/order/OrderController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.kusitms.jipbap.order;

import com.kusitms.jipbap.common.response.CommonResponse;
import com.kusitms.jipbap.food.dto.CategoryDto;
import com.kusitms.jipbap.food.dto.RegisterCategoryRequestDto;
import com.kusitms.jipbap.order.dto.OrderDto;
import com.kusitms.jipbap.order.dto.OrderFoodRequestDto;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

private final OrderService orderService;

@Operation(summary = "์Œ์‹ ์ฃผ๋ฌธํ•˜๊ธฐ")
@PostMapping("/food")
@ResponseStatus(HttpStatus.CREATED)
public CommonResponse<OrderDto> orderFood(@Valid @RequestBody OrderFoodRequestDto dto) {
return new CommonResponse<>(orderService.orderFood(dto));
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/kusitms/jipbap/order/OrderRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.kusitms.jipbap.order;

import com.kusitms.jipbap.food.Food;
import org.springframework.data.jpa.repository.JpaRepository;

public interface OrderRepository extends JpaRepository <Order, Long> {
}
36 changes: 36 additions & 0 deletions src/main/java/com/kusitms/jipbap/order/OrderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.kusitms.jipbap.order;

import com.kusitms.jipbap.food.Category;
import com.kusitms.jipbap.food.Food;
import com.kusitms.jipbap.food.FoodRepository;
import com.kusitms.jipbap.food.dto.CategoryDto;
import com.kusitms.jipbap.food.exception.FoodNotExistsException;
import com.kusitms.jipbap.order.dto.OrderDto;
import com.kusitms.jipbap.order.dto.OrderFoodRequestDto;
import com.kusitms.jipbap.user.User;
import com.kusitms.jipbap.user.UserRepository;
import com.kusitms.jipbap.user.exception.UserNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class OrderService {

private final OrderRepository orderRepository;
private final UserRepository userRepository;
private final FoodRepository foodRepository;

public OrderDto orderFood(OrderFoodRequestDto dto) {
User user = userRepository.findById(dto.getUser()).orElseThrow(()-> new UserNotFoundException("์œ ์ € ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));
Food food = foodRepository.findById(dto.getFood()).orElseThrow(()-> new FoodNotExistsException("ํ•ด๋‹น ์Œ์‹์€ ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));

Order order = orderRepository.save(
new Order(null, user, food, dto.getOrderCount())
);

return new OrderDto(order.getId(), user.getId(), food.getId(), order.getOrderCount());
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/kusitms/jipbap/order/dto/OrderDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kusitms.jipbap.order.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class OrderDto {

private Long id;

private Long user;

private Long food;

private Long orderCount;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.kusitms.jipbap.order.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class OrderFoodRequestDto {
private Long user;

private Long food;

private Long orderCount;
}
4 changes: 3 additions & 1 deletion src/main/java/com/kusitms/jipbap/store/StoreController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
Expand All @@ -26,7 +27,8 @@ public class StoreController {

@Operation(summary = "๊ฐ€๊ฒŒ ๋“ฑ๋กํ•˜๊ธฐ")
@PostMapping
public CommonResponse<StoreDto> registerStore(@Auth AuthInfo authInfo, @Valid @RequestBody RegisterStoreRequestDto dto) {
@ResponseStatus(HttpStatus.CREATED)
public CommonResponse<StoreDto> registerStore(@Auth AuthInfo authInfo, @Valid @RequestBody RegisterStoreRequestDto dto){
return new CommonResponse<>(storeService.registerStore(authInfo.getEmail(), dto));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.kusitms.jipbap.store.dto;

import lombok.Getter;
import lombok.Setter;
import lombok.*;

@Getter
@Setter
@AllArgsConstructor
public class RegisterStoreRequestDto {

private String name;
Expand Down
Loading