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); return this.repo.findById(id);
} }
public Long findNotExistingGroup(List<Long> ids) { public Optional<Long> findNotExistingGroup(List<Long> ids) {
Long response = Long.MIN_VALUE;
for (Long oneId : ids) { for (Long oneId : ids) {
if (this.repo.existsById(oneId) == false) { 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.setEmail("filizy@st.amu.edu.pl");
filip.setName("Filip"); filip.setName("Filip");
filip.setSurname("Izydorczyk"); filip.setSurname("Izydorczyk");
filip.setRole(UserRoles.DEANERY); filip.setRole(UserRoles.STUDENT);
this.userService.save(filip); this.userService.save(filip);
User hub = new User(); 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 org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
import java.util.Optional;
import com.plannaplan.App; import com.plannaplan.App;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
@ -23,6 +24,7 @@ import com.plannaplan.types.UserRoles;
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.util.Assert;
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;
@ -46,21 +48,23 @@ public class CommisionController extends TokenBasedController {
@PostMapping(value = { "/add", "/add/{id}" }) @PostMapping(value = { "/add", "/add/{id}" })
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups, 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))) { final User user = userId != null
return new ResponseEntity<>("Forbidden access atempt", HttpStatus.FORBIDDEN); ? userService.getById(userId).orElseThrow(() -> new UserNotFoundException("Given user id not exist"))
} else if (userId != null) { : asker;
user = userService.getById(userId).orElseThrow(() -> new UserNotFoundException("Given user id not exist"));
}
final Long notExistingGroup = this.groupServcicxe.findNotExistingGroup(groups); Assert.isTrue(
if (notExistingGroup != Long.MIN_VALUE) { (asker.getRole() == UserRoles.DEANERY && user.getRole() == UserRoles.STUDENT
return new ResponseEntity<>("Group " + notExistingGroup.toString() + " doesn't exist", || (asker.getId() == user.getId() && user.getRole() == UserRoles.STUDENT)),
HttpStatus.NOT_FOUND); "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); final Commision com = new Commision(user);
this.commisionService.save(com); this.commisionService.save(com);

View File

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