backend/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java
2020-11-22 03:51:24 +01:00

70 lines
1.9 KiB
Java
Executable File

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 com.plannaplan.entities.Groups;
import com.plannaplan.repositories.GroupRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class GroupService {
@Autowired
private GroupRepository repo;
public GroupService() {
}
public Optional<Groups> find(int time, int capacity, String room) {
return this.repo.find(time, room, capacity);
}
public List<Groups> 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<Groups> getGroupById(Long id) {
return this.repo.findById(id);
}
public Optional<Long> findNotExistingGroup(List<Long> ids) {
for (Long oneId : ids) {
if (this.repo.existsById(oneId) == false) {
return Optional.of(oneId);
}
}
return Optional.empty();
}
public HashMap<Groups, Integer> getTakenPlaces(List<Groups> groups) {
HashMap<Groups, Integer> respoonse = new HashMap<>();
// groups.forEach(group -> {
// final Long id = group.getId();
// final Integer ammount =
// this.repo.getAssignedAmount(id).orElse(Integer.valueOf(0)).intValue();
// respoonse.put(group, ammount);
// });
List<Object[]> respoonse2 = this.repo.getAssignedAmounts(
groups.stream().filter(Objects::nonNull).map(Groups::getId).collect(Collectors.toList()));
return respoonse;
}
}