1 | package com.example.autopartz.service.impl;
|
---|
2 |
|
---|
3 | import com.example.autopartz.model.*;
|
---|
4 | import com.example.autopartz.repository.CarRepository;
|
---|
5 | import com.example.autopartz.repository.CarSampleRepository;
|
---|
6 | import com.example.autopartz.repository.ServiceBookRepository;
|
---|
7 | import com.example.autopartz.repository.UserRepository;
|
---|
8 | import com.example.autopartz.service.UserService;
|
---|
9 | import org.springframework.stereotype.Service;
|
---|
10 |
|
---|
11 | import javax.transaction.Transactional;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | @Service
|
---|
15 | public 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 | }
|
---|