source: src/main/java/com/example/autopartz/service/impl/UserServiceImpl.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: 2.0 KB
Line 
1package com.example.autopartz.service.impl;
2
3import com.example.autopartz.model.*;
4import com.example.autopartz.repository.CarRepository;
5import com.example.autopartz.repository.CarSampleRepository;
6import com.example.autopartz.repository.ServiceBookRepository;
7import com.example.autopartz.repository.UserRepository;
8import com.example.autopartz.service.UserService;
9import org.springframework.stereotype.Service;
10
11import javax.transaction.Transactional;
12import java.util.List;
13
14@Service
15public class UserServiceImpl implements UserService {
16 private final UserRepository userRepository;
17 private final CarRepository carRepository;
18 private final CarSampleRepository carSampleRepository;
19 private final ServiceBookRepository serviceBookRepository;
20
21 public UserServiceImpl(UserRepository userRepository, CarRepository carRepository, CarSampleRepository carSampleRepository, ServiceBookRepository serviceBookRepository) {
22 this.userRepository = userRepository;
23 this.carRepository = carRepository;
24 this.carSampleRepository = carSampleRepository;
25 this.serviceBookRepository = serviceBookRepository;
26 }
27
28 @Override
29 public List<User> findAllUsers() {
30 return userRepository.findAll();
31 }
32
33 @Override
34 public User findByUsername(String username) {
35 return userRepository.findAllByUsername(username).stream().findFirst().orElseThrow(RuntimeException::new);
36 }
37
38 @Override
39 public User findById(Integer id) {
40 return userRepository.findById(id).get();
41 }
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 }
50}
Note: See TracBrowser for help on using the repository browser.