[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;
|
---|
[b738035] | 7 | import finki.diplomska.tripplanner.models.dto.PlannerLocationDto;
|
---|
[6a3a178] | 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;
|
---|
[84d0fbb] | 12 | import finki.diplomska.tripplanner.repository.jpa.JpaUserRepository;
|
---|
[6a3a178] | 13 | import finki.diplomska.tripplanner.service.PlannerService;
|
---|
[b738035] | 14 | import org.springframework.http.ResponseEntity;
|
---|
[6a3a178] | 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;
|
---|
[84d0fbb] | 25 | private final JpaUserRepository userRepository;
|
---|
[6a3a178] | 26 |
|
---|
[84d0fbb] | 27 | public PlannerServiceImpl(JpaPlannerRepository plannerRepository, JpaLocationRepository locationRepository, JpaUserRepository userRepository) {
|
---|
[6a3a178] | 28 | this.plannerRepository = plannerRepository;
|
---|
| 29 | this.locationRepository = locationRepository;
|
---|
[84d0fbb] | 30 | this.userRepository = userRepository;
|
---|
[6a3a178] | 31 | }
|
---|
| 32 |
|
---|
| 33 | @Override
|
---|
| 34 | public List<Planner> getAllPlaners() {
|
---|
| 35 | return this.plannerRepository.findAll();
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[84d0fbb] | 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 |
|
---|
[6a3a178] | 44 | @Override
|
---|
| 45 | public Optional<Planner> findById(Long id) {
|
---|
| 46 | return this.plannerRepository.findById(id);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | @Override
|
---|
[84d0fbb] | 51 | public Optional<Planner> newPlanner(PlannerDto plannerDto, String username) {
|
---|
[6a80231] | 52 | /*
|
---|
[6a3a178] | 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 | }
|
---|
[6a80231] | 59 | */
|
---|
[84d0fbb] | 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)));
|
---|
[6a3a178] | 63 | }
|
---|
| 64 |
|
---|
[bdd6491] | 65 | @Override
|
---|
| 66 | public void deletePlannerById(Long id) {
|
---|
| 67 | this.plannerRepository.deleteById(id);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | @Override
|
---|
[b738035] | 71 | public ResponseEntity deleteLocationFromPlanner(PlannerLocationDto plannerLocationDto) {
|
---|
| 72 | plannerRepository.deleteLocationFromPlanner(plannerLocationDto.getPlannerId(), plannerLocationDto.getLocationId());
|
---|
| 73 | return null;
|
---|
[bdd6491] | 74 | }
|
---|
| 75 |
|
---|
[6a3a178] | 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
|
---|
[84d0fbb] | 92 | public Optional<Planner> editPlanner(Long id, PlannerDto plannerDto, String username) {
|
---|
[6a3a178] | 93 | Planner planner = this.plannerRepository.findById(id).orElseThrow(() -> new PlannerNotFoundException(id));
|
---|
[84d0fbb] | 94 | User user = this.userRepository.findByUsername(username);
|
---|
[6a3a178] | 95 |
|
---|
| 96 | planner.setName(plannerDto.getName());
|
---|
| 97 | planner.setDescription(plannerDto.getDescription());
|
---|
[84d0fbb] | 98 | plannerDto.setUser(user.getUsername());
|
---|
| 99 | planner.setUser(user);
|
---|
[ceaed42] | 100 | /*
|
---|
[6a3a178] | 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);
|
---|
[ceaed42] | 108 |
|
---|
| 109 | */
|
---|
[6a3a178] | 110 | return Optional.of(this.plannerRepository.save(planner));
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | }
|
---|
| 114 |
|
---|