diff --git a/restservice/pom.xml b/restservice/pom.xml
index 3cd37df..68b7c09 100755
--- a/restservice/pom.xml
+++ b/restservice/pom.xml
@@ -31,6 +31,12 @@
test
+
+ io.vavr
+ vavr
+ 0.9.0
+
+
org.apache.httpcomponents
httpclient
diff --git a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java
index 2b56024..b7afa55 100755
--- a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java
+++ b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java
@@ -1,12 +1,12 @@
package com.plannaplan.controllers;
-import java.util.ArrayList;
-import java.util.Dictionary;
-import java.util.Hashtable;
import java.util.List;
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.WithCapacityGroupResponse;
import com.plannaplan.services.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -18,6 +18,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
+import io.vavr.control.Either;
+
@RestController
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/groups")
@@ -26,26 +28,12 @@ public class GroupController {
private GroupService groupService;
@GetMapping("/getCourseGroups")
- public ResponseEntity>> getCourses(@RequestParam("id") Long id,
- @RequestParam(name = "capacity", defaultValue = "true") Boolean capacity) {
+ public ResponseEntity, List>> getCourses(
+ @RequestParam("id") Long id, @RequestParam(name = "capacity", defaultValue = "true") Boolean capacity) {
List groups = this.groupService.getGroupsByCourse(id);
- List> response = new ArrayList<>();
-
- for (Groups g : groups) {
- Dictionary group = new Hashtable<>();
- group.put("id", g.getId());
- group.put("day", g.getDay().label);
- group.put("time", g.getTimeString());
- group.put("lecturer", g.getLecturer().toString());
- group.put("room", g.getRoom());
- if (capacity) {
- group.put("capacity", g.getCapacity());
- }
- group.put("type", g.getType());
-
- response.add(group);
+ if (capacity) {
+ return new ResponseEntity<>(Either.left(GroupsMappers.mapToCapacityResponse(groups)), HttpStatus.OK);
}
-
- return new ResponseEntity<>(response, HttpStatus.OK);
+ return new ResponseEntity<>(Either.right(GroupsMappers.mapToDefaultResponse(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
new file mode 100755
index 0000000..415be62
--- /dev/null
+++ b/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java
@@ -0,0 +1,20 @@
+package com.plannaplan.responses.mappers;
+
+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.WithCapacityGroupResponse;
+
+public class GroupsMappers {
+ public static List mapToDefaultResponse(List groups) {
+ return groups.stream().filter(Objects::nonNull).map(DefaultGroupResponse::new).collect(Collectors.toList());
+ }
+
+ public static List mapToCapacityResponse(List groups) {
+ return groups.stream().filter(Objects::nonNull).map(WithCapacityGroupResponse::new)
+ .collect(Collectors.toList());
+ }
+}
diff --git a/restservice/src/main/java/com/plannaplan/responses/models/DefaultGroupResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/DefaultGroupResponse.java
index 1e1fd15..6054be1 100755
--- a/restservice/src/main/java/com/plannaplan/responses/models/DefaultGroupResponse.java
+++ b/restservice/src/main/java/com/plannaplan/responses/models/DefaultGroupResponse.java
@@ -11,7 +11,6 @@ public class DefaultGroupResponse {
private String time;
private String lecturer;
private String room;
- private int capacity;
private GroupType type;
public DefaultGroupResponse(Groups group) {
@@ -20,7 +19,6 @@ public class DefaultGroupResponse {
this.time = group.getTimeString() != null ? group.getTimeString() : "";
this.lecturer = group.getLecturer() != null ? group.getLecturer().toString() : "";
this.room = group.getRoom() != null ? group.getRoom() : "";
- this.capacity = group.getCapacity();
this.type = group.getType() != null ? group.getType() : null;
}
@@ -32,10 +30,6 @@ public class DefaultGroupResponse {
return type;
}
- public int getCapacity() {
- return capacity;
- }
-
public String getRoom() {
return room;
}
diff --git a/restservice/src/main/java/com/plannaplan/responses/models/GetCurrentAssignmentsResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/GetCurrentAssignmentsResponse.java
index 858630c..b307a61 100755
--- a/restservice/src/main/java/com/plannaplan/responses/models/GetCurrentAssignmentsResponse.java
+++ b/restservice/src/main/java/com/plannaplan/responses/models/GetCurrentAssignmentsResponse.java
@@ -2,7 +2,7 @@ package com.plannaplan.responses.models;
import com.plannaplan.entities.Assignment;
-public class GetCurrentAssignmentsResponse extends DefaultGroupResponse {
+public class GetCurrentAssignmentsResponse extends WithCapacityGroupResponse {
public GetCurrentAssignmentsResponse(Assignment assignment) {
super(assignment);
diff --git a/restservice/src/main/java/com/plannaplan/responses/models/WithCapacityGroupResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/WithCapacityGroupResponse.java
new file mode 100755
index 0000000..4b40dc4
--- /dev/null
+++ b/restservice/src/main/java/com/plannaplan/responses/models/WithCapacityGroupResponse.java
@@ -0,0 +1,23 @@
+package com.plannaplan.responses.models;
+
+import com.plannaplan.entities.Assignment;
+import com.plannaplan.entities.Groups;
+
+public class WithCapacityGroupResponse extends DefaultGroupResponse {
+
+ private int capacity;
+
+ public WithCapacityGroupResponse(Groups group) {
+ super(group);
+ this.capacity = group.getCapacity();
+ }
+
+ public WithCapacityGroupResponse(Assignment assignment) {
+ this(assignment.getGroup());
+ }
+
+ public int getCapacity() {
+ return capacity;
+ }
+
+}
diff --git a/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java b/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java
new file mode 100755
index 0000000..3f735a6
--- /dev/null
+++ b/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java
@@ -0,0 +1,35 @@
+package com.plannaplan.responses.mappers;
+
+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.WithCapacityGroupResponse;
+import com.plannaplan.types.WeekDay;
+
+import org.junit.Test;
+
+public class GroupsMappersTest {
+ @Test
+ public void shouldMapToResponseIncludingCapacity() {
+ final List gropus = Arrays.asList(
+ new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")));
+ final List response = GroupsMappers.mapToCapacityResponse(gropus);
+
+ assert (response.get(0).getCapacity() == 42);
+ assert (response.get(0) instanceof WithCapacityGroupResponse);
+ assert (response.size() == 1);
+ }
+
+ @Test
+ public void shouldMapToResponseWiothoutCapacity() {
+ final List gropus = Arrays.asList(
+ new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")));
+ final List response = GroupsMappers.mapToDefaultResponse(gropus);
+
+ assert (response.get(0) instanceof DefaultGroupResponse);
+ assert (response.size() == 1);
+ }
+}
diff --git a/restservice/src/test/java/com/plannaplan/responses/models/DefaultGroupResponseTest.java b/restservice/src/test/java/com/plannaplan/responses/models/DefaultGroupResponseTest.java
index 278b7b0..e915566 100755
--- a/restservice/src/test/java/com/plannaplan/responses/models/DefaultGroupResponseTest.java
+++ b/restservice/src/test/java/com/plannaplan/responses/models/DefaultGroupResponseTest.java
@@ -17,7 +17,6 @@ public class DefaultGroupResponseTest {
new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")), null);
final DefaultGroupResponse response = new DefaultGroupResponse(assignment);
- assertTrue(response.getCapacity() == 42);
assertTrue(response.getDay() == 0);
assertTrue(response.getLecturer().equals("krul. Wladyslaw Potocki"));
assertTrue(response.getRoom().equals("A4-1"));
@@ -31,7 +30,6 @@ public class DefaultGroupResponseTest {
new Lecturer("krul.", "Wladyslaw", "Potocki"));
final DefaultGroupResponse response = new DefaultGroupResponse(group);
- assertTrue(response.getCapacity() == 42);
assertTrue(response.getDay() == 0);
assertTrue(response.getLecturer().equals("krul. Wladyslaw Potocki"));
assertTrue(response.getRoom().equals("A4-1"));
@@ -44,12 +42,10 @@ public class DefaultGroupResponseTest {
final Groups group = new Groups();
final DefaultGroupResponse response = new DefaultGroupResponse(group);
- assertTrue(response.getCapacity() == 0);
assertTrue(response.getDay() == -1);
assertTrue(response.getLecturer().equals(""));
assertTrue(response.getRoom().equals(""));
assertTrue(response.getTime().equals("0.00"));
assertTrue(response.getType() == null);
}
-
}
diff --git a/restservice/src/test/java/com/plannaplan/responses/models/WithCapacityGroupResponseTest.java b/restservice/src/test/java/com/plannaplan/responses/models/WithCapacityGroupResponseTest.java
new file mode 100755
index 0000000..1495cef
--- /dev/null
+++ b/restservice/src/test/java/com/plannaplan/responses/models/WithCapacityGroupResponseTest.java
@@ -0,0 +1,55 @@
+package com.plannaplan.responses.models;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import com.plannaplan.entities.Assignment;
+import com.plannaplan.entities.Groups;
+import com.plannaplan.entities.Lecturer;
+import com.plannaplan.types.GroupType;
+import com.plannaplan.types.WeekDay;
+
+import org.junit.Test;
+
+public class WithCapacityGroupResponseTest {
+ @Test
+ public void shouldMapAssignmentClassToResponse() {
+ final Assignment assignment = new Assignment(
+ new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")), null);
+
+ final WithCapacityGroupResponse response = new WithCapacityGroupResponse(assignment);
+ assertTrue(response.getCapacity() == 42);
+ assertTrue(response.getDay() == 0);
+ assertTrue(response.getLecturer().equals("krul. Wladyslaw Potocki"));
+ assertTrue(response.getRoom().equals("A4-1"));
+ assertTrue(response.getTime().equals("8.40"));
+ assertTrue(response.getType() == GroupType.CLASS);
+ }
+
+ @Test
+ public void shouldMapGroupClassToResponse() {
+ final Groups group = new Groups(42, "A4-1", null, 520, WeekDay.MONDAY,
+ new Lecturer("krul.", "Wladyslaw", "Potocki"));
+
+ final WithCapacityGroupResponse response = new WithCapacityGroupResponse(group);
+ assertTrue(response.getCapacity() == 42);
+ assertTrue(response.getDay() == 0);
+ assertTrue(response.getLecturer().equals("krul. Wladyslaw Potocki"));
+ assertTrue(response.getRoom().equals("A4-1"));
+ assertTrue(response.getTime().equals("8.40"));
+ assertTrue(response.getType() == GroupType.CLASS);
+ }
+
+ @Test
+ public void shouldMapEmptyGroupClassToResponse() {
+ final Groups group = new Groups();
+
+ final WithCapacityGroupResponse response = new WithCapacityGroupResponse(group);
+ assertTrue(response.getCapacity() == 0);
+ assertTrue(response.getDay() == -1);
+ assertTrue(response.getLecturer().equals(""));
+ assertTrue(response.getRoom().equals(""));
+ assertTrue(response.getTime().equals("0.00"));
+ assertTrue(response.getType() == null);
+ }
+
+}