source: src/main/java/com/example/autopartz/service/impl/PartServiceImpl.java

main
Last change on this file was 2a552fe, checked in by andrejtodorovski <82031894+andrejtodorovski@…>, 16 months ago

Added transactional methods to secure the database integrity and refactoring

  • Property mode set to 100644
File size: 3.7 KB
Line 
1package com.example.autopartz.service.impl;
2
3import com.example.autopartz.model.Car;
4import com.example.autopartz.model.Category;
5import com.example.autopartz.model.Part;
6import com.example.autopartz.model.Price;
7import com.example.autopartz.model.manytomany.PartIsAppropriateForCar;
8import com.example.autopartz.model.manytomany.PartIsFromCategory;
9import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse;
10import com.example.autopartz.model.manytomany.PartIsInStockInWarehouseId;
11import com.example.autopartz.repository.*;
12import com.example.autopartz.service.PartService;
13import com.example.autopartz.service.PriceService;
14import org.springframework.stereotype.Service;
15
16import javax.transaction.Transactional;
17import java.time.LocalDate;
18import java.util.List;
19
20@Service
21public 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}
Note: See TracBrowser for help on using the repository browser.