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