Added taken place in course response (needs refactor later)
This commit is contained in:
parent
48394d3583
commit
c34d53d056
@ -14,7 +14,8 @@ 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 of GroupService which can find(optional), get(By Course, Groups
|
||||
* Ammount, Group By Id, find Not Existing Group), save, delete group.
|
||||
*/
|
||||
|
||||
@Service
|
||||
@ -68,6 +69,10 @@ public class GroupService {
|
||||
public HashMap<Long, Integer> getTakenPlaces(List<Groups> groups) {
|
||||
HashMap<Long, Integer> response = new HashMap<>();
|
||||
|
||||
if (groups.size() == 0) {
|
||||
return response;
|
||||
}
|
||||
|
||||
List<Object[]> respoonses = this.repo
|
||||
.getAssignedAmounts(groups.stream().filter(Objects::nonNull).map(new Function<Groups, Long>() {
|
||||
@Override
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.plannaplan.controllers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import com.plannaplan.App;
|
||||
@ -7,7 +9,10 @@ import com.plannaplan.entities.Course;
|
||||
import com.plannaplan.responses.mappers.CoursesResponseMappers;
|
||||
import com.plannaplan.responses.models.CoursesDefaultResponse;
|
||||
import com.plannaplan.responses.models.CoursesWithGroupsResponse;
|
||||
import com.plannaplan.responses.models.GroupDefaultResponse;
|
||||
import com.plannaplan.services.CourseService;
|
||||
import com.plannaplan.services.GroupService;
|
||||
import com.plannaplan.types.GroupType;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@ -33,12 +38,40 @@ public class CoursesController {
|
||||
@Autowired
|
||||
private CourseService courseService;
|
||||
|
||||
@Autowired
|
||||
private GroupService groupService;
|
||||
|
||||
@GetMapping("/all")
|
||||
@ApiOperation(value = "Return all courses")
|
||||
public ResponseEntity<List<? extends CoursesResponse>> getMethodName(
|
||||
@RequestParam(name = "groups", defaultValue = "false") @ApiParam(value = "Boolean if you want to have resopnse with associated groups or without") Boolean groups) {
|
||||
@RequestParam(name = "groups", defaultValue = "false") @ApiParam(value = "Boolean if you want to have resopnse with associated groups or without") Boolean groups,
|
||||
@RequestParam(name = "takenPlaces", defaultValue = "false") @ApiParam(value = "Boolean if we want to have respoonse with information about taken places by other students. Needs to be set groups true first") Boolean takenPlaces) {
|
||||
List<Course> courses = this.courseService.getAllCourses();
|
||||
if (groups) {
|
||||
if (takenPlaces) {
|
||||
|
||||
final List<CoursesWithGroupsResponse> response = new ArrayList<>();
|
||||
|
||||
courses.forEach(course -> {
|
||||
final List<GroupDefaultResponse> lectures = new ArrayList<>();
|
||||
final List<GroupDefaultResponse> classes = new ArrayList<>();
|
||||
|
||||
final HashMap<Long, Integer> ammounts = this.groupService.getTakenPlaces(course.getGroups());
|
||||
|
||||
course.getGroups().stream().forEach(group -> {
|
||||
if (group.getType() == GroupType.CLASS) {
|
||||
classes.add(new GroupDefaultResponse(group, ammounts.get(group.getId())));
|
||||
} else {
|
||||
lectures.add(new GroupDefaultResponse(group, ammounts.get(group.getId())));
|
||||
}
|
||||
});
|
||||
|
||||
response.add(new CoursesWithGroupsResponse(course, lectures, classes));
|
||||
});
|
||||
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
}
|
||||
|
||||
final List<CoursesWithGroupsResponse> response = CoursesResponseMappers
|
||||
.mapToGetCoursesWithGroupsResponse(courses);
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
|
@ -26,6 +26,13 @@ public class CoursesWithGroupsResponse extends CoursesResponse {
|
||||
});
|
||||
}
|
||||
|
||||
public CoursesWithGroupsResponse(Course course, List<GroupDefaultResponse> lectures,
|
||||
List<GroupDefaultResponse> classes) {
|
||||
super(course);
|
||||
this.lectures = lectures;
|
||||
this.classes = classes;
|
||||
}
|
||||
|
||||
public List<GroupDefaultResponse> getClasses() {
|
||||
return this.classes;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user