diff --git a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java index 478b6c1..90cbdda 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java @@ -6,6 +6,7 @@ import com.plannaplan.App; import com.plannaplan.entities.Groups; import com.plannaplan.responses.mappers.GroupsMappers; import com.plannaplan.responses.models.DefaultGroupResponse; +import com.plannaplan.responses.models.GetCourseGroupsResponse; import com.plannaplan.services.GroupService; import org.springframework.beans.factory.annotation.Autowired; @@ -25,12 +26,12 @@ public class GroupController { private GroupService groupService; @GetMapping("/getCourseGroups") - public ResponseEntity> getCourses(@RequestParam("id") Long id, + public ResponseEntity> getCourses(@RequestParam("id") Long id, @RequestParam(name = "capacity", defaultValue = "true") Boolean capacity) { List groups = this.groupService.getGroupsByCourse(id); if (capacity) { - return new ResponseEntity<>(GroupsMappers.mapToCapacityResponse(groups), HttpStatus.OK); + return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsWithCapacityResponse(groups), HttpStatus.OK); } - return new ResponseEntity<>(GroupsMappers.mapToDefaultResponse(groups), HttpStatus.OK); + return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups), HttpStatus.OK); } } \ No newline at end of file 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 415be62..401a5a1 100755 --- a/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java @@ -1,12 +1,15 @@ package com.plannaplan.responses.mappers; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; import com.plannaplan.entities.Groups; import com.plannaplan.responses.models.DefaultGroupResponse; +import com.plannaplan.responses.models.GetCourseGroupsResponse; import com.plannaplan.responses.models.WithCapacityGroupResponse; +import com.plannaplan.types.GroupType; public class GroupsMappers { public static List mapToDefaultResponse(List groups) { @@ -17,4 +20,39 @@ public class GroupsMappers { return groups.stream().filter(Objects::nonNull).map(WithCapacityGroupResponse::new) .collect(Collectors.toList()); } + + public static GetCourseGroupsResponse mapToGetCourseGroupsDefaultResponse (List groups){ + + List lectures = new ArrayList<>(); + List classes = new ArrayList<>(); + + groups.stream().forEach(group -> { + if (group.getType() == GroupType.CLASS) { + classes.add(new DefaultGroupResponse(group)); + } else { + lectures.add(new DefaultGroupResponse(group)); + } + }); + + + return new GetCourseGroupsResponse<>(classes, lectures); + } + + public static GetCourseGroupsResponse mapToGetCourseGroupsWithCapacityResponse (List groups){ + + List lectures = new ArrayList<>(); + List classes = new ArrayList<>(); + + groups.stream().forEach(group -> { + if (group.getType() == GroupType.CLASS) { + classes.add(new WithCapacityGroupResponse(group)); + } else { + lectures.add(new WithCapacityGroupResponse(group)); + } + }); + + + return new GetCourseGroupsResponse<>(classes, lectures); + } + } diff --git a/restservice/src/main/java/com/plannaplan/responses/models/GetCourseGroupsResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/GetCourseGroupsResponse.java new file mode 100644 index 0000000..c07bede --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/responses/models/GetCourseGroupsResponse.java @@ -0,0 +1,24 @@ +package com.plannaplan.responses.models; + +import java.util.ArrayList; +import java.util.List; + +public class GetCourseGroupsResponse { + + private List lectures = new ArrayList<>(); + private List classes = new ArrayList<>(); + + public GetCourseGroupsResponse(List classes, List lectures ){ + this.lectures = lectures; + this.classes = classes; + } + + public List getClasses() { + return this.classes; + } + + public List getLectures() { + return this.lectures; + } + +} \ No newline at end of file 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 3f735a6..cefc561 100755 --- a/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java +++ b/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java @@ -1,11 +1,14 @@ package com.plannaplan.responses.mappers; +import static org.junit.Assert.assertTrue; + import java.util.Arrays; import java.util.List; import com.plannaplan.entities.Groups; import com.plannaplan.entities.Lecturer; import com.plannaplan.responses.models.DefaultGroupResponse; +import com.plannaplan.responses.models.GetCourseGroupsResponse; import com.plannaplan.responses.models.WithCapacityGroupResponse; import com.plannaplan.types.WeekDay; @@ -32,4 +35,18 @@ public class GroupsMappersTest { assert (response.get(0) instanceof DefaultGroupResponse); assert (response.size() == 1); } + + @Test + public void shouldMapToGetCourseGroupsResponse(){ + 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"))); + + final GetCourseGroupsResponse response = GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups); + + assertTrue(response.getClasses().size() == 1); + assertTrue(response.getLectures().size() == 1); + + } + } + diff --git a/restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupsResponseTest.java b/restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupsResponseTest.java new file mode 100644 index 0000000..4da18db --- /dev/null +++ b/restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupsResponseTest.java @@ -0,0 +1,28 @@ +package com.plannaplan.responses.models; + +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.List; + +import com.plannaplan.entities.Groups; + +import org.junit.Test; + +public class GetCourseGroupsResponseTest { + + @Test public void shouldCreateInstance(){ + + final List classes = Arrays.asList(new DefaultGroupResponse(new Groups())); + + final List lectures = Arrays.asList(new DefaultGroupResponse(new Groups())); + + final GetCourseGroupsResponse instance = new GetCourseGroupsResponse<>(classes,lectures); + + assertTrue(instance.getClasses().get(0) instanceof DefaultGroupResponse); + assertTrue(instance.getLectures().get(0) instanceof DefaultGroupResponse); + + } + + +}