Merge pull request 'ZPI-139' (#12) from ZPI-139 into master

Reviewed-on: http://git.plannaplan.pl/filipizydorczyk/backend/pulls/12
This commit is contained in:
filipizydorczyk 2020-10-24 18:01:17 +02:00
commit 9dba639674
5 changed files with 111 additions and 3 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

@ -0,0 +1,24 @@
package com.plannaplan.responses.models;
import java.util.ArrayList;
import java.util.List;
public class GetCourseGroupsResponse <T> {
private List<T> lectures = new ArrayList<>();
private List<T> classes = new ArrayList<>();
public GetCourseGroupsResponse(List<T> classes, List<T> lectures ){
this.lectures = lectures;
this.classes = classes;
}
public List<T> getClasses() {
return this.classes;
}
public List<T> getLectures() {
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

@ -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);
}
}