Added flaging assignment. Coumetr froup infor needs to be added

This commit is contained in:
BuildTools 2020-11-14 16:52:22 +01:00
parent 1ddeb83cb4
commit c34ce94fb0
7 changed files with 97 additions and 16 deletions

View File

@ -18,12 +18,17 @@ public class Assignment {
@ManyToOne @ManyToOne
@JoinColumn(name = "commision_id") @JoinColumn(name = "commision_id")
private Commision commision; private Commision commision;
private boolean isPastAssignment;
public Assignment(Groups group, Commision commision) { public Assignment(Groups group, Commision commision, boolean isPastAssignment) {
this.commision = commision; this.commision = commision;
this.group = group; this.group = group;
} }
public Assignment(Groups group, Commision commision) {
this(group, commision, false);
}
public Assignment() { public Assignment() {
} }
@ -34,4 +39,12 @@ public class Assignment {
public Groups getGroup() { public Groups getGroup() {
return this.group; return this.group;
} }
public boolean isPastAssignment() {
return isPastAssignment;
}
public void setPastAssignment(boolean isPastAssignment) {
this.isPastAssignment = isPastAssignment;
}
} }

View File

@ -4,6 +4,7 @@ import java.sql.Timestamp;
import java.util.List; import java.util.List;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
@ -21,9 +22,14 @@ public class Commision {
private User commisionOwner; private User commisionOwner;
private Timestamp commisionDate; private Timestamp commisionDate;
@OneToMany(mappedBy = "commision") @OneToMany(mappedBy = "commision", fetch = FetchType.EAGER)
private List<Assignment> assignments; private List<Assignment> assignments;
/**
*
* @param user owner of commission. Can not be null otherwise saving commision
* will fail.
*/
public Commision(User user) { public Commision(User user) {
this.commisionDate = new Timestamp(System.currentTimeMillis()); this.commisionDate = new Timestamp(System.currentTimeMillis());
this.commisionOwner = user; this.commisionOwner = user;
@ -44,4 +50,8 @@ public class Commision {
return commisionOwner; return commisionOwner;
} }
public List<Assignment> getAssignments() {
return this.assignments;
}
} }

View File

