Changeset 2a552fe for src/main/java/com


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
Files:
15 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("/");
  • src/main/java/com/example/autopartz/controller/HomeController.java

    re02787e r2a552fe  
    11package com.example.autopartz.controller;
    22
    3 import com.example.autopartz.model.*;
    43import com.example.autopartz.model.DTO.CurrentOrderDTO;
    54import com.example.autopartz.model.DTO.OrderInfo;
     5import com.example.autopartz.model.*;
    66import com.example.autopartz.model.manytomany.OrderContainsPart;
    77import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse;
    8 import com.example.autopartz.model.manytomany.PartIsInStockInWarehouseId;
    98import com.example.autopartz.model.views.DeliveriesInProgress;
    109import com.example.autopartz.model.views.PartsForCarTypeAndCategory;
     
    2019import java.io.IOException;
    2120import java.util.ArrayList;
    22 import java.util.Comparator;
    2321import java.util.List;
    2422import java.util.Objects;
     
    103101            int total = 0;
    104102            List<OrderContainsPart> qList = orderContainsPartRepository.findAllByOrderid(o.getOrderid());
    105             for (int i = 0; i < qList.size(); i++) {
    106                 int pr = qList.get(i).getQuantity_order()*
    107                         priceService.findPriceForPart(partService.findById(qList.get(i).getPartid())).stream().findFirst().get().getAmount();
     103            for (OrderContainsPart orderContainsPart : qList) {
     104                int pr = orderContainsPart.getQuantity_order() *
     105                        priceService.findPriceForPart(partService.findById(orderContainsPart.getPartid())).stream().findFirst().get().getAmount();
    108106                CurrentOrderDTO temp = new CurrentOrderDTO(
    109                         partService.findById(qList.get(i).getPartid()).getName(),
    110                         partService.findById(qList.get(i).getPartid()).getManufacturer().getName(),
    111                         qList.get(i).getQuantity_order(),
     107                        partService.findById(orderContainsPart.getPartid()).getName(),
     108                        partService.findById(orderContainsPart.getPartid()).getManufacturer().getName(),
     109                        orderContainsPart.getQuantity_order(),
    112110                        pr);
    113111                list.add(temp);
    114                 total+=pr;
     112                total += pr;
    115113            }
    116114            model.addAttribute("total",total);
     
    188186    }
    189187    @PostMapping("/finishRegister")
    190     public void handleWarehousemanRegister(@RequestParam String location,Model model, HttpServletResponse response, HttpSession session){
     188    public void handleWarehousemanRegister(@RequestParam String location, HttpServletResponse response, HttpSession session){
    191189        System.out.println("here?");
    192190        String username = (String) session.getAttribute("username");
     
    243241    }
    244242    @PostMapping("/finishDelivery/{id}")
    245     public void finishDelivery(@PathVariable Integer id, Model model, HttpServletResponse response){
     243    public void finishDelivery(@PathVariable Integer id, HttpServletResponse response){
    246244        Delivery d = deliveryService.findByOrder(orderService.findById(id));
    247245        d.setStatus("finished");
     
    257255        List<OrderContainsPart> list = orderContainsPartRepository.findAllByOrderid(id);
    258256        List<OrderInfo> partList = new ArrayList<>();
    259         for (int i = 0; i < list.size(); i++) {
    260             OrderInfo oi = new OrderInfo(partService.findById(list.get(i).getPartid()).getName(),
    261                     list.get(i).getQuantity_order(),partService.findById(list.get(i).getPartid()).getManufacturer().getName());
     257        for (OrderContainsPart orderContainsPart : list) {
     258            OrderInfo oi = new OrderInfo(partService.findById(orderContainsPart.getPartid()).getName(),
     259                    orderContainsPart.getQuantity_order(), partService.findById(orderContainsPart.getPartid()).getManufacturer().getName());
    262260            partList.add(oi);
    263261        }
     
    301299        Warehouse wh = whm.getWarehouse();
    302300        Integer whId = wh.getId();
    303         PartIsInStockInWarehouseId tmp = new PartIsInStockInWarehouseId(pId,whId);
    304         PartIsInStockInWarehouse temp = partIsInStockInWarehouseRepository.findById(tmp).get();
    305         temp.setQuantity(temp.getQuantity()+quantity);
    306         partIsInStockInWarehouseRepository.save(temp);
     301        partService.addPartToWarehouse(pId,quantity,whId);
    307302        try {
    308303            response.sendRedirect("/myWarehouseReport");
  • src/main/java/com/example/autopartz/controller/PartController.java

    re02787e r2a552fe  
    7171    }
    7272    @PostMapping("/repairshopdelivery")
    73     public void setRepairShopDelivery(@RequestParam Integer vin, HttpServletResponse response, HttpSession session, HttpServletRequest request){
     73    public void setRepairShopDelivery(@RequestParam Integer vin, HttpServletResponse response, HttpSession session){
    7474        // insert into project.repair (vin, id_repair_shop, id_service_book) values (1111,3,1)
    7575        session.setAttribute("carVin",vin);
     
    107107        RepairShop repairShop = repairShopService.getById(rs);
    108108        repairRepository.save(new Repair(o,repairShop,sb));
    109 //        o.setOrder_status("finished");
    110 //        orderService.save(o);
    111109        session.removeAttribute("order");
    112110        try {
     
    125123        Deliveryman dm = deliverymanList.get(deliverer);
    126124        deliveryRepository.save(new Delivery("in progress",address,dm,o));
    127 //        o.setOrder_status("finished");
    128 //        orderService.save(o);
    129125        session.removeAttribute("order");
    130126        try {
     
    138134        if(session.getAttribute("order")==null){
    139135            User u = userService.findByUsername(request.getRemoteUser());
    140             Order newOrder = orderService.create((Client) u);
     136            Order newOrder = orderService.createOrderAndAddPartToOrder((Client) u, id, quantity);
    141137            session.setAttribute("order",newOrder);
     138        } else {
     139            Order order = (Order) session.getAttribute("order");
     140            orderContainsPartRepository.save(new OrderContainsPart(id, order.getOrderid(), quantity));
    142141        }
    143         Order order = (Order) session.getAttribute("order");
    144         orderContainsPartRepository.save(new OrderContainsPart(id,order.getOrderid(),quantity));
    145142        try {
    146143            response.sendRedirect("/products");
  • src/main/java/com/example/autopartz/controller/UserController.java

    re02787e r2a552fe  
    2525    private final UserService userService;
    2626    private final CarService carService;
    27     private final CarSampleRepository carSampleRepository;
    28     private final ServiceBookRepository serviceBookRepository;
    2927    private final RepairService repairService;
    3028    private final ReviewRepository reviewRepository;
    31     private final RepairRepository repairRepository;
    3229    private final OrderRepository orderRepository;
    3330
    34     public UserController(OrdersForUserRepository ordersForUserRepository, RepairsForUserRepository repairsForUserRepository, ReviewsForUserRepository reviewsForUserRepository, UserService userService, CarService carService, CarSampleRepository carSampleRepository, ServiceBookRepository serviceBookRepository, RepairService repairService, ReviewRepository reviewRepository,
    35                           RepairRepository repairRepository,
     31    public UserController(OrdersForUserRepository ordersForUserRepository, RepairsForUserRepository repairsForUserRepository, ReviewsForUserRepository reviewsForUserRepository, UserService userService, CarService carService, RepairService repairService, ReviewRepository reviewRepository,
    3632                          OrderRepository orderRepository) {
    3733        this.ordersForUserRepository = ordersForUserRepository;
     
    4036        this.userService = userService;
    4137        this.carService = carService;
    42         this.carSampleRepository = carSampleRepository;
    43         this.serviceBookRepository = serviceBookRepository;
    4438        this.repairService = repairService;
    4539        this.reviewRepository = reviewRepository;
    46         this.repairRepository = repairRepository;
    4740        this.orderRepository = orderRepository;
    4841    }
     
    9285                                    @RequestParam Integer km, @RequestParam Integer cartype,
    9386                                    HttpServletRequest request, HttpServletResponse response){
    94         CarSample cs = new CarSample(vin,year,power,displacement,fuel,km, (Client) userService.findByUsername(request.getRemoteUser()),carService.findById(cartype));
    95         carSampleRepository.save(cs);
    96         serviceBookRepository.save(new ServiceBook(cs));
     87        userService.addCarSampleForUser(vin,year,power,displacement,fuel,km,cartype,request.getRemoteUser());
    9788        try {
    9889            response.sendRedirect("/");
     
    109100    @PostMapping("/repairs/addReview/{id}")
    110101    public void addReview(@PathVariable Integer id, @RequestParam Integer rating, @RequestParam(required = false) String comment,
    111                           Model model, HttpServletResponse response, HttpServletRequest request){
     102                          HttpServletResponse response, HttpServletRequest request){
    112103        Client client = (Client) userService.findByUsername(request.getRemoteUser());
    113104        Repair repair = repairService.findById(id);
  • src/main/java/com/example/autopartz/repository/OrdersForUserRepository.java

    re02787e r2a552fe  
    66import org.springframework.stereotype.Repository;
    77
    8 import javax.transaction.Transactional;
    98import java.util.List;
    109
    1110@Repository
    12 @Transactional
    1311public interface OrdersForUserRepository extends JpaRepository<OrdersForUser, OrdersForUserId> {
    1412    List<OrdersForUser> findAllByUserid(Integer userid);
  • src/main/java/com/example/autopartz/repository/PartsForCarTypeAndCategoryRepository.java

    re02787e r2a552fe  
    55import org.springframework.stereotype.Repository;
    66
    7 import javax.transaction.Transactional;
    87import java.util.List;
    98
    109@Repository
    11 @Transactional
    1210public interface PartsForCarTypeAndCategoryRepository extends JpaRepository<PartsForCarTypeAndCategory,Integer> {
    1311    List<PartsForCarTypeAndCategory> findAllByCartypeAndCategory(String cartype, String category);
  • src/main/java/com/example/autopartz/repository/RepairShopReviewSummaryRepository.java

    re02787e r2a552fe  
    55import org.springframework.stereotype.Repository;
    66
    7 import javax.transaction.Transactional;
    8 
    97// извештај за сите сервиси и за сите број на reviews и avg рејтинг
    108@Repository
    11 @Transactional
    129public interface RepairShopReviewSummaryRepository extends JpaRepository<RepairShopReviewsSummary,Integer> {
    13     // List<RepairShopReviewsSummary> findAllByRsid(Integer rsid);
    1410}
  • 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.