1 | package com.example.autopartz.service.impl;
|
---|
2 |
|
---|
3 | import com.example.autopartz.model.Car;
|
---|
4 | import com.example.autopartz.model.Category;
|
---|
5 | import com.example.autopartz.model.Part;
|
---|
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.*;
|
---|
12 | import com.example.autopartz.service.PartService;
|
---|
13 | import com.example.autopartz.service.PriceService;
|
---|
14 | import org.springframework.stereotype.Service;
|
---|
15 |
|
---|
16 | import javax.transaction.Transactional;
|
---|
17 | import java.time.LocalDate;
|
---|
18 | import java.util.List;
|
---|
19 |
|
---|
20 | @Service
|
---|
21 | public class PartServiceImpl implements PartService {
|
---|
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;
|
---|
29 |
|
---|
30 | public PartServiceImpl(PartRepository partRepository, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository, PartIsFromCategoryRepository partIsFromCategoryRepository, PartIsAppropriateForCarRepository partIsAppropriateForCarRepository, WarehouseRepository warehouseRepository, PartManufacturerRepository partManufacturerRepository, PriceService priceService) {
|
---|
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;
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public List<Part> findAll() {
|
---|
42 | return partRepository.findAll();
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public Part findById(Integer id) {
|
---|
47 | return partRepository.findById(id).orElseThrow(RuntimeException::new);
|
---|
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 | }
|
---|
75 | }
|
---|