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

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

spring security 2.0

  • Property mode set to 100644
File size: 3.5 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.service.LocationService;
8import finki.diplomska.tripplanner.service.PlannerService;
9import org.springframework.http.HttpStatus;
10import org.springframework.http.MediaType;
11import org.springframework.http.ResponseEntity;
12import org.springframework.security.core.Authentication;
13import org.springframework.web.bind.annotation.*;
14import org.springframework.web.bind.annotation.RestController;
15
16import javax.validation.Valid;
17import java.util.ArrayList;
18import java.util.List;
19
20
21@RestController
22@CrossOrigin(origins = "http://localhost:4200", exposedHeaders = "token")
23@RequestMapping(value = "/api")
24public class PlannerRestController {
25 private final PlannerService plannerService;
26 private final LocationService locationService;
27
28 public PlannerRestController(PlannerService plannerService, LocationService locationService) {
29 this.plannerService = plannerService;
30 this.locationService = locationService;
31 }
32
33 @GetMapping(value = "/planners")
34 public List<Planner> getAllPlanners(){
35 return this.plannerService.getAllPlaners();
36 }
37
38
39 @GetMapping(value = "/planners/user")
40 public List<Planner> getPlannersByUser(Authentication authentication){
41 User user = (User) authentication.getPrincipal();
42 return this.plannerService.getPlannersByUser(user.getUsername());
43 }
44
45 @GetMapping(value = "/planner/{id}")
46 public ResponseEntity<Planner> findPlannerById(@PathVariable Long id){
47 return this.plannerService.findById(id)
48 .map(planner -> ResponseEntity.ok().body(planner))
49 .orElseGet(()-> ResponseEntity.notFound().build());
50 }
51
52 @PostMapping(value = "/planner")
53 @ResponseStatus(HttpStatus.CREATED)
54 public Planner newPlanner(@RequestParam String description,
55 @RequestParam String name,
56 @RequestParam List<Long> locationList) {
57 List<Location> locations = new ArrayList<>();
58 for(long id: locationList){
59 Location location = locationService.getById(id);
60 locations.add(location);
61 }
62 Planner planner = plannerService.createPlannerWithRequestParams(description, name, locations);
63 return planner;
64 }
65
66
67 @PostMapping(value = "/planner/new", consumes= MediaType.APPLICATION_JSON_VALUE)
68 @ResponseStatus(HttpStatus.CREATED)
69 public ResponseEntity<?> newPlanner(@Valid @RequestBody PlannerDto plannerDto, Authentication authentication) {
70
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
87}
Note: See TracBrowser for help on using the repository browser.