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

Location:
src/main/java/com/example/autopartz/service
Files:
8 edited

Legend:

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

    re02787e r2a552fe  
    88public interface OrderService {
    99    List<Order> findAll();
    10     Order create(Client user);
     10    Order createOrderAndAddPartToOrder(Client user, Integer partId, Integer quantity);
    1111    Order findById(Integer id);
    1212    void save(Order o);
  • src/main/java/com/example/autopartz/service/PartService.java

    re02787e r2a552fe  
    11package com.example.autopartz.service;
    22
     3import com.example.autopartz.model.Car;
     4import com.example.autopartz.model.Category;
    35import com.example.autopartz.model.Part;
    46
     
    79public interface PartService {
    810    List<Part> findAll();
     11
    912    Part findById(Integer id);
     13
     14    void addPartToWarehouse(Integer partId, Integer quantity, Integer warehouseId);
     15
     16    void addPart(String name, String description, Integer manufacturer, List<Car> cars, List<Category> categories, Integer warehouse, Integer quantity, Integer amount
     17    );
    1018}
  • src/main/java/com/example/autopartz/service/RepairShopService.java

    re02787e r2a552fe  
    99    RepairShop getByName(String name);
    1010    RepairShop getById(Integer id);
     11    void save(String name, String location, String number, Integer carMId);
    1112}
  • src/main/java/com/example/autopartz/service/UserService.java

    re02787e r2a552fe  
    77public interface UserService {
    88    List<User> findAllUsers();
     9
    910    User findByUsername(String username);
     11
    1012    User findById(Integer id);
     13
     14    void addCarSampleForUser(Integer vin, Integer year, Integer power, Integer displacement, String fuel, Integer km, Integer cartype, String username);
     15
    1116}
  • src/main/java/com/example/autopartz/service/impl/OrderServiceImpl.java

    re02787e r2a552fe  
    33import com.example.autopartz.model.Client;
    44import com.example.autopartz.model.Order;
     5import com.example.autopartz.model.manytomany.OrderContainsPart;
     6import com.example.autopartz.repository.OrderContainsPartRepository;
    57import com.example.autopartz.repository.OrderRepository;
    68import com.example.autopartz.service.OrderService;
    79import org.springframework.stereotype.Service;
    810
     11import javax.transaction.Transactional;
    912import java.util.List;
    1013@Service
    1114public class OrderServiceImpl implements OrderService {
    1215    private final OrderRepository orderRepository;
     16    private final OrderContainsPartRepository orderContainsPartRepository;
    1317
    14     public OrderServiceImpl(OrderRepository orderRepository) {
     18    public OrderServiceImpl(OrderRepository orderRepository, OrderContainsPartRepository orderContainsPartRepository) {
    1519        this.orderRepository = orderRepository;
     20        this.orderContainsPartRepository = orderContainsPartRepository;
    1621    }
    1722
     
    2227
    2328    @Override
    24     public Order create(Client user) {
    25         return orderRepository.save(new Order(user));
     29    @Transactional
     30    public Order createOrderAndAddPartToOrder(Client user, Integer partId, Integer quantity) {
     31        Order order = orderRepository.save(new Order(user));
     32        orderContainsPartRepository.save(new OrderContainsPart(partId, order.getOrderid(), quantity));
     33        return order;
    2634    }
    2735
  • 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}
  • src/main/java/com/example/autopartz/service/impl/RepairShopServiceImpl.java

    re02787e r2a552fe  
    22
    33import com.example.autopartz.model.RepairShop;
     4import com.example.autopartz.model.manytomany.RsForCm;
     5import com.example.autopartz.repository.CarManufacturerRepository;
    46import com.example.autopartz.repository.RepairShopRepository;
     7import com.example.autopartz.repository.RsForCmRepository;
    58import com.example.autopartz.service.RepairShopService;
    69import org.springframework.stereotype.Service;
    710
     11import javax.transaction.Transactional;
    812import java.util.List;
    913
     
    1115public class RepairShopServiceImpl implements RepairShopService {
    1216    private final RepairShopRepository repairShopRepository;
     17    private final CarManufacturerRepository carManufacturerRepository;
     18    private final RsForCmRepository rsForCmRepository;
    1319
    14     public RepairShopServiceImpl(RepairShopRepository repairShopRepository) {
     20    public RepairShopServiceImpl(RepairShopRepository repairShopRepository, CarManufacturerRepository carManufacturerRepository, RsForCmRepository rsForCmRepository) {
    1521        this.repairShopRepository = repairShopRepository;
     22        this.carManufacturerRepository = carManufacturerRepository;
     23        this.rsForCmRepository = rsForCmRepository;
    1624    }
    1725
     
    3038        return repairShopRepository.findById(id).get();
    3139    }
     40
     41    @Override
     42    @Transactional
     43    public void save(String name, String location, String number, Integer carMId) {
     44        RepairShop newRs = new RepairShop(name,location,number,
     45                List.of(carManufacturerRepository.findById(carMId).get()));
     46        repairShopRepository.save(newRs);
     47        rsForCmRepository.save(new RsForCm(newRs.getId(), carMId));
     48    }
    3249}
  • src/main/java/com/example/autopartz/service/impl/UserServiceImpl.java

    re02787e r2a552fe  
    11package com.example.autopartz.service.impl;
    22
    3 import com.example.autopartz.model.Role;
    4 import com.example.autopartz.model.User;
     3import com.example.autopartz.model.*;
     4import com.example.autopartz.repository.CarRepository;
     5import com.example.autopartz.repository.CarSampleRepository;
     6import com.example.autopartz.repository.ServiceBookRepository;
    57import com.example.autopartz.repository.UserRepository;
    68import com.example.autopartz.service.UserService;
    79import org.springframework.stereotype.Service;
    810
    9 import java.util.Collections;
     11import javax.transaction.Transactional;
    1012import java.util.List;
    1113
     
    1315public class UserServiceImpl implements UserService {
    1416    private final UserRepository userRepository;
     17    private final CarRepository carRepository;
     18    private final CarSampleRepository carSampleRepository;
     19    private final ServiceBookRepository serviceBookRepository;
    1520
    16     public UserServiceImpl(UserRepository userRepository) {
     21    public UserServiceImpl(UserRepository userRepository, CarRepository carRepository, CarSampleRepository carSampleRepository, ServiceBookRepository serviceBookRepository) {
    1722        this.userRepository = userRepository;
     23        this.carRepository = carRepository;
     24        this.carSampleRepository = carSampleRepository;
     25        this.serviceBookRepository = serviceBookRepository;
    1826    }
    1927
     
    3240        return userRepository.findById(id).get();
    3341    }
     42
     43    @Override
     44    @Transactional
     45    public void addCarSampleForUser(Integer vin, Integer year, Integer power, Integer displacement, String fuel, Integer km, Integer cartype, String username) {
     46        CarSample cs = new CarSample(vin, year, power, displacement, fuel, km, (Client) this.findByUsername(username), carRepository.findById(cartype).get());
     47        carSampleRepository.save(cs);
     48        serviceBookRepository.save(new ServiceBook(cs));
     49    }
    3450}
Note: See TracChangeset for help on using the changeset viewer.