Worked blocking accepted assignment

This commit is contained in:
Filip Izydorczyk 2021-01-16 16:29:24 +01:00
parent 2cfe0e7551
commit 98d6faf555
2 changed files with 22 additions and 24 deletions

View File

@ -1,11 +1,8 @@
package com.plannaplan.services; package com.plannaplan.services;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import com.plannaplan.entities.User; import com.plannaplan.entities.User;
import com.plannaplan.repositories.AssignmentRepository; import com.plannaplan.repositories.AssignmentRepository;
@ -39,33 +36,12 @@ public class CommisionService {
public Commision save(Commision commision) { public Commision save(Commision commision) {
Optional<Commision> lastCommision = this.getNewestCommision(commision.getCommisionOwner()); Optional<Commision> lastCommision = this.getNewestCommision(commision.getCommisionOwner());
if (lastCommision.isPresent()) { if (lastCommision.isPresent()) {
final List<Assignment> assignments = commision.getAssignments();
List<Long> newAssignments = null;
if (assignments != null) {
newAssignments = assignments.stream().map(Assignment::getId).collect(Collectors.toList());
}
final Commision lastCom = lastCommision.get(); final Commision lastCom = lastCommision.get();
lastCom.getAssignments().forEach(assignment -> { lastCom.getAssignments().forEach(assignment -> {
assignment.setPastAssignment(true); assignment.setPastAssignment(true);
this.aRepository.save(assignment); this.aRepository.save(assignment);
}); });
final List<Assignment> lastComAssignments = lastCom.getAssignments();
if (newAssignments != null) {
Iterator<Assignment> newAssignmentsIterator = lastComAssignments.iterator();
while (newAssignmentsIterator.hasNext()) {
final Assignment a = newAssignmentsIterator.next();
if (!newAssignments.contains(a.getId()) && a.isAccepted()) {
final Assignment assignment = Assignment.getNewFromAssignment(a, commision);
this.aRepository.save(assignment);
}
}
}
} }
this.repo.save(commision); this.repo.save(commision);
return commision; return commision;

View File

@ -39,6 +39,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import com.plannaplan.responses.mappers.AssignmentResponseMappers; import com.plannaplan.responses.mappers.AssignmentResponseMappers;
import com.plannaplan.responses.models.AssignmentResponse; import com.plannaplan.responses.models.AssignmentResponse;
@ -99,16 +100,37 @@ public class CommisionController extends TokenBasedController {
Assert.isTrue(!notExistingGroup.isPresent(), "Group " Assert.isTrue(!notExistingGroup.isPresent(), "Group "
+ notExistingGroup.orElse(Long.MIN_VALUE).toString() + "doesn't exist"); + notExistingGroup.orElse(Long.MIN_VALUE).toString() + "doesn't exist");
final Optional<Commision> comPrev = this.commisionService.getNewestCommision(user);
final Commision com = new Commision(user, asker); final Commision com = new Commision(user, asker);
this.commisionService.save(com); this.commisionService.save(com);
final List<Long> addedGroups = new ArrayList<>();
groups.stream().forEach((groupId) -> { groups.stream().forEach((groupId) -> {
Groups group = this.groupServcicxe.getGroupById(groupId) Groups group = this.groupServcicxe.getGroupById(groupId)
.orElseThrow(() -> new NullPointerException()); .orElseThrow(() -> new NullPointerException());
Assignment a = new Assignment(group, com); Assignment a = new Assignment(group, com);
this.assignmentService.save(a); this.assignmentService.save(a);
addedGroups.add(groupId);
}); });
boolean isBad = false;
if (comPrev.isPresent()) {
final Iterator<Assignment> it = comPrev.get().getAssignments().iterator();
while (it.hasNext() && !isBad) {
final Assignment a = it.next();
if (a.isAccepted() && !addedGroups.contains(a.getId())) {
isBad = true;
}
}
}
if (isBad) {
return new ResponseEntity<>("Error. You can't delete accepted assignmet",
HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>("Succes", HttpStatus.OK); return new ResponseEntity<>("Succes", HttpStatus.OK);
} catch (UserNotFoundException exception) { } catch (UserNotFoundException exception) {
return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND); return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND);