[6a3a178] | 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 | private final LocationService locationService;
|
---|
| 25 |
|
---|
| 26 | public PlannerServiceImpl(JpaPlannerRepository plannerRepository, JpaLocationRepository locationRepository, LocationService locationService) {
|
---|
| 27 | this.plannerRepository = plannerRepository;
|
---|
| 28 | this.locationRepository = locationRepository;
|
---|
| 29 | this.locationService = locationService;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | @Override
|
---|
| 33 | public List<Planner> getAllPlaners() {
|
---|
| 34 | return this.plannerRepository.findAll();
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | @Override
|
---|
| 38 | public Optional<Planner> findById(Long id) {
|
---|
| 39 | return this.plannerRepository.findById(id);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | @Override
|
---|
| 43 | public Planner addLocationToPlanner(Long plannerId, Long locationId) {
|
---|
| 44 | Location location = this.locationService.findById(locationId)
|
---|
| 45 | .orElseThrow(() -> new LocationNotFoundException(locationId));
|
---|
| 46 | Planner planner = this.plannerRepository.getById(plannerId);
|
---|
| 47 | planner.getLocationList().add(location);
|
---|
| 48 | return this.plannerRepository.save(planner);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | @Override
|
---|
| 52 | public Optional<Planner> newPlanner(PlannerDto plannerDto) {
|
---|
| 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 | return Optional.of(this.plannerRepository.save(new Planner(plannerDto.getName(), plannerDto.getDescription(), locationList)));
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | @Override
|
---|
| 63 | public Planner createPlannerWithRequestParams(String plandesc,String planname, List<Location> locationList) {
|
---|
| 64 | Planner planner = Planner.createNewPlanner(plandesc, planname, locationList);
|
---|
| 65 | return this.plannerRepository.save(planner);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | @Override
|
---|
| 69 | public Planner editPlannerWithRequestParams(Long id, String description, String name, List<Location> locationList) {
|
---|
| 70 | Planner planner = this.plannerRepository.findById(id).orElseThrow(() -> new LocationNotFoundException(id));
|
---|
| 71 | planner.setDescription(description);
|
---|
| 72 | planner.setName(name);
|
---|
| 73 | planner.setLocationList(locationList);
|
---|
| 74 | return this.plannerRepository.save(planner);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | @Override
|
---|
| 78 | public Optional<Planner> editPlanner(Long id, PlannerDto plannerDto) {
|
---|
| 79 | Planner planner = this.plannerRepository.findById(id).orElseThrow(() -> new PlannerNotFoundException(id));
|
---|
| 80 |
|
---|
| 81 | planner.setName(plannerDto.getName());
|
---|
| 82 | planner.setDescription(plannerDto.getDescription());
|
---|
| 83 |
|
---|
| 84 | List<Location> locationList = new ArrayList<>();
|
---|
| 85 | for(Long location : plannerDto.getLocationList()){
|
---|
| 86 | Location loc = this.locationRepository.findById(location)
|
---|
| 87 | .orElseThrow(() -> new LocationNotFoundException(location));
|
---|
| 88 | locationList.add(loc);
|
---|
| 89 | }
|
---|
| 90 | planner.setLocationList(locationList);
|
---|
| 91 | return Optional.of(this.plannerRepository.save(planner));
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | }
|
---|
| 95 |
|
---|