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

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