Responses up[dated

This commit is contained in:
Filip Izydorczyk 2020-10-13 17:07:04 +02:00
parent bdd0278880
commit 148d7a31c6
12 changed files with 161 additions and 49 deletions

View File

@ -12,7 +12,7 @@ import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import com.plannaplan.entities.User; import com.plannaplan.entities.User;
import com.plannaplan.responses.mappers.AssignmentResponseMappers; import com.plannaplan.responses.mappers.AssignmentResponseMappers;
import com.plannaplan.responses.models.AssignmentResponse; import com.plannaplan.responses.models.GetCurrentAssignmentsResponse;
import com.plannaplan.services.AssignmentService; import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.CommisionService; import com.plannaplan.services.CommisionService;
@ -33,7 +33,7 @@ public class AssignmentsController extends TokenBasedController {
private AssignmentService assignmentService; private AssignmentService assignmentService;
@GetMapping("/getCurrentAssignments") @GetMapping("/getCurrentAssignments")
public ResponseEntity<List<AssignmentResponse>> getCurrentAssignments() throws Exception { public ResponseEntity<List<GetCurrentAssignmentsResponse>> getCurrentAssignments() throws Exception {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException("User not found")); User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException("User not found"));
Optional<Commision> com = this.commisionService.getNewestCommision(user); Optional<Commision> com = this.commisionService.getNewestCommision(user);

View File

@ -1,15 +1,12 @@
package com.plannaplan.controllers; package com.plannaplan.controllers;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List; import java.util.List;
import com.plannaplan.App; import com.plannaplan.App;
import com.plannaplan.entities.Course; import com.plannaplan.entities.Course;
import com.plannaplan.entities.Groups;
import com.plannaplan.responses.mappers.CoursesResponseMappers; import com.plannaplan.responses.mappers.CoursesResponseMappers;
import com.plannaplan.responses.models.GetCoursesResponse; import com.plannaplan.responses.models.GetCoursesResponse;
import com.plannaplan.responses.models.GetCoursesWithGroupsResponse;
import com.plannaplan.services.CourseService; import com.plannaplan.services.CourseService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -35,30 +32,33 @@ public class CoursesController {
} }
@GetMapping("/getCoursesWithGroups") @GetMapping("/getCoursesWithGroups")
public ResponseEntity<List<Dictionary<String, Object>>> getCoursesWithGroups() { public ResponseEntity<List<GetCoursesWithGroupsResponse>> getCoursesWithGroups() {
List<Course> courses = this.courseService.getAllCourses(); final List<Course> courses = this.courseService.getAllCourses();
List<Dictionary<String, Object>> response = new ArrayList<>(); final List<GetCoursesWithGroupsResponse> response = CoursesResponseMappers
for (Course c : courses) { .mapCoursesWithGrtoupsListToCoursesResponseList(courses);
Dictionary<String, Object> element = new Hashtable<>();
element.put("id", c.getId());
element.put("name", c.getName());
List<Dictionary<String, Object>> groups = new ArrayList<>();
for (Groups g : c.getGroups()) {
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());
group.put("type", g.getType());
groups.add(group);
}
element.put("groups", groups);
response.add(element);
}
return new ResponseEntity<>(response, HttpStatus.OK); return new ResponseEntity<>(response, HttpStatus.OK);
// for (Course c : courses) {
// Dictionary<String, Object> element = new Hashtable<>();
// element.put("id", c.getId());
// element.put("name", c.getName());
// List<Dictionary<String, Object>> groups = new ArrayList<>();
// for (Groups g : c.getGroups()) {
// 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());
// group.put("type", g.getType());
// groups.add(group);
// }
// element.put("groups", groups);
// response.add(element);
// }
// return new ResponseEntity<>(response, HttpStatus.OK);
} }
} }

View File

@ -5,16 +5,13 @@ import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Groups; import com.plannaplan.responses.models.GetCurrentAssignmentsResponse;
import com.plannaplan.responses.models.AssignmentResponse;
public class AssignmentResponseMappers { public class AssignmentResponseMappers {
public static final List<AssignmentResponse> mapGroupsListToAssignmentResponseList(List<Groups> groups) {
return groups.stream().filter(Objects::nonNull).map(AssignmentResponse::new).collect(Collectors.toList());
}
public static final List<AssignmentResponse> mapAssignmentsListToAssignmentResponseList( public static final List<GetCurrentAssignmentsResponse> mapAssignmentsListToAssignmentResponseList(
List<Assignment> assignments) { List<Assignment> assignments) {
return assignments.stream().filter(Objects::nonNull).map(AssignmentResponse::new).collect(Collectors.toList()); return assignments.stream().filter(Objects::nonNull).map(GetCurrentAssignmentsResponse::new)
.collect(Collectors.toList());
} }
} }

