2020-07-23 17:27:20 +02:00
|
|
|
package com.plannaplan.services;
|
|
|
|
|
2020-11-22 03:51:24 +01:00
|
|
|
import java.util.HashMap;
|
2020-08-08 14:14:42 +02:00
|
|
|
import java.util.List;
|
2020-11-22 03:51:24 +01:00
|
|
|
import java.util.Objects;
|
2020-09-21 17:45:52 +02:00
|
|
|
import java.util.Optional;
|
2020-11-22 03:51:24 +01:00
|
|
|
import java.util.stream.Collectors;
|
2020-12-01 17:00:56 +01:00
|
|
|
import java.util.function.Function;
|
2020-08-08 14:14:42 +02:00
|
|
|
|
2020-12-29 16:36:12 +01:00
|
|
|
import com.plannaplan.entities.Assignment;
|
2020-08-06 15:18:18 +02:00
|
|
|
import com.plannaplan.entities.Groups;
|
2020-07-23 17:27:20 +02:00
|
|
|
import com.plannaplan.repositories.GroupRepository;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2020-11-20 13:20:44 +01:00
|
|
|
/**
|
2020-12-07 23:32:11 +01:00
|
|
|
* Service of GroupService which can find(optional), get(By Course, Groups
|
|
|
|
* Ammount, Group By Id, find Not Existing Group), save, delete group.
|
2020-11-20 13:20:44 +01:00
|
|
|
*/
|
|
|
|
|
2020-07-23 17:27:20 +02:00
|
|
|
@Service
|
|
|
|
public class GroupService {
|
|
|
|
@Autowired
|
|
|
|
private GroupRepository repo;
|
2020-08-06 15:18:18 +02:00
|
|
|
|
2020-08-08 00:08:36 +02:00
|
|
|
public GroupService() {
|
|
|
|
}
|
|
|
|
|
2020-09-21 17:45:52 +02:00
|
|
|
public Optional<Groups> find(int time, int capacity, String room) {
|
2020-08-06 15:18:18 +02:00
|
|
|
return this.repo.find(time, room, capacity);
|
|
|
|
}
|
|
|
|
|
2020-12-29 16:36:12 +01:00
|
|
|
public Optional<Groups> find(Integer zajCykId, Integer nrGr) {
|
2020-12-26 13:17:39 +01:00
|
|
|
return this.repo.find(zajCykId, nrGr);
|
|
|
|
}
|
|
|
|
|
2020-08-08 14:14:42 +02:00
|
|
|
public List<Groups> getGroupsByCourse(Long id) {
|
|
|
|
return this.repo.getByCourse(id);
|
|
|
|
}
|
|
|
|
|
2020-09-21 17:45:52 +02:00
|
|
|
public Groups save(Groups group) {
|
2020-11-17 19:36:56 +01:00
|
|
|
return this.repo.save(group);
|
2020-08-06 15:18:18 +02:00
|
|
|
}
|
2020-09-04 16:24:00 +02:00
|
|
|
|
2020-09-21 17:45:52 +02:00
|
|
|
public void delete(Groups groups) {
|
2020-09-16 17:23:33 +02:00
|
|
|
this.repo.delete(groups);
|
|
|
|
}
|
|
|
|
|
2020-09-21 17:45:52 +02:00
|
|
|
public int getGroupsAmmount() {
|
|
|
|
return (int) this.repo.count();
|
2020-09-04 16:24:00 +02:00
|
|
|
}
|
2020-09-29 17:21:34 +02:00
|
|
|
|
|
|
|
public Optional<Groups> getGroupById(Long id) {
|
|
|
|
return this.repo.findById(id);
|
2020-09-04 16:24:00 +02:00
|
|
|
}
|
2020-10-29 16:25:55 +01:00
|
|
|
|
2020-10-29 17:38:05 +01:00
|
|
|
public Optional<Long> findNotExistingGroup(List<Long> ids) {
|
2020-10-29 16:25:55 +01:00
|
|
|
for (Long oneId : ids) {
|
|
|
|
if (this.repo.existsById(oneId) == false) {
|
2020-10-29 17:38:05 +01:00
|
|
|
return Optional.of(oneId);
|
2020-10-29 16:25:55 +01:00
|
|
|
}
|
|
|
|
}
|
2020-10-29 17:38:05 +01:00
|
|
|
return Optional.empty();
|
2020-10-29 16:25:55 +01:00
|
|
|
|
|
|
|
}
|
2020-11-17 19:36:56 +01:00
|
|
|
|
2020-12-29 16:36:12 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param assingemnts list of assingemnts you want to get taken places ammount
|
|
|
|
* @return HashMap<Long, Integer> where Long is group id and Integer is how many
|
|
|
|
* places in gorup is already taken
|
|
|
|
*/
|
|
|
|
public HashMap<Long, Integer> getTakenPlacesOfAssignments(List<Assignment> assignments) {
|
|
|
|
return getTakenPlaces(assignments.stream().map(Assignment::getGroup).collect(Collectors.toList()));
|
|
|
|
}
|
|
|
|
|
2020-11-27 16:55:15 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param groups list of groups you want to get taken places ammount
|
|
|
|
* @return HashMap<Long, Integer> where Long is group id and Integer is how many
|
|
|
|
* places in gorup is already taken
|
|
|
|
*/
|
|
|
|
public HashMap<Long, Integer> getTakenPlaces(List<Groups> groups) {
|
|
|
|
HashMap<Long, Integer> response = new HashMap<>();
|
|
|
|
|
2020-12-07 23:32:11 +01:00
|
|
|
if (groups.size() == 0) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:00:56 +01:00
|
|
|
List<Object[]> respoonses = this.repo
|
|
|
|
.getAssignedAmounts(groups.stream().filter(Objects::nonNull).map(new Function<Groups, Long>() {
|
|
|
|
@Override
|
|
|
|
public Long apply(Groups p) {
|
|
|
|
final Long id = p.getId();
|
|
|
|
response.put(id, 0);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}).collect(Collectors.toList()));
|
2020-11-26 13:58:27 +01:00
|
|
|
|
2020-11-27 16:55:15 +01:00
|
|
|
for (Object[] element : respoonses) {
|
|
|
|
response.put(((Groups) element[0]).getId(), ((Long) element[1]).intValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
2020-11-17 19:36:56 +01:00
|
|
|
}
|
2020-07-23 17:27:20 +02:00
|
|
|
}
|