1 | package finki.diplomska.tripplanner.service.impl;
|
---|
2 |
|
---|
3 | import finki.diplomska.tripplanner.models.Location;
|
---|
4 | import finki.diplomska.tripplanner.models.Planner;
|
---|
5 | import finki.diplomska.tripplanner.models.User;
|
---|
6 | import finki.diplomska.tripplanner.models.dto.PlannerDto;
|
---|
7 | import finki.diplomska.tripplanner.models.exceptions.LocationNotFoundException;
|
---|
8 | import finki.diplomska.tripplanner.models.exceptions.PlannerNotFoundException;
|
---|
9 | import finki.diplomska.tripplanner.repository.jpa.JpaLocationRepository;
|
---|
10 | import finki.diplomska.tripplanner.repository.jpa.JpaPlannerRepository;
|
---|
11 | import finki.diplomska.tripplanner.repository.jpa.JpaUserRepository;
|
---|
12 | import finki.diplomska.tripplanner.service.PlannerService;
|
---|
13 | import org.springframework.stereotype.Service;
|
---|
14 |
|
---|
15 | import javax.jws.soap.SOAPBinding;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Optional;
|
---|
18 |
|
---|
19 | @Service
|
---|
20 | public class PlannerServiceImpl implements PlannerService {
|
---|
21 |
|
---|
22 | private final JpaPlannerRepository plannerRepository;
|
---|
23 | private final JpaLocationRepository locationRepository;
|
---|
24 | private final JpaUserRepository userRepository;
|
---|
25 |
|
---|
26 | public PlannerServiceImpl(JpaPlannerRepository plannerRepository, JpaLocationRepository locationRepository, JpaUserRepository userRepository) {
|
---|
27 | this.plannerRepository = plannerRepository;
|
---|
28 | this.locationRepository = locationRepository;
|
---|
29 | this.userRepository = userRepository;
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public List<Planner> getAllPlaners() {
|
---|
34 | return this.plannerRepository.findAll();
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public List<Planner> getPlannersByUser(String username) {
|
---|
39 | User user = this.userRepository.findByUsername(username);
|
---|
40 | return this.plannerRepository.getPlannersByUser(user.getUsername());
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public Optional<Planner> findById(Long id) {
|
---|
45 | return this.plannerRepository.findById(id);
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public Optional<Planner> newPlanner(PlannerDto plannerDto, String username) {
|
---|
51 | /*
|
---|
52 | List<Location> locationList = new ArrayList<>();
|
---|
53 | for(Long location : plannerDto.getLocationList()){
|
---|
54 | Location loc = this.locationRepository.findById(location)
|
---|
55 | .orElseThrow(() -> new LocationNotFoundException(location));
|
---|
56 | locationList.add(loc);
|
---|
57 | }
|
---|
58 | */
|
---|
59 | User user = this.userRepository.findByUsername(username);
|
---|
60 | plannerDto.setUser(user.getUsername());
|
---|
61 | return Optional.of(this.plannerRepository.save(new Planner(plannerDto.getName(), plannerDto.getDescription(), null, user)));
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public Planner createPlannerWithRequestParams(String plandesc,String planname, List<Location> locationList) {
|
---|
66 | Planner planner = Planner.createNewPlanner(plandesc, planname, locationList);
|
---|
67 | return this.plannerRepository.save(planner);
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public Planner editPlannerWithRequestParams(Long id, String description, String name, List<Location> locationList) {
|
---|
72 | Planner planner = this.plannerRepository.findById(id).orElseThrow(() -> new LocationNotFoundException(id));
|
---|
73 | planner.setDescription(description);
|
---|
74 | planner.setName(name);
|
---|
75 | planner.setLocationList(locationList);
|
---|
76 | return this.plannerRepository.save(planner);
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public Optional<Planner> editPlanner(Long id, PlannerDto plannerDto, String username) {
|
---|
81 | Planner planner = this.plannerRepository.findById(id).orElseThrow(() -> new PlannerNotFoundException(id));
|
---|
82 | User user = this.userRepository.findByUsername(username);
|
---|
83 |
|
---|
84 | planner.setName(plannerDto.getName());
|
---|
85 | planner.setDescription(plannerDto.getDescription());
|
---|
86 | plannerDto.setUser(user.getUsername());
|
---|
87 | planner.setUser(user);
|
---|
88 | /*
|
---|
89 | List<Location> locationList = new ArrayList<>();
|
---|
90 | for(Long location : plannerDto.getLocationList()){
|
---|
91 | Location loc = this.locationRepository.findById(location)
|
---|
92 | .orElseThrow(() -> new LocationNotFoundException(location));
|
---|
93 | locationList.add(loc);
|
---|
94 | }
|
---|
95 | planner.setLocationList(locationList);
|
---|
96 |
|
---|
97 | */
|
---|
98 | return Optional.of(this.plannerRepository.save(planner));
|
---|
99 | }
|
---|
100 |
|
---|
101 | }
|
---|
102 |
|
---|