View File

@ -6,9 +6,16 @@ import java.util.stream.Collectors;
import com.plannaplan.entities.Course; import com.plannaplan.entities.Course;
import com.plannaplan.responses.models.GetCoursesResponse; import com.plannaplan.responses.models.GetCoursesResponse;
import com.plannaplan.responses.models.GetCoursesWithGroupsResponse;
public class CoursesResponseMappers { public class CoursesResponseMappers {
public static final List<GetCoursesResponse> mapCoursesListToCoursesResponseList(List<Course> courses) { public static final List<GetCoursesResponse> mapCoursesListToCoursesResponseList(List<Course> courses) {
return courses.stream().filter(Objects::nonNull).map(GetCoursesResponse::new).collect(Collectors.toList()); return courses.stream().filter(Objects::nonNull).map(GetCoursesResponse::new).collect(Collectors.toList());
} }
public static final List<GetCoursesWithGroupsResponse> mapCoursesWithGrtoupsListToCoursesResponseList(
List<Course> courses) {
return courses.stream().filter(Objects::nonNull).map(GetCoursesWithGroupsResponse::new)
.collect(Collectors.toList());
}
} }

View File

@ -4,7 +4,7 @@ import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.types.GroupType; import com.plannaplan.types.GroupType;
public class AssignmentResponse { public class DefaultGroupResponse {
private Long id; private Long id;
private int day; private int day;
@ -14,7 +14,7 @@ public class AssignmentResponse {
private int capacity; private int capacity;
private GroupType type; private GroupType type;
public AssignmentResponse(Groups group) { public DefaultGroupResponse(Groups group) {
this.id = group.getId() != null ? group.getId() : null; this.id = group.getId() != null ? group.getId() : null;
this.day = group.getDay() != null ? group.getDay().label : -1; this.day = group.getDay() != null ? group.getDay().label : -1;
this.time = group.getTimeString() != null ? group.getTimeString() : ""; this.time = group.getTimeString() != null ? group.getTimeString() : "";
@ -24,7 +24,7 @@ public class AssignmentResponse {
this.type = group.getType() != null ? group.getType() : null; this.type = group.getType() != null ? group.getType() : null;
} }
public AssignmentResponse(Assignment assignment) { public DefaultGroupResponse(Assignment assignment) {
this(assignment.getGroup()); this(assignment.getGroup());
} }

View File

@ -0,0 +1,33 @@
package com.plannaplan.responses.models;
import java.util.ArrayList;
import java.util.List;
import com.plannaplan.entities.Course;
import com.plannaplan.types.GroupType;
public class GetCoursesWithGroupsResponse extends CoursesResponse {
private List<DefaultGroupResponse> lectures = new ArrayList<>();
private List<DefaultGroupResponse> classes = new ArrayList<>();
public GetCoursesWithGroupsResponse(Course course) {
super(course);
course.getGroups().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() {
return this.classes;
}
public List<DefaultGroupResponse> getLectures() {
return this.lectures;
}
}

View File

@ -0,0 +1,11 @@
package com.plannaplan.responses.models;
import com.plannaplan.entities.Assignment;
public class GetCurrentAssignmentsResponse extends DefaultGroupResponse {
public GetCurrentAssignmentsResponse(Assignment assignment) {
super(assignment);
}
}

View File

@ -5,20 +5,24 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.responses.models.AssignmentResponse; import com.plannaplan.responses.models.GetCurrentAssignmentsResponse;
import org.junit.Test; import org.junit.Test;
public class AssignmentResponseMappersTest { public class AssignmentResponseMappersTest {
@Test @Test
public void shouldReturnNewList() { public void shouldReturnNewList() {
final List<Groups> groups = Arrays.asList(new Groups(), new Groups()); final Commision com = new Commision();
final List<AssignmentResponse> response = AssignmentResponseMappers final List<Assignment> groups = Arrays.asList(new Assignment(new Groups(), com),
.mapGroupsListToAssignmentResponseList(groups); new Assignment(new Groups(), com));
final List<GetCurrentAssignmentsResponse> response = AssignmentResponseMappers
.mapAssignmentsListToAssignmentResponseList(groups);
assertTrue(response.size() == 2); assertTrue(response.size() == 2);
assertTrue(response instanceof List); assertTrue(response instanceof List);
assertTrue(response.get(0) instanceof AssignmentResponse); assertTrue(response.get(0) instanceof GetCurrentAssignmentsResponse);
} }
} }

View File

@ -7,6 +7,7 @@ import java.util.List;
import com.plannaplan.entities.Course; import com.plannaplan.entities.Course;
import com.plannaplan.responses.models.GetCoursesResponse; import com.plannaplan.responses.models.GetCoursesResponse;
import com.plannaplan.responses.models.GetCoursesWithGroupsResponse;
import org.junit.Test; import org.junit.Test;
@ -20,4 +21,15 @@ public class CoursesResponseMappersTest {
assertTrue(response.get(0) instanceof GetCoursesResponse); assertTrue(response.get(0) instanceof GetCoursesResponse);
} }
@Test
public void shouldMapListCoursesToResponseWithGroupsList() {
final List<Course> courses = Arrays.asList(new Course(), new Course());
final List<GetCoursesWithGroupsResponse> response = CoursesResponseMappers
.mapCoursesWithGrtoupsListToCoursesResponseList(courses);
assertTrue(response.size() == 2);
assertTrue(response.get(0) instanceof GetCoursesWithGroupsResponse);
}
} }

View File

@ -10,14 +10,13 @@ import com.plannaplan.types.WeekDay;
import org.junit.Test; import org.junit.Test;
public class AssignmentResponseTest { public class DefaultGroupResponseTest {
@Test @Test
public void shouldMapAssignmentClassToResponse() { public void shouldMapAssignmentClassToResponse() {
final Assignment assignment = new Assignment( final Assignment assignment = new Assignment(
new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")), null); new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")), null);
final AssignmentResponse response = new AssignmentResponse(assignment); final DefaultGroupResponse response = new DefaultGroupResponse(assignment);
assertTrue(response.getCapacity() == 42); assertTrue(response.getCapacity() == 42);
assertTrue(response.getDay() == 0); assertTrue(response.getDay() == 0);
assertTrue(response.getLecturer().equals("krul. Wladyslaw Potocki")); assertTrue(response.getLecturer().equals("krul. Wladyslaw Potocki"));
@ -31,7 +30,7 @@ public class AssignmentResponseTest {
final Groups group = new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, final Groups group = new Groups(42, "A4-1", null, 520, WeekDay.MONDAY,
new Lecturer("krul.", "Wladyslaw", "Potocki")); new Lecturer("krul.", "Wladyslaw", "Potocki"));
final AssignmentResponse response = new AssignmentResponse(group); final DefaultGroupResponse response = new DefaultGroupResponse(group);
assertTrue(response.getCapacity() == 42); assertTrue(response.getCapacity() == 42);
assertTrue(response.getDay() == 0); assertTrue(response.getDay() == 0);
assertTrue(response.getLecturer().equals("krul. Wladyslaw Potocki")); assertTrue(response.getLecturer().equals("krul. Wladyslaw Potocki"));
@ -44,7 +43,7 @@ public class AssignmentResponseTest {
public void shouldMapEmptyGroupClassToResponse() { public void shouldMapEmptyGroupClassToResponse() {
final Groups group = new Groups(); final Groups group = new Groups();
final AssignmentResponse response = new AssignmentResponse(group); final DefaultGroupResponse response = new DefaultGroupResponse(group);
assertTrue(response.getCapacity() == 0); assertTrue(response.getCapacity() == 0);
assertTrue(response.getDay() == -1); assertTrue(response.getDay() == -1);
assertTrue(response.getLecturer().equals("")); assertTrue(response.getLecturer().equals(""));

View File

@ -0,0 +1,20 @@
package com.plannaplan.responses.models;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.plannaplan.entities.Course;
import org.junit.Test;
public class GetCoursesWithGroupsResponseTest {
@Test
public void shouldMapCourseToResponseWithGroupsIncluded() {
final GetCoursesWithGroupsResponse response = new GetCoursesWithGroupsResponse(
new Course("Programowanie funkcyjne", "xd"));
assertTrue(response.getName() == "Programowanie funkcyjne");
assertTrue(response.getClasses().size() == 0);
assertTrue(response.getLectures().size() == 0);
}
}

View File

@ -0,0 +1,29 @@
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 GetCurrentAssignmentsResponseTest {
@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 GetCurrentAssignmentsResponse response = new GetCurrentAssignmentsResponse(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);
}
}