source: trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/PlannerRestController.java@ ceaed42

Last change on this file since ceaed42 was ceaed42, checked in by Ema <ema_spirova@…>, 3 years ago

add location to planner

  • Property mode set to 100644
File size: 2.9 KB
Line 
1package finki.diplomska.tripplanner.web.rest;
2
3import finki.diplomska.tripplanner.models.Location;
4import finki.diplomska.tripplanner.models.Planner;
5import finki.diplomska.tripplanner.models.dto.PlannerDto;
6import finki.diplomska.tripplanner.service.LocationService;
7import finki.diplomska.tripplanner.service.PlannerService;
8import org.springframework.http.HttpStatus;
9import org.springframework.http.MediaType;
10import org.springframework.http.ResponseEntity;
11import org.springframework.web.bind.annotation.*;
12import org.springframework.web.bind.annotation.RestController;
13
14import java.util.ArrayList;
15import java.util.List;
16
17
18@RestController
19@CrossOrigin(origins = "http://localhost:4200")
20@RequestMapping(value = "/api")
21public 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
74}
Note: See TracBrowser for help on using the repository browser.