source: trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/PlannerRestController.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.1 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.User;
6import finki.diplomska.tripplanner.models.dto.PlannerDto;
7import finki.diplomska.tripplanner.models.dto.PlannerLocationDto;
8import finki.diplomska.tripplanner.service.LocationService;
9import finki.diplomska.tripplanner.service.PlannerService;
10import org.springframework.http.HttpStatus;
11import org.springframework.http.MediaType;
12import org.springframework.http.ResponseEntity;
13import org.springframework.security.core.Authentication;
14import org.springframework.web.bind.annotation.*;
15import org.springframework.web.bind.annotation.RestController;
16
17import javax.validation.Valid;
18import java.util.ArrayList;
19import java.util.List;
20
21
22@RestController
23@CrossOrigin(origins = "http://localhost:4200", exposedHeaders = "token")
24@RequestMapping(value = "/api")
25public class PlannerRestController {
26 private final PlannerService plannerService;
27 private final LocationService locationService;
28
29 public PlannerRestController(PlannerService plannerService, LocationService locationService) {
30 this.plannerService = plannerService;
31 this.locationService = locationService;
32 }
33
34 @GetMapping(value = "/planners")
35 public List<Planner> getAllPlanners(){
36 return this.plannerService.getAllPlaners();
37 }
38
39
40 @GetMapping(value = "/planners/user")
41 public List<Planner> getPlannersByUser(Authentication authentication){
42 User user = (User) authentication.getPrincipal();
43 return this.plannerService.getPlannersByUser(user.getUsername());
44 }
45
46 @GetMapping(value = "/planner/{id}")
47 public ResponseEntity<Planner> findPlannerById(@PathVariable Long id){
48 return this.plannerService.findById(id)
49 .map(planner -> ResponseEntity.ok().body(planner))
50 .orElseGet(()-> ResponseEntity.notFound().build());
51 }
52
53 @PostMapping(value = "/planner")
54 @ResponseStatus(HttpStatus.CREATED)
55 public Planner newPlanner(@RequestParam String description,
56 @RequestParam String name,
57 @RequestParam List<Long> locationList) {
58 List<Location> locations = new ArrayList<>();
59 for(long id: locationList){
60 Location location = locationService.getById(id);
61 locations.add(location);
62 }
63 Planner planner = plannerService.createPlannerWithRequestParams(description, name, locations);
64 return planner;
65 }
66
67
68 @PostMapping(value = "/planner/new", consumes= MediaType.APPLICATION_JSON_VALUE)
69 @ResponseStatus(HttpStatus.CREATED)
70 public ResponseEntity<?> newPlanner(@Valid @RequestBody PlannerDto plannerDto, Authentication authentication) {
71 User user = (User) authentication.getPrincipal();
72 return this.plannerService.newPlanner(plannerDto, user.getUsername())
73 .map(planner -> ResponseEntity.ok().body(planner))
74 .orElseGet(() -> ResponseEntity.badRequest().build());
75 }
76
77
78 @PutMapping(value ="edit/planner/{id}", consumes= MediaType.APPLICATION_JSON_VALUE)
79 public ResponseEntity<Planner> editPlanner(@PathVariable Long id, @Valid @RequestBody PlannerDto plannerDto, Authentication authentication){
80 User user = (User) authentication.getPrincipal();
81 return this.plannerService.editPlanner(id, plannerDto, user.getUsername())
82 .map(planner -> ResponseEntity.ok().body(planner))
83 .orElseGet(()-> ResponseEntity.badRequest().build());
84 }
85
86 @DeleteMapping("/delete/{id}")
87 public ResponseEntity deleteById(@PathVariable Long id) {
88 this.plannerService.deletePlannerById(id);
89 return this.plannerService.findById(id)
90 .map(planner -> ResponseEntity.ok().body(planner))
91 .orElseGet(()-> ResponseEntity.notFound().build());
92 }
93
94 @DeleteMapping(value = "/delete-location")
95 public ResponseEntity deleteLocationFromPlanner(@RequestBody PlannerLocationDto plannerLocationDto){
96 return this.plannerService.deleteLocationFromPlanner(plannerLocationDto);
97 }
98}
Note: See TracBrowser for help on using the repository browser.