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.User;
|
---|
6 | import finki.diplomska.tripplanner.models.dto.PlannerDto;
|
---|
7 | import finki.diplomska.tripplanner.models.dto.PlannerLocationDto;
|
---|
8 | import finki.diplomska.tripplanner.service.LocationService;
|
---|
9 | import finki.diplomska.tripplanner.service.PlannerService;
|
---|
10 | import org.springframework.http.HttpStatus;
|
---|
11 | import org.springframework.http.MediaType;
|
---|
12 | import org.springframework.http.ResponseEntity;
|
---|
13 | import org.springframework.security.core.Authentication;
|
---|
14 | import org.springframework.web.bind.annotation.*;
|
---|
15 | import org.springframework.web.bind.annotation.RestController;
|
---|
16 |
|
---|
17 | import javax.validation.Valid;
|
---|
18 | import java.util.ArrayList;
|
---|
19 | import java.util.List;
|
---|
20 |
|
---|
21 |
|
---|
22 | @RestController
|
---|
23 | @CrossOrigin(origins = "http://localhost:4200", exposedHeaders = "token")
|
---|
24 | @RequestMapping(value = "/api")
|
---|
25 | public 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 | }
|
---|