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] 음식 등록 기능 추가 #18

Merged
merged 1 commit into from
Nov 10, 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 @@ -28,7 +28,10 @@ public enum ErrorCode {

//store
STORE_NOT_FOUND_ERROR(false, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 가게입니다."),
STORE_ALREADY_EXISTS_ERROR(false, HttpStatus.BAD_REQUEST.value(), "이미 채팅방을 만든 유저입니다.")
STORE_ALREADY_EXISTS_ERROR(false, HttpStatus.BAD_REQUEST.value(), "이미 존재하는 가게입니다."),

//food
CATEGORY_NOT_FOUND_ERROR(false, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 카테고리입니다.")
;

private Boolean isSuccess;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/kusitms/jipbap/food/CategoryRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kusitms.jipbap.food;

import org.springframework.data.jpa.repository.JpaRepository;

public interface CategoryRepository extends JpaRepository<Category, Long> {
}
27 changes: 27 additions & 0 deletions src/main/java/com/kusitms/jipbap/food/FoodController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.kusitms.jipbap.food;

import com.kusitms.jipbap.common.response.CommonResponse;
import com.kusitms.jipbap.food.dto.FoodDto;
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;

@RestController
@RequestMapping("/food")
@RequiredArgsConstructor
public class FoodController {

private final FoodService foodService;

@Operation(summary = "가게 등록하기")
@PostMapping
public CommonResponse<FoodDto> registerFood(@Auth AuthInfo authInfo, RegisterFoodRequestDto dto) {
return new CommonResponse<>(foodService.registerFood(authInfo.getEmail(), dto));
}

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

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.store.exception.StoreExistsException;
import com.kusitms.jipbap.store.exception.StoreNotExistsException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class FoodExceptionHandler {
@ExceptionHandler(CategoryNotExistsException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public CommonResponse<?> handleCategoryNotExistsException(CategoryNotExistsException e, HttpServletRequest request) {
log.warn("FOOD-001> 요청 URI: " + request.getRequestURI() + ", 에러 메세지: " + e.getMessage());
return new CommonResponse<>(ErrorCode.CATEGORY_NOT_FOUND_ERROR);
}

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

import com.kusitms.jipbap.store.Store;
import com.kusitms.jipbap.store.StoreRepositoryExtension;
import com.kusitms.jipbap.user.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface FoodRepository extends JpaRepository<Food, Long> {
}
38 changes: 38 additions & 0 deletions src/main/java/com/kusitms/jipbap/food/FoodService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.kusitms.jipbap.food;

import com.kusitms.jipbap.food.dto.FoodDto;
import com.kusitms.jipbap.food.dto.RegisterFoodRequestDto;
import com.kusitms.jipbap.food.exception.CategoryNotExistsException;
import com.kusitms.jipbap.store.Store;
import com.kusitms.jipbap.store.StoreRepository;
import com.kusitms.jipbap.store.exception.StoreNotExistsException;
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;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
public class FoodService {

private final UserRepository userRepository;
private final StoreRepository storeRepository;
private final FoodRepository foodRepository;
private final CategoryRepository categoryRepository;

@Transactional
public FoodDto registerFood(String email, RegisterFoodRequestDto dto) {
userRepository.findByEmail(email).orElseThrow(()-> new UserNotFoundException("유저 정보가 존재하지 않습니다."));
Store store = storeRepository.findById(dto.getStoreId()).orElseThrow(()-> new StoreNotExistsException("해당 가게 id는 유효하지 않습니다."));
Category category = categoryRepository.findById(dto.getCategoryId()).orElseThrow(()-> new CategoryNotExistsException("해당 카테고리 id는 유효하지 않습니다."));

Food food = foodRepository.save(
new Food(null, store, category, dto.getName(), dto.getPrice(), dto.getDescription(), 0L)
);
return new FoodDto(food.getId(), store.getId(), category.getId(), food.getName(), food.getPrice(), food.getDescription());
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/kusitms/jipbap/food/dto/FoodDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.kusitms.jipbap.food.dto;

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

@Getter
@Setter
@AllArgsConstructor
public class FoodDto {

private Long id;
private Long storeId;
private Long categoryId;
private String name;
private Long price;
private String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kusitms.jipbap.food.dto;

import com.kusitms.jipbap.food.Category;
import com.kusitms.jipbap.store.Store;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class RegisterFoodRequestDto {

private Long storeId;
private Long categoryId;
private String name;
private Long price;
private String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.kusitms.jipbap.food.exception;

public class CategoryNotExistsException extends RuntimeException{
public CategoryNotExistsException(String message) {
super(message);
}
}
Loading