154 lines
5.6 KiB
Java
Executable File
154 lines
5.6 KiB
Java
Executable File
package com.plannaplan.responses.mappers;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.function.Function;
|
|
import java.util.stream.Collectors;
|
|
|
|
import com.plannaplan.entities.Groups;
|
|
import com.plannaplan.responses.models.GroupDefaultResponse;
|
|
import com.plannaplan.responses.models.CourseWithGroupsResponse;
|
|
import com.plannaplan.responses.models.GroupWithCapacityResponse;
|
|
import com.plannaplan.types.GroupType;
|
|
|
|
/**
|
|
* Mappers for Groups to api responses
|
|
*/
|
|
public class GroupsMappers {
|
|
/**
|
|
* @param groups list of groups to be mapped
|
|
* @param taken ammoints to be take into account
|
|
* @return list of api responses
|
|
*/
|
|
public static List<GroupDefaultResponse> mapToDefaultResponse(List<Groups> groups, HashMap<Long, Integer> taken) {
|
|
return groups.stream().filter(Objects::nonNull).map(new Function<Groups, GroupDefaultResponse>() {
|
|
@Override
|
|
public GroupDefaultResponse apply(Groups p) {
|
|
if (taken != null) {
|
|
return new GroupDefaultResponse(p, taken.get(p.getId()));
|
|
} else {
|
|
return new GroupDefaultResponse(p);
|
|
}
|
|
}
|
|
}).collect(Collectors.toList());
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param groups list of groups to be mapped
|
|
* @return ammoints to be take into account
|
|
*/
|
|
public static List<GroupDefaultResponse> mapToDefaultResponse(List<Groups> groups) {
|
|
return GroupsMappers.mapToDefaultResponse(groups, null);
|
|
}
|
|
|
|
/**
|
|
* @param groups list of groups to be mapped
|
|
* @param taken ammoints to be take into account
|
|
* @return list of api responses
|
|
*/
|
|
public static List<GroupWithCapacityResponse> mapToCapacityResponse(List<Groups> groups,
|
|
HashMap<Long, Integer> taken) {
|
|
return groups.stream().filter(Objects::nonNull).map(new Function<Groups, GroupWithCapacityResponse>() {
|
|
@Override
|
|
public GroupWithCapacityResponse apply(Groups p) {
|
|
if (taken != null) {
|
|
return new GroupWithCapacityResponse(p, taken.get(p.getId()));
|
|
} else {
|
|
return new GroupWithCapacityResponse(p);
|
|
}
|
|
}
|
|
}).collect(Collectors.toList());
|
|
}
|
|
|
|
/**
|
|
* @param groups list of groups to be mapped
|
|
* @return ammoints to be take into account
|
|
*/
|
|
public static List<GroupWithCapacityResponse> mapToCapacityResponse(List<Groups> groups) {
|
|
return GroupsMappers.mapToCapacityResponse(groups, null);
|
|
}
|
|
|
|
/**
|
|
* @param groups list of groups to be mapped
|
|
* @param taken ammoints to be take into account
|
|
* @return list of api responses
|
|
*/
|
|
public static CourseWithGroupsResponse<GroupDefaultResponse> mapToGetCourseGroupsDefaultResponse(
|
|
List<Groups> groups, HashMap<Long, Integer> taken) {
|
|
|
|
List<GroupDefaultResponse> lectures = new ArrayList<>();
|
|
List<GroupDefaultResponse> classes = new ArrayList<>();
|
|
|
|
groups.stream().forEach(group -> {
|
|
if (GroupType.isLectureOrClass(group.getType()) == GroupType.CLASS) {
|
|
if (taken != null) {
|
|
classes.add(new GroupDefaultResponse(group, taken.get(group.getId())));
|
|
} else {
|
|
classes.add(new GroupDefaultResponse(group));
|
|
}
|
|
} else {
|
|
if (taken != null) {
|
|
lectures.add(new GroupDefaultResponse(group, taken.get(group.getId())));
|
|
} else {
|
|
lectures.add(new GroupDefaultResponse(group));
|
|
}
|
|
}
|
|
});
|
|
|
|
return new CourseWithGroupsResponse<>(classes, lectures);
|
|
}
|
|
|
|
/**
|
|
* @param groups list of groups to be mapped
|
|
* @return ammoints to be take into account
|
|
*/
|
|
public static CourseWithGroupsResponse<GroupDefaultResponse> mapToGetCourseGroupsDefaultResponse(
|
|
List<Groups> groups) {
|
|
return GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups, null);
|
|
}
|
|
|
|
/**
|
|
* @param groups list of groups to be mapped
|
|
* @param taken ammoints to be take into account
|
|
* @return list of api responses
|
|
*/
|
|
public static CourseWithGroupsResponse<GroupWithCapacityResponse> mapToGetCourseGroupsWithCapacityResponse(
|
|
List<Groups> groups, HashMap<Long, Integer> taken) {
|
|
|
|
List<GroupWithCapacityResponse> lectures = new ArrayList<>();
|
|
List<GroupWithCapacityResponse> classes = new ArrayList<>();
|
|
|
|
groups.stream().forEach(group -> {
|
|
if (GroupType.isLectureOrClass(group.getType()) == GroupType.CLASS) {
|
|
if (taken != null) {
|
|
classes.add(new GroupWithCapacityResponse(group, taken.get(group.getId())));
|
|
} else {
|
|
classes.add(new GroupWithCapacityResponse(group));
|
|
}
|
|
|
|
} else {
|
|
if (taken != null) {
|
|
lectures.add(new GroupWithCapacityResponse(group, taken.get(group.getId())));
|
|
} else {
|
|
lectures.add(new GroupWithCapacityResponse(group));
|
|
}
|
|
}
|
|
});
|
|
|
|
return new CourseWithGroupsResponse<>(classes, lectures);
|
|
}
|
|
|
|
/**
|
|
* @param groups list of groups to be mapped
|
|
* @return ammoints to be take into account
|
|
*/
|
|
public static CourseWithGroupsResponse<GroupWithCapacityResponse> mapToGetCourseGroupsWithCapacityResponse(
|
|
List<Groups> groups) {
|
|
return GroupsMappers.mapToGetCourseGroupsWithCapacityResponse(groups, null);
|
|
}
|
|
|
|
}
|