Mapping of getCourseGroups

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2020-10-20 19:02:18 +02:00
parent a20bc89fc6
commit 603dadd9c1
6 changed files with 95 additions and 43 deletions

View File

@ -6,6 +6,7 @@ import com.plannaplan.App;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.responses.mappers.GroupsMappers; import com.plannaplan.responses.mappers.GroupsMappers;
import com.plannaplan.responses.models.DefaultGroupResponse; import com.plannaplan.responses.models.DefaultGroupResponse;
import com.plannaplan.responses.models.GetCourseGroupsResponse;
import com.plannaplan.services.GroupService; import com.plannaplan.services.GroupService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -25,12 +26,12 @@ public class GroupController {
private GroupService groupService; private GroupService groupService;
@GetMapping("/getCourseGroups") @GetMapping("/getCourseGroups")
public ResponseEntity<List<? extends DefaultGroupResponse>> getCourses(@RequestParam("id") Long id, public ResponseEntity<GetCourseGroupsResponse<? extends DefaultGroupResponse>> getCourses(@RequestParam("id") Long id,
@RequestParam(name = "capacity", defaultValue = "true") Boolean capacity) { @RequestParam(name = "capacity", defaultValue = "true") Boolean capacity) {
List<Groups> groups = this.groupService.getGroupsByCourse(id); List<Groups> groups = this.groupService.getGroupsByCourse(id);
if (capacity) { 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);
} }
} }

View File

@ -1,12 +1,15 @@
package com.plannaplan.responses.mappers; package com.plannaplan.responses.mappers;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.responses.models.DefaultGroupResponse; import com.plannaplan.responses.models.DefaultGroupResponse;
import com.plannaplan.responses.models.GetCourseGroupsResponse;
import com.plannaplan.responses.models.WithCapacityGroupResponse; import com.plannaplan.responses.models.WithCapacityGroupResponse;
import com.plannaplan.types.GroupType;
public class GroupsMappers { public class GroupsMappers {
public static List<DefaultGroupResponse> mapToDefaultResponse(List<Groups> groups) { public static List<DefaultGroupResponse> mapToDefaultResponse(List<Groups> groups) {
@ -17,4 +20,39 @@ public class GroupsMappers {
return groups.stream().filter(Objects::nonNull).map(WithCapacityGroupResponse::new) return groups.stream().filter(Objects::nonNull).map(WithCapacityGroupResponse::new)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public static GetCourseGroupsResponse<DefaultGroupResponse> mapToGetCourseGroupsDefaultResponse (List<Groups> groups){
List<DefaultGroupResponse> lectures = new ArrayList<>();
List<DefaultGroupResponse> 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<WithCapacityGroupResponse> mapToGetCourseGroupsWithCapacityResponse (List<Groups> groups){
List<WithCapacityGroupResponse> lectures = new ArrayList<>();
List<WithCapacityGroupResponse> 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);
}
} }

View File

@ -3,29 +3,21 @@ package com.plannaplan.responses.models;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.plannaplan.entities.Groups; public class GetCourseGroupsResponse <T> {
import com.plannaplan.types.GroupType;
public class GetCourseGroupsResponse { private List<T> lectures = new ArrayList<>();
private List<T> classes = new ArrayList<>();
private List<DefaultGroupResponse> lectures = new ArrayList<>(); public GetCourseGroupsResponse(List<T> classes, List<T> lectures ){
private List<DefaultGroupResponse> classes = new ArrayList<>(); this.lectures = lectures;
this.classes = classes;
public GetCourseGroupsResponse(List<Groups> groups ){
groups.stream().forEach(group -> {
if (group.getType() == GroupType.CLASS) {
this.classes.add(new DefaultGroupResponse(group));
} else {
this.lectures.add(new DefaultGroupResponse(group));
}
});
} }
public List<DefaultGroupResponse> getClasses() { public List<T> getClasses() {
return this.classes; return this.classes;
} }
public List<DefaultGroupResponse> getLectures() { public List<T> getLectures() {
return this.lectures; return this.lectures;
} }

View File

@ -1,11 +1,14 @@
package com.plannaplan.responses.mappers; package com.plannaplan.responses.mappers;
import static org.junit.Assert.assertTrue;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.entities.Lecturer; import com.plannaplan.entities.Lecturer;
import com.plannaplan.responses.models.DefaultGroupResponse; import com.plannaplan.responses.models.DefaultGroupResponse;
import com.plannaplan.responses.models.GetCourseGroupsResponse;
import com.plannaplan.responses.models.WithCapacityGroupResponse; import com.plannaplan.responses.models.WithCapacityGroupResponse;
import com.plannaplan.types.WeekDay; import com.plannaplan.types.WeekDay;
@ -32,4 +35,18 @@ public class GroupsMappersTest {
assert (response.get(0) instanceof DefaultGroupResponse); assert (response.get(0) instanceof DefaultGroupResponse);
assert (response.size() == 1); assert (response.size() == 1);
} }
@Test
public void shouldMapToGetCourseGroupsResponse(){
final List<Groups> 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<DefaultGroupResponse> response = GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups);
assertTrue(response.getClasses().size() == 1);
assertTrue(response.getLectures().size() == 1);
}
} }

View File

@ -1,24 +0,0 @@
package com.plannaplan.responses.models;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.List;
import com.plannaplan.entities.Groups;
import com.plannaplan.types.WeekDay;
import org.junit.Test;
public class GetCourseGroupResponseTest {
@Test
public void shouldMapListGroupsToResponse() {
final List<Groups> groups = Arrays.asList(new Groups(32,"A1-1", null, 480, WeekDay.MONDAY, null), new Groups(150,"A1-2", null, 500, WeekDay.THURSDAY, null));
final GetCourseGroupsResponse response = new GetCourseGroupsResponse(groups);
assertTrue(response.getClasses().size() == 1);
assertTrue(response.getLectures().size() == 1);
}
}

View File

@ -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<DefaultGroupResponse> classes = Arrays.asList(new DefaultGroupResponse(new Groups()));
final List<DefaultGroupResponse> lectures = Arrays.asList(new DefaultGroupResponse(new Groups()));
final GetCourseGroupsResponse<DefaultGroupResponse> instance = new GetCourseGroupsResponse<>(classes,lectures);
assertTrue(instance.getClasses().get(0) instanceof DefaultGroupResponse);
assertTrue(instance.getLectures().get(0) instanceof DefaultGroupResponse);
}
}