-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/com/kusitms/jipbap/food/CategoryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/main/java/com/kusitms/jipbap/food/FoodExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/kusitms/jipbap/food/dto/RegisterFoodRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/kusitms/jipbap/food/exception/CategoryNotExistsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |