package com.plannaplan.services; import java.util.HashMap; import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; import java.util.function.Function; import com.plannaplan.entities.Groups; import com.plannaplan.repositories.GroupRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Service of GroupService which can find(optional), get(By Course, Groups * Ammount, Group By Id, find Not Existing Group), save, delete group. */ @Service public class GroupService { @Autowired private GroupRepository repo; public GroupService() { } public Optional find(int time, int capacity, String room) { return this.repo.find(time, room, capacity); } public List getGroupsByCourse(Long id) { return this.repo.getByCourse(id); } public Groups save(Groups group) { return this.repo.save(group); } public void delete(Groups groups) { this.repo.delete(groups); } public int getGroupsAmmount() { return (int) this.repo.count(); } public Optional getGroupById(Long id) { return this.repo.findById(id); } public Optional findNotExistingGroup(List ids) { for (Long oneId : ids) { if (this.repo.existsById(oneId) == false) { return Optional.of(oneId); } } return Optional.empty(); } /** * * @param groups list of groups you want to get taken places ammount * @return HashMap where Long is group id and Integer is how many * places in gorup is already taken */ public HashMap getTakenPlaces(List groups) { HashMap response = new HashMap<>(); if (groups.size() == 0) { return response; } List respoonses = this.repo .getAssignedAmounts(groups.stream().filter(Objects::nonNull).map(new Function() { @Override public Long apply(Groups p) { final Long id = p.getId(); response.put(id, 0); return id; } }).collect(Collectors.toList())); for (Object[] element : respoonses) { response.put(((Groups) element[0]).getId(), ((Long) element[1]).intValue()); } return response; } }