Checkpoint - group controller needs to be fixed

This commit is contained in:
Filip Izydorczyk 2020-10-13 18:03:37 +02:00
parent 932e1a8830
commit 3e8bfe85d6
9 changed files with 150 additions and 33 deletions

View File

@ -31,6 +31,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>

View File

@ -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<List<Dictionary<String, Object>>> getCourses(@RequestParam("id") Long id,
@RequestParam(name = "capacity", defaultValue = "true") Boolean capacity) {
public ResponseEntity<Either<List<WithCapacityGroupResponse>, List<DefaultGroupResponse>>> getCourses(
@RequestParam("id") Long id, @RequestParam(name = "capacity", defaultValue = "true") Boolean capacity) {
List<Groups> groups = this.groupService.getGroupsByCourse(id);
List<Dictionary<String, Object>> response = new ArrayList<>();
for (Groups g : groups) {
Dictionary<String, Object> 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);
}
}

View File

@ -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<DefaultGroupResponse> mapToDefaultResponse(List<Groups> groups) {
return groups.stream().filter(Objects::nonNull).map(DefaultGroupResponse::new).collect(Collectors.toList());
}
public static List<WithCapacityGroupResponse> mapToCapacityResponse(List<Groups> groups) {
return groups.stream().filter(Objects::nonNull).map(WithCapacityGroupResponse::new)
.collect(Collectors.toList());
}
}

View File

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

View File

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

View File

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

View File

@ -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<Groups> gropus = Arrays.asList(
new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")));
final List<WithCapacityGroupResponse> 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<Groups> gropus = Arrays.asList(
new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")));
final List<DefaultGroupResponse> response = GroupsMappers.mapToDefaultResponse(gropus);
assert (response.get(0) instanceof DefaultGroupResponse);
assert (response.size() == 1);
}
}

View File

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

View File

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