[6a3a178] | 1 | package finki.diplomska.tripplanner.web.rest;
|
---|
| 2 |
|
---|
| 3 | import finki.diplomska.tripplanner.models.Location;
|
---|
| 4 | import finki.diplomska.tripplanner.models.Planner;
|
---|
[84d0fbb] | 5 | import finki.diplomska.tripplanner.models.User;
|
---|
[6a3a178] | 6 | import finki.diplomska.tripplanner.models.dto.PlannerDto;
|
---|
| 7 | import finki.diplomska.tripplanner.service.LocationService;
|
---|
| 8 | import finki.diplomska.tripplanner.service.PlannerService;
|
---|
| 9 | import org.springframework.http.HttpStatus;
|
---|
| 10 | import org.springframework.http.MediaType;
|
---|
| 11 | import org.springframework.http.ResponseEntity;
|
---|
[84d0fbb] | 12 | import org.springframework.security.core.Authentication;
|
---|
[6a3a178] | 13 | import org.springframework.web.bind.annotation.*;
|
---|
| 14 | import org.springframework.web.bind.annotation.RestController;
|
---|
| 15 |
|
---|
[84d0fbb] | 16 | import javax.validation.Valid;
|
---|
[6a3a178] | 17 | import java.util.ArrayList;
|
---|
| 18 | import java.util.List;
|
---|
| 19 |
|
---|
| 20 |
|
---|
| 21 | @RestController
|
---|
[84d0fbb] | 22 | @CrossOrigin(origins = "http://localhost:4200", exposedHeaders = "token")
|
---|
[6a3a178] | 23 | @RequestMapping(value = "/api")
|
---|
| 24 | public 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 |
|
---|
[84d0fbb] | 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 |
|
---|
[6a3a178] | 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)
|
---|
[84d0fbb] | 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())
|
---|
[6a3a178] | 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)
|
---|
[84d0fbb] | 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())
|
---|
[6a3a178] | 82 | .map(planner -> ResponseEntity.ok().body(planner))
|
---|
| 83 | .orElseGet(()-> ResponseEntity.badRequest().build());
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[ceaed42] | 86 |
|
---|
[6a3a178] | 87 | }
|
---|