Changeset ceaed42 for trip-planner


Ignore:
Timestamp:
11/11/21 12:59:26 (3 years ago)
Author:
Ema <ema_spirova@…>
Branches:
master
Children:
59329aa
Parents:
6c1585f
Message:

add location to planner

Location:
trip-planner/src
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • trip-planner/src/main/java/finki/diplomska/tripplanner/models/dto/PlannerDto.java

    r6c1585f rceaed42  
    1010    private String name;
    1111    private String description;
    12     private List<Long> locationList;
    1312
    14     public PlannerDto(String name, String description, List<Long> locationList) {
     13    public PlannerDto(String name, String description) {
    1514        this.name = name;
    1615        this.description = description;
    17         this.locationList = locationList;
    1816    }
    1917}
  • trip-planner/src/main/java/finki/diplomska/tripplanner/repository/jpa/JpaLocationRepository.java

    r6c1585f rceaed42  
    7777            "GROUP BY location.id_location ORDER BY CASE location.priority WHEN 'high' THEN 1 WHEN 'medium' THEN 2 WHEN 'low' THEN 3 END", nativeQuery = true)
    7878    List<Location> findLocationsFromRegionForm(@Param("regionId") Long regionId, @Param("companionId") Long companionId, @Param("categoryIds") List<Long> categoryIds);
     79
     80    @Query(value = "SELECT * " +
     81            "FROM locations AS l " +
     82            "WHERE l.id_location IN " +
     83            "(SELECT pl.id_location FROM planners_contain AS pl WHERE pl.id_planner = :plannerId)", nativeQuery = true)
     84    List<Location> getAllLocationsForPlanner(@Param("plannerId") Long plannerId);
    7985}
  • trip-planner/src/main/java/finki/diplomska/tripplanner/service/LocationService.java

    r6c1585f rceaed42  
    33
    44import finki.diplomska.tripplanner.models.Location;
     5import finki.diplomska.tripplanner.models.Planner;
     6import finki.diplomska.tripplanner.models.dto.PlannerLocationDto;
    57
    68import java.util.List;
     
    1719    List<Location> findLocationsFromCityForm(Long cityId, Long companionId, Long lengthOfStay, String categoryIds);
    1820    List<Location> findLocationsFromRegionForm(Long regionId, Long companionId, Long lengthOfStay, String categoryIds);
     21    Location addLocationToPlanner(PlannerLocationDto plannerLocationDto);
     22    List<Location> getAllLocationsForPlanner(Long plannerId);
     23
    1924}
  • trip-planner/src/main/java/finki/diplomska/tripplanner/service/PlannerService.java

    r6c1585f rceaed42  
    1414        List<Planner> getAllPlaners();
    1515        Optional<Planner> findById(Long id);
    16         Planner addLocationToPlanner(Long plannerId, Long locationId);
    1716        Planner editPlannerWithRequestParams(Long id, String description, String name, List<Location> locationList);
    1817        Optional<Planner> newPlanner(PlannerDto plannerDto);
  • trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/LocationServiceImpl.java

    r6c1585f rceaed42  
    22
    33import finki.diplomska.tripplanner.models.Location;
     4import finki.diplomska.tripplanner.models.Planner;
     5import finki.diplomska.tripplanner.models.dto.PlannerLocationDto;
    46import finki.diplomska.tripplanner.models.exceptions.LocationNotFoundException;
    57import finki.diplomska.tripplanner.repository.jpa.JpaLocationRepository;
     8import finki.diplomska.tripplanner.repository.jpa.JpaPlannerRepository;
    69import finki.diplomska.tripplanner.service.LocationService;
    710import org.springframework.stereotype.Service;
     
    1518
    1619    private final JpaLocationRepository locationRepository;
     20    private final JpaPlannerRepository plannerRepository;
    1721
    18     public LocationServiceImpl(JpaLocationRepository locationRepository) {
     22    public LocationServiceImpl(JpaLocationRepository locationRepository, JpaPlannerRepository plannerRepository) {
    1923        this.locationRepository = locationRepository;
     24        this.plannerRepository = plannerRepository;
    2025    }
    2126
     
    150155    }
    151156
     157    @Override
     158    public Location addLocationToPlanner(PlannerLocationDto plannerLocationDto) {
     159        Location location = this.locationRepository.findById(plannerLocationDto.getLocationId())
     160                .orElseThrow(() -> new LocationNotFoundException(plannerLocationDto.getLocationId()));
     161        Planner planner = this.plannerRepository.getById(plannerLocationDto.getPlannerId());
     162        planner.getLocationList().add(location);
     163        return this.locationRepository.save(location);
     164    }
     165
     166    @Override
     167    public List<Location> getAllLocationsForPlanner(Long plannerId) {
     168        return this.locationRepository.getAllLocationsForPlanner(plannerId);
     169    }
     170
     171
    152172}
  • trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/PlannerServiceImpl.java

    r6c1585f rceaed42  
    2222    private final JpaPlannerRepository plannerRepository;
    2323    private final JpaLocationRepository locationRepository;
    24     private final LocationService locationService;
    2524
    26     public PlannerServiceImpl(JpaPlannerRepository plannerRepository, JpaLocationRepository locationRepository, LocationService locationService) {
     25    public PlannerServiceImpl(JpaPlannerRepository plannerRepository, JpaLocationRepository locationRepository) {
    2726        this.plannerRepository = plannerRepository;
    2827        this.locationRepository = locationRepository;
    29         this.locationService = locationService;
    3028    }
    3129
     
    4038    }
    4139
    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     }
    5040
    5141    @Override
     
    8474        planner.setName(plannerDto.getName());
    8575        planner.setDescription(plannerDto.getDescription());
    86 
     76/*
    8777        List<Location> locationList  = new ArrayList<>();
    8878        for(Long location : plannerDto.getLocationList()){
     
    9282        }
    9383        planner.setLocationList(locationList);
     84
     85 */
    9486        return Optional.of(this.plannerRepository.save(planner));
    9587    }
  • trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/LocationRestController.java

    r6c1585f rceaed42  
    22
    33import finki.diplomska.tripplanner.models.Location;
     4import finki.diplomska.tripplanner.models.dto.PlannerLocationDto;
    45import finki.diplomska.tripplanner.service.LocationService;
    56import finki.diplomska.tripplanner.service.PlannerService;
     
    5051        return this.locationService.findLocationsFromRegionForm(regionId, companionId,lengthOfStay, categoryIds);
    5152    }
     53
     54    @PutMapping(value = "/add-location")
     55    public Location addLocationToPlanner(@RequestBody PlannerLocationDto plannerLocationDto){
     56        return this.locationService.addLocationToPlanner(plannerLocationDto);
     57    }
     58
     59    @GetMapping(value = "/planner/locations")
     60    public List<Location> getAllLocationsForPlanner (@RequestParam Long plannerId){
     61        return this.locationService.getAllLocationsForPlanner(plannerId);
     62    }
    5263}
  • trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/PlannerRestController.java

    r6c1585f rceaed42  
    7171    }
    7272
    73     @PutMapping(value = "/add-location")
    74     public Planner addLocationToPlanner(@RequestParam Long lid, @RequestParam Long pid){
    75         return this.plannerService.addLocationToPlanner(lid, pid);
    76     }
     73
    7774}
  • trip-planner/src/test/api.http

    r6c1585f rceaed42  
    5252###
    5353PUT http://localhost:8080/api/add-location
     54Content-Type: application/json
     55
     56{"plannerId": 1, "locationId": 2}
     57
     58###
     59GET http://localhost:8080/api/planner/locations?plannerId=1
    5460Content-Type: application/x-www-form-urlencoded
    5561
    56 lid=1&pid=1
Note: See TracChangeset for help on using the changeset viewer.