Added endpoint - waiting for other branch to merge to master

This commit is contained in:
BuildTools 2020-10-30 14:53:33 +01:00
parent 4267fadf62
commit ca01b20221
2 changed files with 44 additions and 27 deletions

View File

@ -49,4 +49,8 @@ public class UserService {
return this.repo.searchForUsers(query, UserRoles.STUDENT); return this.repo.searchForUsers(query, UserRoles.STUDENT);
} }
public Optional<User> getById(Long userId) {
return this.repo.findById(userId);
}
} }

View File

@ -2,6 +2,7 @@ package com.plannaplan.controllers;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
@ -21,6 +22,7 @@ import com.plannaplan.services.GroupService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -30,40 +32,51 @@ import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/api/" + App.API_VERSION + "/commisions") @RequestMapping("/api/" + App.API_VERSION + "/commisions")
public class CommisionController extends TokenBasedController { public class CommisionController extends TokenBasedController {
@Autowired @Autowired
private CommisionService commisionService; private CommisionService commisionService;
@Autowired @Autowired
private GroupService groupServcicxe; private GroupService groupServcicxe;
@Autowired @Autowired
private AssignmentService assignmentService; private AssignmentService assignmentService;
public CommisionController() { public CommisionController() {
} }
@PostMapping("/add") @PostMapping("/add")
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups) throws UserNotFoundException { public ResponseEntity<String> addCommision(@RequestBody List<Long> groups) throws UserNotFoundException {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
Commision com = new Commision(user); Commision com = new Commision(user);
this.commisionService.save(com); this.commisionService.save(com);
groups.stream().forEach((groupId) -> { groups.stream().forEach((groupId) -> {
Groups group = this.groupServcicxe.getGroupById(groupId).orElseThrow(() -> new NullPointerException()); Groups group = this.groupServcicxe.getGroupById(groupId)
Assignment a = new Assignment(group, com); .orElseThrow(() -> new NullPointerException());
this.assignmentService.save(a); Assignment a = new Assignment(group, com);
}); this.assignmentService.save(a);
});
return new ResponseEntity<>("Succes", HttpStatus.OK); return new ResponseEntity<>("Succes", HttpStatus.OK);
} }
@GetMapping("/getAllCommisions") @GetMapping("/getAllCommisions")
public ResponseEntity<List<CommisionResponse>> getAlCommisions() throws UserNotFoundException { public ResponseEntity<List<CommisionResponse>> getAlCommisions() throws UserNotFoundException {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
List<CommisionResponse> result = CommisionResponseMappers List<CommisionResponse> result = CommisionResponseMappers
.mapToResponse(this.commisionService.getUsersCommisions(user)); .mapToResponse(this.commisionService.getUsersCommisions(user));
return new ResponseEntity<>(result, HttpStatus.OK); return new ResponseEntity<>(result, HttpStatus.OK);
} }
@PreAuthorize("hasRole('ROLE_DEANERY')")
@GetMapping("/user/{id}")
public ResponseEntity<List<CommisionResponse>> getCommision(@PathVariable(name = "id") Long userId)
throws UserNotFoundException {
User user = this.userService.getById(userId).orElseThrow(() -> new NullPointerException());
List<CommisionResponse> result = CommisionResponseMappers
.mapToResponse(this.commisionService.getUsersCommisions(user));
return new ResponseEntity<>(result, HttpStatus.OK);
}
} }