source: src/main/java/com/example/autopartz/service/impl/RepairShopServiceImpl.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: 1.8 KB
Line 
1package com.example.autopartz.service.impl;
2
3import com.example.autopartz.model.RepairShop;
4import com.example.autopartz.model.manytomany.RsForCm;
5import com.example.autopartz.repository.CarManufacturerRepository;
6import com.example.autopartz.repository.RepairShopRepository;
7import com.example.autopartz.repository.RsForCmRepository;
8import com.example.autopartz.service.RepairShopService;
9import org.springframework.stereotype.Service;
10
11import javax.transaction.Transactional;
12import java.util.List;
13
14@Service
15public class RepairShopServiceImpl implements RepairShopService {
16 private final RepairShopRepository repairShopRepository;
17 private final CarManufacturerRepository carManufacturerRepository;
18 private final RsForCmRepository rsForCmRepository;
19
20 public RepairShopServiceImpl(RepairShopRepository repairShopRepository, CarManufacturerRepository carManufacturerRepository, RsForCmRepository rsForCmRepository) {
21 this.repairShopRepository = repairShopRepository;
22 this.carManufacturerRepository = carManufacturerRepository;
23 this.rsForCmRepository = rsForCmRepository;
24 }
25
26 @Override
27 public List<RepairShop> findAll() {
28 return repairShopRepository.findAll();
29 }
30
31 @Override
32 public RepairShop getByName(String name) {
33 return repairShopRepository.getRepairShopByName(name);
34 }
35
36 @Override
37 public RepairShop getById(Integer id) {
38 return repairShopRepository.findById(id).get();
39 }
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 }
49}
Note: See TracBrowser for help on using the repository browser.