From 98d6faf555f104b300c08504e9078d518364d6e1 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Sat, 16 Jan 2021 16:29:24 +0100 Subject: [PATCH] Worked blocking accepted assignment --- .../plannaplan/services/CommisionService.java | 24 ------------------- .../controllers/CommisionController.java | 22 +++++++++++++++++ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java b/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java index 6f35660..2f01a46 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java @@ -1,11 +1,8 @@ package com.plannaplan.services; -import java.util.Iterator; import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; -import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; import com.plannaplan.entities.User; import com.plannaplan.repositories.AssignmentRepository; @@ -39,33 +36,12 @@ public class CommisionService { public Commision save(Commision commision) { Optional lastCommision = this.getNewestCommision(commision.getCommisionOwner()); if (lastCommision.isPresent()) { - - final List assignments = commision.getAssignments(); - List newAssignments = null; - if (assignments != null) { - newAssignments = assignments.stream().map(Assignment::getId).collect(Collectors.toList()); - } final Commision lastCom = lastCommision.get(); lastCom.getAssignments().forEach(assignment -> { assignment.setPastAssignment(true); this.aRepository.save(assignment); }); - final List lastComAssignments = lastCom.getAssignments(); - if (newAssignments != null) { - Iterator 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); return commision; diff --git a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java index 2ce264a..583acd4 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java @@ -39,6 +39,7 @@ import org.springframework.web.bind.annotation.RequestParam; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import com.plannaplan.responses.mappers.AssignmentResponseMappers; import com.plannaplan.responses.models.AssignmentResponse; @@ -99,16 +100,37 @@ public class CommisionController extends TokenBasedController { Assert.isTrue(!notExistingGroup.isPresent(), "Group " + notExistingGroup.orElse(Long.MIN_VALUE).toString() + "doesn't exist"); + final Optional comPrev = this.commisionService.getNewestCommision(user); final Commision com = new Commision(user, asker); this.commisionService.save(com); + final List addedGroups = new ArrayList<>(); groups.stream().forEach((groupId) -> { Groups group = this.groupServcicxe.getGroupById(groupId) .orElseThrow(() -> new NullPointerException()); Assignment a = new Assignment(group, com); this.assignmentService.save(a); + addedGroups.add(groupId); }); + boolean isBad = false; + if (comPrev.isPresent()) { + final Iterator 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); } catch (UserNotFoundException exception) { return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND);