1 | package finki.diplomska.tripplanner.web.rest;
|
---|
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.service.LocationService;
|
---|
7 | import finki.diplomska.tripplanner.service.PlannerService;
|
---|
8 | import org.springframework.http.HttpStatus;
|
---|
9 | import org.springframework.http.MediaType;
|
---|
10 | import org.springframework.http.ResponseEntity;
|
---|
11 | import org.springframework.web.bind.annotation.*;
|
---|
12 | import org.springframework.web.bind.annotation.RestController;
|
---|
13 |
|
---|
14 | import java.util.ArrayList;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 |
|
---|
18 | @RestController
|
---|
19 | @CrossOrigin(origins = "http://localhost:4200")
|
---|
20 | @RequestMapping(value = "/api")
|
---|
21 | public class PlannerRestController {
|
---|
22 | private final PlannerService plannerService;
|
---|
23 | private final LocationService locationService;
|
---|
24 |
|
---|
25 | public PlannerRestController(PlannerService plannerService, LocationService locationService) {
|
---|
26 | this.plannerService = plannerService;
|
---|
27 | this.locationService = locationService;
|
---|
28 | }
|
---|
29 |
|
---|
30 | @GetMapping(value = "/planners")
|
---|
31 | public List<Planner> getAllPlanners(){
|
---|
32 | return this.plannerService.getAllPlaners();
|
---|
33 | }
|
---|
34 |
|
---|
35 | @GetMapping(value = "/planner/{id}")
|
---|
36 | public ResponseEntity<Planner> findPlannerById(@PathVariable Long id){
|
---|
37 | return this.plannerService.findById(id)
|
---|
38 | .map(planner -> ResponseEntity.ok().body(planner))
|
---|
39 | .orElseGet(()-> ResponseEntity.notFound().build());
|
---|
40 | }
|
---|
41 |
|
---|
42 | @PostMapping(value = "/planner")
|
---|
43 | @ResponseStatus(HttpStatus.CREATED)
|
---|
44 | public Planner newPlanner(@RequestParam String description,
|
---|
45 | @RequestParam String name,
|
---|
46 | @RequestParam List<Long> locationList) {
|
---|
47 | List<Location> locations = new ArrayList<>();
|
---|
48 | for(long id: locationList){
|
---|
49 | Location location = locationService.getById(id);
|
---|
50 | locations.add(location);
|
---|
51 | }
|
---|
52 | Planner planner = plannerService.createPlannerWithRequestParams(description, name, locations);
|
---|
53 | return planner;
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | @PostMapping(value = "/planner/new", consumes= MediaType.APPLICATION_JSON_VALUE)
|
---|
58 | @ResponseStatus(HttpStatus.CREATED)
|
---|
59 | public ResponseEntity<Planner> newPlanner(@RequestBody PlannerDto plannerDto) {
|
---|
60 | return this.plannerService.newPlanner(plannerDto)
|
---|
61 | .map(planner -> ResponseEntity.ok().body(planner))
|
---|
62 | .orElseGet(() -> ResponseEntity.badRequest().build());
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | @PutMapping(value ="edit/planner/{id}", consumes= MediaType.APPLICATION_JSON_VALUE)
|
---|
67 | public ResponseEntity<Planner> editPlanner(@PathVariable Long id, @RequestBody PlannerDto plannerDto){
|
---|
68 | return this.plannerService.editPlanner(id, plannerDto)
|
---|
69 | .map(planner -> ResponseEntity.ok().body(planner))
|
---|
70 | .orElseGet(()-> ResponseEntity.badRequest().build());
|
---|
71 | }
|
---|
72 |
|
---|
73 | @PutMapping(value = "/add-location")
|
---|
74 | public Planner addLocationToPlanner(@RequestParam Long lid, @RequestParam Long pid){
|
---|
75 | return this.plannerService.addLocationToPlanner(lid, pid);
|
---|
76 | }
|
---|
77 | }
|
---|