Ignore:
Timestamp:
07/26/23 19:26:35 (12 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/controller/AdminController.java

    re02787e r2a552fe  
    22
    33import com.example.autopartz.model.*;
    4 import com.example.autopartz.model.manytomany.PartIsAppropriateForCar;
    5 import com.example.autopartz.model.manytomany.PartIsFromCategory;
    6 import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse;
    7 import com.example.autopartz.model.manytomany.RsForCm;
    84import com.example.autopartz.repository.*;
    9 import com.example.autopartz.service.PriceService;
     5import com.example.autopartz.service.PartService;
     6import com.example.autopartz.service.RepairShopService;
    107import com.example.autopartz.service.UserService;
    118import org.springframework.stereotype.Controller;
     
    2421    private final UserService userService;
    2522    private final CarManufacturerRepository carManufacturerRepository;
    26     private final PartIsFromCategoryRepository partIsFromCategoryRepository;
    27     private final PartIsAppropriateForCarRepository partIsAppropriateForCarRepository;
    2823    private final WarehousemanRepository warehousemanRepository;
    29     private final PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository;
    3024    private final DeliverymanRepository deliverymanRepository;
    3125    private final CategoryRepository categoryRepository;
    32     private final PartRepository partRepository;
    3326    private final WarehouseRepository warehouseRepository;
    3427    private final CarRepository carRepository;
    3528    private final PartManufacturerRepository partManufacturerRepository;
    36     private final PriceService priceService;
    37     private final RepairShopRepository repairShopRepository;
    38     private final RsForCmRepository rsForCmRepository;
     29    private final PartService partService;
     30    private final RepairShopService repairShopService;
    3931
    40     public AdminController(UserService userService, CarManufacturerRepository carManufacturerRepository, PartIsFromCategoryRepository partIsFromCategoryRepository, PartIsAppropriateForCarRepository partIsAppropriateForCarRepository, WarehousemanRepository warehousemanRepository, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository, DeliverymanRepository deliverymanRepository, CategoryRepository categoryRepository, PartRepository partRepository, WarehouseRepository warehouseRepository, CarRepository carRepository, PartManufacturerRepository partManufacturerRepository, PriceService priceService, RepairShopRepository repairShopRepository, RsForCmRepository rsForCmRepository) {
     32    public AdminController(UserService userService, CarManufacturerRepository carManufacturerRepository, WarehousemanRepository warehousemanRepository, DeliverymanRepository deliverymanRepository, CategoryRepository categoryRepository, WarehouseRepository warehouseRepository, CarRepository carRepository, PartManufacturerRepository partManufacturerRepository, PartService partService, RepairShopService repairShopService) {
    4133        this.userService = userService;
    4234        this.carManufacturerRepository = carManufacturerRepository;
    43         this.partIsFromCategoryRepository = partIsFromCategoryRepository;
    44         this.partIsAppropriateForCarRepository = partIsAppropriateForCarRepository;
    4535        this.warehousemanRepository = warehousemanRepository;
    46         this.partIsInStockInWarehouseRepository = partIsInStockInWarehouseRepository;
    4736        this.deliverymanRepository = deliverymanRepository;
    4837        this.categoryRepository = categoryRepository;
    49         this.partRepository = partRepository;
    5038        this.warehouseRepository = warehouseRepository;
    5139        this.carRepository = carRepository;
    5240        this.partManufacturerRepository = partManufacturerRepository;
    53         this.priceService = priceService;
    54         this.repairShopRepository = repairShopRepository;
    55         this.rsForCmRepository = rsForCmRepository;
     41        this.partService = partService;
     42        this.repairShopService = repairShopService;
    5643    }
    5744
     
    10794                        @RequestParam List<Category> categories, @RequestParam Integer warehouse,
    10895                        @RequestParam Integer quantity, @RequestParam Integer amount, HttpServletResponse response){
    109         // Part(String name, String description, PartManufacturer manufacturer, List<Category> categoryList, List<Warehouse> warehouseList, List<Car> carList) {
    110         Part newPart = new Part(name, description==null ? "" : description, partManufacturerRepository.findById(manufacturer).get(),
    111                 categories, List.of(warehouseRepository.findById(warehouse).get()),cars);
    112         partRepository.save(newPart);
    113         priceService.save(new Price(amount, LocalDate.now(),newPart));
    114         partIsInStockInWarehouseRepository.save(new PartIsInStockInWarehouse(newPart.getId(),warehouse,quantity));
    115         for (Category c:categories
    116              ) {
    117             partIsFromCategoryRepository.save(new PartIsFromCategory(newPart.getId(),c.getId()));
    118         }
    119         for (Car car:cars){
    120             partIsAppropriateForCarRepository.save(new PartIsAppropriateForCar(newPart.getId(),car.getId()));
    121         }
     96        partService.addPart(name,description,manufacturer,cars,categories,warehouse,quantity,amount);
    12297        try {
    12398            response.sendRedirect("/");
     
    133108    @PostMapping("/addCarManufacturer")
    134109    public void saveCarManufacturer(@RequestParam String name,@RequestParam String location,
    135                                     Model model, HttpServletResponse response) {
     110                                    HttpServletResponse response) {
    136111        carManufacturerRepository.save(new CarManufacturer(name,location));
    137112        try {
     
    148123    @PostMapping("/addPartManufacturer")
    149124    public void savePartManufacturer(@RequestParam String name,@RequestParam String location,
    150                                     Model model, HttpServletResponse response) {
     125                                    HttpServletResponse response) {
    151126        partManufacturerRepository.save(new PartManufacturer(name,location));
    152127        try {
     
    163138    @PostMapping("/addCategory")
    164139    public void saveCategory(@RequestParam String name,
    165                                     Model model, HttpServletResponse response) {
     140                             HttpServletResponse response) {
    166141        categoryRepository.save(new Category(name));
    167142        try {
     
    198173                        @RequestParam String number,@RequestParam Integer carMId,
    199174                        HttpServletResponse response) {
    200         RepairShop newRs = new RepairShop(name,location,number,
    201                 List.of(carManufacturerRepository.findById(carMId).get()));
    202         repairShopRepository.save(newRs);
    203         rsForCmRepository.save(new RsForCm(newRs.getId(), carMId));
     175        repairShopService.save(name,location,number,carMId);
    204176        try {
    205177            response.sendRedirect("/");
Note: See TracChangeset for help on using the changeset viewer.