[6a3a178] | 1 | package finki.diplomska.tripplanner.web.rest;
|
---|
| 2 |
|
---|
| 3 | import finki.diplomska.tripplanner.models.Location;
|
---|
[6fe77af] | 4 | import finki.diplomska.tripplanner.models.User;
|
---|
| 5 | import finki.diplomska.tripplanner.models.dto.LocationDto;
|
---|
[ceaed42] | 6 | import finki.diplomska.tripplanner.models.dto.PlannerLocationDto;
|
---|
[6a3a178] | 7 | import finki.diplomska.tripplanner.service.LocationService;
|
---|
| 8 | import finki.diplomska.tripplanner.service.PlannerService;
|
---|
| 9 | import org.springframework.http.ResponseEntity;
|
---|
[6fe77af] | 10 | import org.springframework.security.core.Authentication;
|
---|
[6a3a178] | 11 | import org.springframework.web.bind.annotation.*;
|
---|
| 12 |
|
---|
| 13 | import java.util.List;
|
---|
| 14 |
|
---|
| 15 | @RestController
|
---|
| 16 | @CrossOrigin(origins = "http://localhost:4200")
|
---|
| 17 | @RequestMapping(value = "/api")
|
---|
| 18 | public class LocationRestController {
|
---|
| 19 |
|
---|
| 20 | private final LocationService locationService;
|
---|
| 21 | private final PlannerService plannerService;
|
---|
| 22 |
|
---|
| 23 | public LocationRestController(LocationService locationService, PlannerService plannerService) {
|
---|
| 24 | this.locationService = locationService;
|
---|
| 25 | this.plannerService = plannerService;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | @GetMapping(value = "/locations")
|
---|
| 29 | public List<Location> findAllLocations(){
|
---|
| 30 | return this.locationService.findAll();
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | @GetMapping(value = "/location/{id}")
|
---|
| 34 | public ResponseEntity<Location> findLocationById(@PathVariable Long id){
|
---|
| 35 | return this.locationService.findById(id)
|
---|
| 36 | .map(location -> ResponseEntity.ok().body(location))
|
---|
| 37 | .orElseGet(()-> ResponseEntity.notFound().build());
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[188ee53] | 40 | @GetMapping(value = "/city/locations")
|
---|
| 41 | public List<Location> getAllLocationsFromCity(@RequestParam(required = false) Long cityId,
|
---|
[eed0bf8] | 42 | @RequestParam(required = false) Long companionId,
|
---|
| 43 | @RequestParam(required = false) Long lengthOfStay,
|
---|
| 44 | @RequestParam(required = false) String categoryIds) {
|
---|
[188ee53] | 45 | return this.locationService.findLocationsFromCityForm(cityId, companionId, lengthOfStay, categoryIds);
|
---|
| 46 |
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | @GetMapping(value = "/region/locations")
|
---|
[b738035] | 50 | public List<Location> getAllLocationsFromRegion(@RequestParam Long regionId,
|
---|
| 51 | @RequestParam Long companionId,
|
---|
| 52 | @RequestParam Long lengthOfStay,
|
---|
| 53 | @RequestParam String categoryIds){
|
---|
[188ee53] | 54 | return this.locationService.findLocationsFromRegionForm(regionId, companionId,lengthOfStay, categoryIds);
|
---|
[6a3a178] | 55 | }
|
---|
[ceaed42] | 56 |
|
---|
[b738035] | 57 | @PostMapping(value = "/add-location")
|
---|
[ceaed42] | 58 | public Location addLocationToPlanner(@RequestBody PlannerLocationDto plannerLocationDto){
|
---|
| 59 | return this.locationService.addLocationToPlanner(plannerLocationDto);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | @GetMapping(value = "/planner/locations")
|
---|
| 63 | public List<Location> getAllLocationsForPlanner (@RequestParam Long plannerId){
|
---|
| 64 | return this.locationService.getAllLocationsForPlanner(plannerId);
|
---|
| 65 | }
|
---|
[59329aa] | 66 |
|
---|
| 67 | @GetMapping(value = "weekend")
|
---|
| 68 | public List<Location> getWeekendGetaways(){
|
---|
| 69 | return this.locationService.getWeekendGetaways();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | @GetMapping(value = "villages")
|
---|
| 73 | public List<Location> getVillages(){
|
---|
| 74 | return this.locationService.getVillages();
|
---|
| 75 | }
|
---|
[8d391a1] | 76 |
|
---|
| 77 | @GetMapping(value = "/planner/locationIds")
|
---|
| 78 | public List<Long> getAllLocationIdsForPlanner(@RequestParam Long plannerId){
|
---|
| 79 | return this.locationService.getAllLocationIdsForPlanner(plannerId);
|
---|
| 80 | }
|
---|
[bdd6491] | 81 |
|
---|
[76712b2] | 82 | @GetMapping(value = "/all")
|
---|
| 83 | public List<Location> getAllLocations(@RequestParam String place){
|
---|
| 84 | return this.locationService.getAllLocations(place);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[6fe77af] | 87 | @PostMapping("/add")
|
---|
| 88 | public ResponseEntity<Location> save(@RequestBody LocationDto locationDto, Authentication authentication) {
|
---|
| 89 | User user = (User) authentication.getPrincipal();
|
---|
| 90 | return this.locationService.save(locationDto, user.getUsername())
|
---|
| 91 | .map(location -> ResponseEntity.ok().body(location))
|
---|
| 92 | .orElseGet(() -> ResponseEntity.badRequest().build());
|
---|
| 93 | }
|
---|
[6a3a178] | 94 | }
|
---|