source: trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/LocationRestController.java

Last change on this file was 6fe77af, checked in by Ema <ema_spirova@…>, 2 years ago

add location feature

  • Property mode set to 100644
File size: 4.0 KB
Line 
1package finki.diplomska.tripplanner.web.rest;
2
3import finki.diplomska.tripplanner.models.Location;
4import finki.diplomska.tripplanner.models.User;
5import finki.diplomska.tripplanner.models.dto.LocationDto;
6import finki.diplomska.tripplanner.models.dto.PlannerLocationDto;
7import finki.diplomska.tripplanner.service.LocationService;
8import finki.diplomska.tripplanner.service.PlannerService;
9import org.springframework.http.ResponseEntity;
10import org.springframework.security.core.Authentication;
11import org.springframework.web.bind.annotation.*;
12
13import java.util.List;
14
15@RestController
16@CrossOrigin(origins = "http://localhost:4200")
17@RequestMapping(value = "/api")
18public 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
40 @GetMapping(value = "/city/locations")
41 public List<Location> getAllLocationsFromCity(@RequestParam(required = false) Long cityId,
42 @RequestParam(required = false) Long companionId,
43 @RequestParam(required = false) Long lengthOfStay,
44 @RequestParam(required = false) String categoryIds) {
45 return this.locationService.findLocationsFromCityForm(cityId, companionId, lengthOfStay, categoryIds);
46
47 }
48
49 @GetMapping(value = "/region/locations")
50 public List<Location> getAllLocationsFromRegion(@RequestParam Long regionId,
51 @RequestParam Long companionId,
52 @RequestParam Long lengthOfStay,
53 @RequestParam String categoryIds){
54 return this.locationService.findLocationsFromRegionForm(regionId, companionId,lengthOfStay, categoryIds);
55 }
56
57 @PostMapping(value = "/add-location")
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 }
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 }
76
77 @GetMapping(value = "/planner/locationIds")
78 public List<Long> getAllLocationIdsForPlanner(@RequestParam Long plannerId){
79 return this.locationService.getAllLocationIdsForPlanner(plannerId);
80 }
81
82 @GetMapping(value = "/all")
83 public List<Location> getAllLocations(@RequestParam String place){
84 return this.locationService.getAllLocations(place);
85 }
86
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 }
94}
Note: See TracBrowser for help on using the repository browser.