[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) {
|
---|
[6a80231] | 53 | /*
|
---|
[6a3a178] | 54 | List<Location> locationList = new ArrayList<>();
|
---|
| 55 | for(Long location : plannerDto.getLocationList()){
|
---|
| 56 | Location loc = this.locationRepository.findById(location)
|
---|
| 57 | .orElseThrow(() -> new LocationNotFoundException(location));
|
---|
| 58 | locationList.add(loc);
|
---|
| 59 | }
|
---|
[6a80231] | 60 |
|
---|
| 61 | */
|
---|
| 62 | return Optional.of(this.plannerRepository.save(new Planner(plannerDto.getName(), plannerDto.getDescription(), null)));
|
---|
[6a3a178] | 63 | }
|
---|
| 64 |
|
---|
| 65 | @Override
|
---|
| 66 | public Planner createPlannerWithRequestParams(String plandesc,String planname, List<Location> locationList) {
|
---|
| 67 | Planner planner = Planner.createNewPlanner(plandesc, planname, locationList);
|
---|
| 68 | return this.plannerRepository.save(planner);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | @Override
|
---|
| 72 | public Planner editPlannerWithRequestParams(Long id, String description, String name, List<Location> locationList) {
|
---|
| 73 | Planner planner = this.plannerRepository.findById(id).orElseThrow(() -> new LocationNotFoundException(id));
|
---|
| 74 | planner.setDescription(description);
|
---|
| 75 | planner.setName(name);
|
---|
| 76 | planner.setLocationList(locationList);
|
---|
| 77 | return this.plannerRepository.save(planner);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | @Override
|
---|
| 81 | public Optional<Planner> editPlanner(Long id, PlannerDto plannerDto) {
|
---|
| 82 | Planner planner = this.plannerRepository.findById(id).orElseThrow(() -> new PlannerNotFoundException(id));
|
---|
| 83 |
|
---|
| 84 | planner.setName(plannerDto.getName());
|
---|
| 85 | planner.setDescription(plannerDto.getDescription());
|
---|
| 86 |
|
---|
| 87 | List<Location> locationList = new ArrayList<>();
|
---|
| 88 | for(Long location : plannerDto.getLocationList()){
|
---|
| 89 | Location loc = this.locationRepository.findById(location)
|
---|
| 90 | .orElseThrow(() -> new LocationNotFoundException(location));
|
---|
| 91 | locationList.add(loc);
|
---|
| 92 | }
|
---|
| 93 | planner.setLocationList(locationList);
|
---|
| 94 | return Optional.of(this.plannerRepository.save(planner));
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | }
|
---|
| 98 |
|
---|