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