little refactor

This commit is contained in:
BuildTools 2020-10-29 17:38:05 +01:00
parent 3a3e6dc427
commit ed00731654
4 changed files with 21 additions and 18 deletions

View File

@ -42,14 +42,13 @@ public class GroupService {
return this.repo.findById(id);
}
public Long findNotExistingGroup(List<Long> ids) {
Long response = Long.MIN_VALUE;
public Optional<Long> findNotExistingGroup(List<Long> ids) {
for (Long oneId : ids) {
if (this.repo.existsById(oneId) == false) {
response = oneId;
return Optional.of(oneId);
}
}
return response;
return Optional.empty();
}
}

View File

@ -32,7 +32,7 @@ public class App {
filip.setEmail("filizy@st.amu.edu.pl");
filip.setName("Filip");
filip.setSurname("Izydorczyk");
filip.setRole(UserRoles.DEANERY);
filip.setRole(UserRoles.STUDENT);
this.userService.save(filip);
User hub = new User();

View File

@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Optional;
import com.plannaplan.App;
import com.plannaplan.entities.Assignment;
@ -23,6 +24,7 @@ import com.plannaplan.types.UserRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@ -46,21 +48,23 @@ public class CommisionController extends TokenBasedController {
@PostMapping(value = { "/add", "/add/{id}" })
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups,
@PathVariable(name = "id", required = false) Long userId) throws UserNotFoundException {
@PathVariable(name = "id", required = false) Long userId)
throws UserNotFoundException, IllegalArgumentException {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
final User asker = this.getCurrentUser().orElseThrow(() -> new UserNotFoundException("Invalid token"));
if (userId != null && !((user.getId() == userId) || (user.getRole() == UserRoles.DEANERY))) {
return new ResponseEntity<>("Forbidden access atempt", HttpStatus.FORBIDDEN);
} else if (userId != null) {
user = userService.getById(userId).orElseThrow(() -> new UserNotFoundException("Given user id not exist"));
}
final User user = userId != null
? userService.getById(userId).orElseThrow(() -> new UserNotFoundException("Given user id not exist"))
: asker;
final Long notExistingGroup = this.groupServcicxe.findNotExistingGroup(groups);
if (notExistingGroup != Long.MIN_VALUE) {
return new ResponseEntity<>("Group " + notExistingGroup.toString() + " doesn't exist",
HttpStatus.NOT_FOUND);
}
Assert.isTrue(
(asker.getRole() == UserRoles.DEANERY && user.getRole() == UserRoles.STUDENT
|| (asker.getId() == user.getId() && user.getRole() == UserRoles.STUDENT)),
"Incorrect attempt to change plan");
Optional<Long> notExistingGroup = this.groupServcicxe.findNotExistingGroup(groups);
Assert.isTrue(!notExistingGroup.isPresent(),
"Group " + notExistingGroup.orElse(Long.MIN_VALUE).toString() + "doesn't exist");
final Commision com = new Commision(user);
this.commisionService.save(com);

View File

@ -29,7 +29,7 @@ public class ConfigController {
private ConfiguratorService contrl;
@PostMapping("/config")
// @PreAuthorize("hasRole('ROLE_ADMIN')")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public ResponseEntity<String> configApp(@RequestParam("file") MultipartFile file) {
try {