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.dto.PlannerDto;
|
---|
6 | import finki.diplomska.tripplanner.models.exceptions.LocationNotFoundException;
|
---|
7 | import finki.diplomska.tripplanner.models.exceptions.PlannerNotFoundException;
|
---|
8 | import finki.diplomska.tripplanner.repository.jpa.JpaLocationRepository;
|
---|
9 | import finki.diplomska.tripplanner.repository.jpa.JpaPlannerRepository;
|
---|
10 | import finki.diplomska.tripplanner.service.LocationService;
|
---|
11 | import finki.diplomska.tripplanner.service.PlannerService;
|
---|
12 | import org.springframework.stereotype.Service;
|
---|
13 | import reactor.util.annotation.Nullable;
|
---|
14 |
|
---|
15 | import java.util.ArrayList;
|
---|
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 |
|
---|
25 | public PlannerServiceImpl(JpaPlannerRepository plannerRepository, JpaLocationRepository locationRepository) {
|
---|
26 | this.plannerRepository = plannerRepository;
|
---|
27 | this.locationRepository = locationRepository;
|
---|
28 | }
|
---|
29 |
|
---|
30 | @Override
|
---|
31 | public List<Planner> getAllPlaners() {
|
---|
32 | return this.plannerRepository.findAll();
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public Optional<Planner> findById(Long id) {
|
---|
37 | return this.plannerRepository.findById(id);
|
---|
38 | }
|
---|
39 |
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public Optional<Planner> newPlanner(PlannerDto plannerDto) {
|
---|
43 | /*
|
---|
44 | List<Location> locationList = new ArrayList<>();
|
---|
45 | for(Long location : plannerDto.getLocationList()){
|
---|
46 | Location loc = this.locationRepository.findById(location)
|
---|
47 | .orElseThrow(() -> new LocationNotFoundException(location));
|
---|
48 | locationList.add(loc);
|
---|
49 | }
|
---|
50 |
|
---|
51 | */
|
---|
52 | return Optional.of(this.plannerRepository.save(new Planner(plannerDto.getName(), plannerDto.getDescription(), null)));
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public Planner createPlannerWithRequestParams(String plandesc,String planname, List<Location> locationList) {
|
---|
57 | Planner planner = Planner.createNewPlanner(plandesc, planname, locationList);
|
---|
58 | return this.plannerRepository.save(planner);
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public Planner editPlannerWithRequestParams(Long id, String description, String name, List<Location> locationList) {
|
---|
63 | Planner planner = this.plannerRepository.findById(id).orElseThrow(() -> new LocationNotFoundException(id));
|
---|
64 | planner.setDescription(description);
|
---|
65 | planner.setName(name);
|
---|
66 | planner.setLocationList(locationList);
|
---|
67 | return this.plannerRepository.save(planner);
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public Optional<Planner> editPlanner(Long id, PlannerDto plannerDto) {
|
---|
72 | Planner planner = this.plannerRepository.findById(id).orElseThrow(() -> new PlannerNotFoundException(id));
|
---|
73 |
|
---|
74 | planner.setName(plannerDto.getName());
|
---|
75 | planner.setDescription(plannerDto.getDescription());
|
---|
76 | /*
|
---|
77 | List<Location> locationList = new ArrayList<>();
|
---|
78 | for(Long location : plannerDto.getLocationList()){
|
---|
79 | Location loc = this.locationRepository.findById(location)
|
---|
80 | .orElseThrow(() -> new LocationNotFoundException(location));
|
---|
81 | locationList.add(loc);
|
---|
82 | }
|
---|
83 | planner.setLocationList(locationList);
|
---|
84 |
|
---|
85 | */
|
---|
86 | return Optional.of(this.plannerRepository.save(planner));
|
---|
87 | }
|
---|
88 |
|
---|
89 | }
|
---|
90 |
|
---|