Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/autopartz/service/impl/PartServiceImpl.java
re02787e r2a552fe 1 1 package com.example.autopartz.service.impl; 2 2 3 import com.example.autopartz.model.Car; 4 import com.example.autopartz.model.Category; 3 5 import com.example.autopartz.model.Part; 4 import com.example.autopartz.repository.PartRepository; 6 import com.example.autopartz.model.Price; 7 import com.example.autopartz.model.manytomany.PartIsAppropriateForCar; 8 import com.example.autopartz.model.manytomany.PartIsFromCategory; 9 import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse; 10 import com.example.autopartz.model.manytomany.PartIsInStockInWarehouseId; 11 import com.example.autopartz.repository.*; 5 12 import com.example.autopartz.service.PartService; 13 import com.example.autopartz.service.PriceService; 6 14 import org.springframework.stereotype.Service; 7 15 16 import javax.transaction.Transactional; 17 import java.time.LocalDate; 8 18 import java.util.List; 9 19 … … 11 21 public class PartServiceImpl implements PartService { 12 22 private final PartRepository partRepository; 23 private final PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository; 24 private final PartIsFromCategoryRepository partIsFromCategoryRepository; 25 private final PartIsAppropriateForCarRepository partIsAppropriateForCarRepository; 26 private final WarehouseRepository warehouseRepository; 27 private final PartManufacturerRepository partManufacturerRepository; 28 private final PriceService priceService; 13 29 14 public PartServiceImpl(PartRepository partRepository ) {30 public PartServiceImpl(PartRepository partRepository, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository, PartIsFromCategoryRepository partIsFromCategoryRepository, PartIsAppropriateForCarRepository partIsAppropriateForCarRepository, WarehouseRepository warehouseRepository, PartManufacturerRepository partManufacturerRepository, PriceService priceService) { 15 31 this.partRepository = partRepository; 32 this.partIsInStockInWarehouseRepository = partIsInStockInWarehouseRepository; 33 this.partIsFromCategoryRepository = partIsFromCategoryRepository; 34 this.partIsAppropriateForCarRepository = partIsAppropriateForCarRepository; 35 this.warehouseRepository = warehouseRepository; 36 this.partManufacturerRepository = partManufacturerRepository; 37 this.priceService = priceService; 16 38 } 17 39 … … 25 47 return partRepository.findById(id).orElseThrow(RuntimeException::new); 26 48 } 49 50 @Override 51 @Transactional 52 public void addPartToWarehouse(Integer partId, Integer quantity, Integer warehouseId) { 53 PartIsInStockInWarehouseId tmp = new PartIsInStockInWarehouseId(partId, warehouseId); 54 PartIsInStockInWarehouse temp = partIsInStockInWarehouseRepository.findById(tmp).get(); 55 temp.setQuantity(temp.getQuantity() + quantity); 56 partIsInStockInWarehouseRepository.save(temp); 57 } 58 59 @Override 60 @Transactional 61 public void addPart(String name, String description, Integer manufacturer, List<Car> cars, List<Category> categories, Integer warehouse, Integer quantity, Integer amount) { 62 Part newPart = new Part(name, description==null ? "" : description, partManufacturerRepository.findById(manufacturer).get(), 63 categories, List.of(warehouseRepository.findById(warehouse).get()),cars); 64 partRepository.save(newPart); 65 priceService.save(new Price(amount, LocalDate.now(),newPart)); 66 partIsInStockInWarehouseRepository.save(new PartIsInStockInWarehouse(newPart.getId(),warehouse,quantity)); 67 for (Category c:categories 68 ) { 69 partIsFromCategoryRepository.save(new PartIsFromCategory(newPart.getId(),c.getId())); 70 } 71 for (Car car:cars){ 72 partIsAppropriateForCarRepository.save(new PartIsAppropriateForCar(newPart.getId(),car.getId())); 73 } 74 } 27 75 }
Note:
See TracChangeset
for help on using the changeset viewer.