backend/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java

107 lines
3.1 KiB
Java
Raw Normal View History

package com.plannaplan.services;
import java.util.HashMap;
2020-08-08 14:14:42 +02:00
import java.util.List;
import java.util.Objects;
import java.util.Optional;
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;
import com.plannaplan.entities.Groups;
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
/**
* 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
*/
@Service
public class GroupService {
@Autowired
private GroupRepository repo;
2020-08-08 00:08:36 +02:00
public GroupService() {
}
public Optional<Groups> find(int time, int capacity, String room) {
return this.repo.find(time, room, capacity);
}
2020-12-29 16:36:12 +01:00
public Optional<Groups> find(Integer zajCykId, Integer nrGr) {
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);
}
public Groups save(Groups group) {
2020-11-17 19:36:56 +01:00
return this.repo.save(group);
}
2020-09-04 16:24:00 +02:00
public void delete(Groups groups) {
this.repo.delete(groups);
}
public int getGroupsAmmount() {
return (int) this.repo.count();
2020-09-04 16:24:00 +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 assignments list of assignments you want to get taken places ammount
* @return HashMap of Long to Integer where Long is group id and Integer is how
* many places in gorup is already taken
2020-12-29 16:36:12 +01:00
*/
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 of Long to Integer where Long is group id and Integer is how
* many places in gorup is already taken
2020-11-27 16:55:15 +01:00
*/
public HashMap<Long, Integer> getTakenPlaces(List<Groups> groups) {
HashMap<Long, Integer> response = new HashMap<>();
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
}
}