Ignore:
Timestamp:
07/26/23 19:26:35 (16 months ago)
Author:
andrejtodorovski <82031894+andrejtodorovski@…>
Branches:
main
Parents:
e02787e
Message:

Added transactional methods to secure the database integrity and refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/example/autopartz/service/impl/PartServiceImpl.java

    re02787e r2a552fe  
    11package com.example.autopartz.service.impl;
    22
     3import com.example.autopartz.model.Car;
     4import com.example.autopartz.model.Category;
    35import com.example.autopartz.model.Part;
    4 import com.example.autopartz.repository.PartRepository;
     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.*;
    512import com.example.autopartz.service.PartService;
     13import com.example.autopartz.service.PriceService;
    614import org.springframework.stereotype.Service;
    715
     16import javax.transaction.Transactional;
     17import java.time.LocalDate;
    818import java.util.List;
    919
     
    1121public class PartServiceImpl implements PartService {
    1222    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;
    1329
    14     public PartServiceImpl(PartRepository partRepository) {
     30    public PartServiceImpl(PartRepository partRepository, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository, PartIsFromCategoryRepository partIsFromCategoryRepository, PartIsAppropriateForCarRepository partIsAppropriateForCarRepository, WarehouseRepository warehouseRepository, PartManufacturerRepository partManufacturerRepository, PriceService priceService) {
    1531        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;
    1638    }
    1739
     
    2547        return partRepository.findById(id).orElseThrow(RuntimeException::new);
    2648    }
     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    }
    2775}
Note: See TracChangeset for help on using the changeset viewer.