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/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.