diff --git a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java index 6be9f55..42d2742 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java @@ -41,10 +41,15 @@ public class GroupController { @RequestParam(name = "capacity", defaultValue = "true") @ApiParam(value = "Boolean if we want to have capacity field in response") Boolean capacity, @RequestParam(name = "takenPlaces", defaultValue = "false") @ApiParam(value = "Boolean if we want to have respoonse with information about taken places by other students") Boolean takenPlaces) { List groups = this.groupService.getGroupsByCourse(id); - HashMap ammounts; if (takenPlaces) { - ammounts = this.groupService.getTakenPlaces(groups); + HashMap ammounts = this.groupService.getTakenPlaces(groups); + if (capacity) { + return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsWithCapacityResponse(groups, ammounts), + HttpStatus.OK); + } + return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups, ammounts), + HttpStatus.OK); } if (capacity) { diff --git a/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java b/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java index 4144fd2..43779d3 100755 --- a/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java @@ -80,20 +80,34 @@ public class GroupsMappers { } public static CourseWithGroupsResponse mapToGetCourseGroupsWithCapacityResponse( - List groups) { + List groups, HashMap taken) { List lectures = new ArrayList<>(); List classes = new ArrayList<>(); groups.stream().forEach(group -> { if (group.getType() == GroupType.CLASS) { - classes.add(new GroupWithCapacityResponse(group)); + if (taken != null) { + classes.add(new GroupWithCapacityResponse(group, taken.get(group.getId()))); + } else { + classes.add(new GroupWithCapacityResponse(group)); + } + } else { - lectures.add(new GroupWithCapacityResponse(group)); + if (taken != null) { + lectures.add(new GroupWithCapacityResponse(group, taken.get(group.getId()))); + } else { + lectures.add(new GroupWithCapacityResponse(group)); + } } }); return new CourseWithGroupsResponse<>(classes, lectures); } + public static CourseWithGroupsResponse mapToGetCourseGroupsWithCapacityResponse( + List groups) { + return GroupsMappers.mapToGetCourseGroupsWithCapacityResponse(groups, null); + } + } diff --git a/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java b/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java index d3311de..174c147 100755 --- a/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java +++ b/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java @@ -81,6 +81,35 @@ public class GroupsMappersTest { assertTrue(response.getClasses().get(0).getTakenPlaces() == 21); } + @Test + public void shouldMapToGetCourseCapacityGroupsResponseWithTakenPlaces() throws NoSuchFieldException, + SecurityException, IllegalArgumentException, IllegalAccessException { + final Field reader = Groups.class.getDeclaredField("id"); + reader.setAccessible(true); + + final List groups = Arrays.asList( + new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")), + new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki"))); + reader.set(groups.get(0), Long.valueOf(0)); + reader.set(groups.get(1), Long.valueOf(1)); + + final HashMap placeMap = new HashMap<>(); + placeMap.put(Long.valueOf(0), 0); + placeMap.put(Long.valueOf(1), 21); + + final CourseWithGroupsResponse response = GroupsMappers + .mapToGetCourseGroupsWithCapacityResponse(groups, placeMap); + + assertTrue(response.getClasses().size() == 1); + assertTrue(response.getLectures().size() == 1); + assertTrue(response.getLectures().get(0).getTakenPlaces() == 0); + assertTrue(response.getLectures().get(0).getCapacity() == 150); + assertTrue(response.getClasses().get(0).getTakenPlaces() == 21); + assertTrue(response.getClasses().get(0).getCapacity() == 24); + } + @Test public void shouldMapToDefaultResponseWithTakenPlace() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {