1 | package com.example.rezevirajmasa.demo.service.impl;
|
---|
2 |
|
---|
3 | import com.example.rezevirajmasa.demo.model.Menu;
|
---|
4 | import com.example.rezevirajmasa.demo.model.PriceHistory;
|
---|
5 | import com.example.rezevirajmasa.demo.model.Restaurant;
|
---|
6 | import com.example.rezevirajmasa.demo.repository.MenuRepository;
|
---|
7 | import com.example.rezevirajmasa.demo.repository.PriceHistoryRepository;
|
---|
8 | import com.example.rezevirajmasa.demo.service.MenuService;
|
---|
9 | import com.example.rezevirajmasa.demo.service.RestaurantService;
|
---|
10 | import org.openqa.selenium.InvalidArgumentException;
|
---|
11 | import org.springframework.stereotype.Service;
|
---|
12 |
|
---|
13 | import java.math.BigDecimal;
|
---|
14 | import java.time.LocalDateTime;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | @Service
|
---|
18 | public class MenuServiceImpl implements MenuService {
|
---|
19 | private final MenuRepository menuRepository;
|
---|
20 | private final RestaurantService restaurantService;
|
---|
21 |
|
---|
22 | private final PriceHistoryRepository priceHistoryRepository;
|
---|
23 |
|
---|
24 | public MenuServiceImpl(MenuRepository menuRepository, RestaurantService restaurantService, PriceHistoryRepository priceHistoryRepository) {
|
---|
25 | this.menuRepository = menuRepository;
|
---|
26 | this.restaurantService = restaurantService;
|
---|
27 | this.priceHistoryRepository = priceHistoryRepository;
|
---|
28 | }
|
---|
29 |
|
---|
30 | @Override
|
---|
31 | public List<Menu> getMenuByRestaurantId(Long restaurantId) {
|
---|
32 | Restaurant restaurant = restaurantService.findByIdRestaurant(restaurantId);
|
---|
33 | return menuRepository.findAllByRestaurant(restaurant);
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public void updateMenuPrice(Long menuId, BigDecimal newPrice) {
|
---|
38 | Menu menu = menuRepository.findById(menuId)
|
---|
39 | .orElseThrow(() -> new IllegalArgumentException("Menu not found"));
|
---|
40 |
|
---|
41 | if (menu.getPrice() != null && !menu.getPrice().equals(newPrice)) {
|
---|
42 | PriceHistory priceHistory = new PriceHistory(menu, menu.getPrice(), LocalDateTime.now());
|
---|
43 | priceHistoryRepository.save(priceHistory);
|
---|
44 | }
|
---|
45 |
|
---|
46 | menu.setPrice(newPrice);
|
---|
47 | menuRepository.save(menu);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public Menu getMenuById(Long id) {
|
---|
52 | return menuRepository.findById(id)
|
---|
53 | .orElseThrow(()->new InvalidArgumentException("Invalid id sent: " + id));
|
---|
54 | }
|
---|
55 | }
|
---|