[6a3a178] | 1 | package finki.diplomska.tripplanner.service.impl;
|
---|
| 2 |
|
---|
| 3 | import finki.diplomska.tripplanner.models.Location;
|
---|
| 4 | import finki.diplomska.tripplanner.models.Planner;
|
---|
[84d0fbb] | 5 | import finki.diplomska.tripplanner.models.User;
|
---|
[6a3a178] | 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;
|
---|
[84d0fbb] | 11 | import finki.diplomska.tripplanner.repository.jpa.JpaUserRepository;
|
---|
[6a3a178] | 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;
|
---|
[84d0fbb] | 23 | private final JpaUserRepository userRepository;
|
---|
[6a3a178] | 24 |
|
---|
[84d0fbb] | 25 | public PlannerServiceImpl(JpaPlannerRepository plannerRepository, JpaLocationRepository locationRepository, JpaUserRepository userRepository) {
|
---|
[6a3a178] | 26 | this.plannerRepository = plannerRepository;
|
---|
| 27 | this.locationRepository = locationRepository;
|
---|
[84d0fbb] | 28 | this.userRepository = userRepository;
|
---|
[6a3a178] | 29 | }
|
---|
| 30 |
|
---|
| 31 | @Override
|
---|
| 32 | public List<Planner> getAllPlaners() {
|
---|
| 33 | return this.plannerRepository.findAll();
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[84d0fbb] | 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 |
|
---|
[6a3a178] | 42 | @Override
|
---|
| 43 | public Optional<Planner> findById(Long id) {
|
---|
| 44 | return this.plannerRepository.findById(id);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | @Override
|
---|
[84d0fbb] | 49 | public Optional<Planner> newPlanner(PlannerDto plannerDto, String username) {
|
---|
[6a80231] | 50 | /*
|
---|
[6a3a178] | 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 | }
|
---|
[6a80231] | 57 | */
|
---|
[84d0fbb] | 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)));
|
---|
[6a3a178] | 61 | }
|
---|
| 62 |
|
---|
[bdd6491] | 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 |
|
---|
[6a3a178] | 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
|
---|
[84d0fbb] | 89 | public Optional<Planner> editPlanner(Long id, PlannerDto plannerDto, String username) {
|
---|
[6a3a178] | 90 | Planner planner = this.plannerRepository.findById(id).orElseThrow(() -> new PlannerNotFoundException(id));
|
---|
[84d0fbb] | 91 | User user = this.userRepository.findByUsername(username);
|
---|
[6a3a178] | 92 |
|
---|
| 93 | planner.setName(plannerDto.getName());
|
---|
| 94 | planner.setDescription(plannerDto.getDescription());
|
---|
[84d0fbb] | 95 | plannerDto.setUser(user.getUsername());
|
---|
| 96 | planner.setUser(user);
|
---|
[ceaed42] | 97 | /*
|
---|
[6a3a178] | 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);
|
---|
[ceaed42] | 105 |
|
---|
| 106 | */
|
---|
[6a3a178] | 107 | return Optional.of(this.plannerRepository.save(planner));
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | }
|
---|
| 111 |
|
---|