source: src/main/java/com/example/rezevirajmasa/demo/service/impl/MenuServiceImpl.java

main
Last change on this file was e48199a, checked in by Aleksandar Panovski <apano77@…>, 11 days ago

Final version for DB

  • Property mode set to 100644
File size: 2.1 KB
Line 
1package com.example.rezevirajmasa.demo.service.impl;
2
3import com.example.rezevirajmasa.demo.model.Menu;
4import com.example.rezevirajmasa.demo.model.PriceHistory;
5import com.example.rezevirajmasa.demo.model.Restaurant;
6import com.example.rezevirajmasa.demo.repository.MenuRepository;
7import com.example.rezevirajmasa.demo.repository.PriceHistoryRepository;
8import com.example.rezevirajmasa.demo.service.MenuService;
9import com.example.rezevirajmasa.demo.service.RestaurantService;
10import org.openqa.selenium.InvalidArgumentException;
11import org.springframework.stereotype.Service;
12
13import java.math.BigDecimal;
14import java.time.LocalDateTime;
15import java.util.List;
16
17@Service
18public 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}
Note: See TracBrowser for help on using the repository browser.