@ -1,6 +1,7 @@
package com.plannaplan.services; package com.plannaplan.services;
import java.util.List; import java.util.List;
import java.util.Optional;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
@ -17,8 +18,8 @@ public class AssignmentService {
public AssignmentService() { public AssignmentService() {
} }
public void save(Assignment assignment) { public Assignment save(Assignment assignment) {
this.repo.save(assignment); return this.repo.save(assignment);
} }
public List<Assignment> getCommisionAssignments(Commision com) { public List<Assignment> getCommisionAssignments(Commision com) {
@ -28,4 +29,8 @@ public class AssignmentService {
public long getAssignmentsAmmount() { public long getAssignmentsAmmount() {
return this.repo.count(); return this.repo.count();
} }
public Optional<Assignment> getById(Long id) {
return this.repo.findById(id);
}
} }

View File

@ -5,6 +5,7 @@ import java.util.Optional;
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.CommisionRepository; import com.plannaplan.repositories.CommisionRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -14,11 +15,21 @@ import org.springframework.stereotype.Service;
public class CommisionService { public class CommisionService {
@Autowired @Autowired
private CommisionRepository repo; private CommisionRepository repo;
@Autowired
private AssignmentRepository aRepository;
public CommisionService() { public CommisionService() {
} }
public Commision save(Commision commision) { public Commision save(Commision commision) {
Optional<Commision> lastCommision = this.getNewestCommision(commision.getCommisionOwner());
if (lastCommision.isPresent()) {
final Commision lastCom = lastCommision.get();
lastCom.getAssignments().forEach(assignment -> {
assignment.setPastAssignment(true);
this.aRepository.save(assignment);
});
}
this.repo.save(commision); this.repo.save(commision);
return commision; return commision;

View File

@ -11,8 +11,9 @@ import java.util.List;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import com.plannaplan.entities.User;
import com.plannaplan.types.UserRoles;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -26,24 +27,31 @@ public class AssignmentServiceTest {
private AssignmentService service; private AssignmentService service;
@Autowired @Autowired
private CommisionService comServie; private CommisionService comServie;
private Commision com; @Autowired
private UserService userService;
@Before
public void init() {
this.com = new Commision();
this.comServie.save(this.com);
}
@Test @Test
public void shouldSaveAssignment() { public void shouldSaveAssignment() {
final User user = new User("Gibi", "Kovalsky", "shouldSaveAssignment@assignmentservice.test",
UserRoles.STUDENT);
this.userService.save(user);
long beginState = this.service.getAssignmentsAmmount(); long beginState = this.service.getAssignmentsAmmount();
this.addAssignmentToCommision(this.com); Commision com = new Commision(user);
this.comServie.save(com);
this.addAssignmentToCommision(com);
assertTrue("Assign ammount should increase", this.service.getAssignmentsAmmount() > beginState); assertTrue("Assign ammount should increase", this.service.getAssignmentsAmmount() > beginState);
} }
@Test @Test
public void shouldGetCommisionAssignments() { public void shouldGetCommisionAssignments() {
this.addAssignmentToCommision(this.com); final User user = new User("Gibi", "Kovalsky", "shouldGetCommisionAssignments@assignmentservice.test",
UserRoles.STUDENT);
this.userService.save(user);
Commision com = new Commision(user);
this.comServie.save(com);
this.addAssignmentToCommision(com);
final List<Assignment> response = this.service.getCommisionAssignments(com); final List<Assignment> response = this.service.getCommisionAssignments(com);
assertTrue("Returned list size should be 1", response.size() == 1); assertTrue("Returned list size should be 1", response.size() == 1);
} }

View File

@ -7,8 +7,10 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
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.types.UserRoles;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -24,10 +26,15 @@ public class CommisionServiceTest {
@Autowired @Autowired
private UserService userService; private UserService userService;
@Autowired
private AssignmentService assignmentService;
@Test @Test
public void shouldSaveCommision() { public void shouldSaveCommision() {
User usr = new User();
this.userService.save(usr);
long beginState = this.service.getCommisionsAmmount(); long beginState = this.service.getCommisionsAmmount();
this.service.save(new Commision()); this.service.save(new Commision(usr));
assertTrue("Commision ammount should have changed", this.service.getCommisionsAmmount() > beginState); assertTrue("Commision ammount should have changed", this.service.getCommisionsAmmount() > beginState);
} }
@ -57,4 +64,31 @@ public class CommisionServiceTest {
assertTrue("Wrong commision was returned", result.getId().equals(newestCommision.getId())); assertTrue("Wrong commision was returned", result.getId().equals(newestCommision.getId()));
} }
@Test
public void shouldMarkAssignmentsOfPreviousCommisionPast() {
final User usr = new User("Trevor", "Hammolt",
"shouldMarkAssignmentsOfPreviousCommisionPast@commisionservice.test", UserRoles.STUDENT);
this.userService.save(usr);
final Commision firstCommision = new Commision(usr);
this.service.save(firstCommision);
Assignment firstCommisionAssignment = this.assignmentService.save(new Assignment(null, firstCommision));
assertTrue("FirstCommisionAssignment should be flaged as present assignment",
!firstCommisionAssignment.isPastAssignment());
final Commision secondCommision = new Commision(usr);
this.service.save(secondCommision);
Assignment secondCommisionAssignment = new Assignment(null, secondCommision);
this.assignmentService.save(secondCommisionAssignment);
assertTrue("SecondCommisionAssignment should be flaged as present assignment",
!secondCommisionAssignment.isPastAssignment());
assertTrue("FirstCommisionAssignment should be flaged as past assignment",
this.assignmentService.getById(firstCommisionAssignment.getId()).get().isPastAssignment());
}
} }

View File

@ -33,7 +33,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.ADMIN); filip.setRole(UserRoles.STUDENT);
this.userService.save(filip); this.userService.save(filip);
User hub = new User(); User hub = new User();