Ignore:
Timestamp:
12/19/21 19:39:00 (3 years ago)
Author:
Ema <ema_spirova@…>
Branches:
master
Children:
bdd6491
Parents:
1ad8e64
Message:

spring security 2.0

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/PlannerRestController.java

    r1ad8e64 r84d0fbb  
    33import finki.diplomska.tripplanner.models.Location;
    44import finki.diplomska.tripplanner.models.Planner;
     5import finki.diplomska.tripplanner.models.User;
    56import finki.diplomska.tripplanner.models.dto.PlannerDto;
    67import finki.diplomska.tripplanner.service.LocationService;
     
    910import org.springframework.http.MediaType;
    1011import org.springframework.http.ResponseEntity;
     12import org.springframework.security.core.Authentication;
    1113import org.springframework.web.bind.annotation.*;
    1214import org.springframework.web.bind.annotation.RestController;
    1315
     16import javax.validation.Valid;
    1417import java.util.ArrayList;
    1518import java.util.List;
     
    1720
    1821@RestController
    19 @CrossOrigin(origins = "http://localhost:4200")
     22@CrossOrigin(origins = "http://localhost:4200",  exposedHeaders = "token")
    2023@RequestMapping(value = "/api")
    2124public class PlannerRestController {
     
    3134    public List<Planner> getAllPlanners(){
    3235        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());
    3343    }
    3444
     
    5767    @PostMapping(value = "/planner/new", consumes= MediaType.APPLICATION_JSON_VALUE)
    5868    @ResponseStatus(HttpStatus.CREATED)
    59     public ResponseEntity<Planner> newPlanner(@RequestBody PlannerDto plannerDto)  {
    60         return this.plannerService.newPlanner(plannerDto)
     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())
    6173                .map(planner -> ResponseEntity.ok().body(planner))
    6274                .orElseGet(() -> ResponseEntity.badRequest().build());
     
    6577
    6678    @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)
     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())
    6982                .map(planner -> ResponseEntity.ok().body(planner))
    7083                .orElseGet(()-> ResponseEntity.badRequest().build());
Note: See TracChangeset for help on using the changeset viewer.