86 lines
2.3 KiB
Java
Executable File
86 lines
2.3 KiB
Java
Executable File
package com.plannaplan.services;
|
|
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
|
|
import com.plannaplan.entities.Assignment;
|
|
import com.plannaplan.entities.Commision;
|
|
import com.plannaplan.entities.Groups;
|
|
import com.plannaplan.entities.User;
|
|
import com.plannaplan.repositories.AssignmentRepository;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
/**
|
|
* Service of Assignment which can save assignments, diplay assignments, get
|
|
* ammount of assigments.
|
|
*/
|
|
|
|
@Service
|
|
public class AssignmentService {
|
|
@Autowired
|
|
private AssignmentRepository repo;
|
|
|
|
@Autowired
|
|
private CommisionService service;
|
|
|
|
@Autowired
|
|
private UserService userService;
|
|
|
|
public AssignmentService() {
|
|
}
|
|
|
|
/**
|
|
* Save given assignment
|
|
*
|
|
* @param assignment assignment to save
|
|
* @return assignment saved assignment with database id
|
|
*/
|
|
public Assignment save(Assignment assignment) {
|
|
return this.repo.save(assignment);
|
|
}
|
|
|
|
/*
|
|
* getCommisionAssignments Return id of the commision
|
|
*/
|
|
public List<Assignment> getCommisionAssignments(Commision com) {
|
|
return this.repo.getByCommision(com.getId());
|
|
}
|
|
|
|
/*
|
|
* getAssignmentsAmmount Return count assignments ammount
|
|
*/
|
|
public long getAssignmentsAmmount() {
|
|
return this.repo.count();
|
|
}
|
|
|
|
/**
|
|
* Get assigmnent by id
|
|
*
|
|
* @param id id of assigmnent
|
|
* @return Optional of assignment
|
|
*/
|
|
public Optional<Assignment> getById(Long id) {
|
|
return this.repo.findById(id);
|
|
}
|
|
|
|
public void callAcceptAlgorythm() {
|
|
final List<User> students = this.userService.getStudentsSortedByRanking();
|
|
|
|
students.forEach(e -> {
|
|
final Optional<Commision> com = this.service.getNewestCommision(e);
|
|
if (com.isPresent()) {
|
|
final List<Assignment> assignments = this.getCommisionAssignments(com.get());
|
|
assignments.forEach(a -> {
|
|
final Groups group = a.getGroup();
|
|
if (group.getCapacity() > group.getRegisteredStudents().size()) {
|
|
e.claimGroup(group);
|
|
this.userService.save(e);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|