From 148d7a31c651bb08941777a29ad57a69121809da Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Tue, 13 Oct 2020 17:07:04 +0200 Subject: [PATCH] Responses up[dated --- .../controllers/AssignmentsController.java | 4 +- .../controllers/CoursesController.java | 54 +++++++++---------- .../mappers/AssignmentResponseMappers.java | 11 ++-- .../mappers/CoursesResponseMappers.java | 7 +++ ...esponse.java => DefaultGroupResponse.java} | 6 +-- .../models/GetCoursesWithGroupsResponse.java | 33 ++++++++++++ .../models/GetCurrentAssignmentsResponse.java | 11 ++++ .../AssignmentResponseMappersTest.java | 14 +++-- .../mappers/CoursesResponseMappersTest.java | 12 +++++ ...est.java => DefaultGroupResponseTest.java} | 9 ++-- .../GetCoursesWithGroupsResponseTest.java | 20 +++++++ .../GetCurrentAssignmentsResponseTest.java | 29 ++++++++++ 12 files changed, 161 insertions(+), 49 deletions(-) rename restservice/src/main/java/com/plannaplan/responses/models/{AssignmentResponse.java => DefaultGroupResponse.java} (90%) create mode 100755 restservice/src/main/java/com/plannaplan/responses/models/GetCoursesWithGroupsResponse.java create mode 100755 restservice/src/main/java/com/plannaplan/responses/models/GetCurrentAssignmentsResponse.java rename restservice/src/test/java/com/plannaplan/responses/models/{AssignmentResponseTest.java => DefaultGroupResponseTest.java} (87%) create mode 100755 restservice/src/test/java/com/plannaplan/responses/models/GetCoursesWithGroupsResponseTest.java create mode 100755 restservice/src/test/java/com/plannaplan/responses/models/GetCurrentAssignmentsResponseTest.java diff --git a/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java b/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java index f23c9c7..ca8af01 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java @@ -12,7 +12,7 @@ import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; import com.plannaplan.entities.User; 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.CommisionService; @@ -33,7 +33,7 @@ public class AssignmentsController extends TokenBasedController { private AssignmentService assignmentService; @GetMapping("/getCurrentAssignments") - public ResponseEntity> getCurrentAssignments() throws Exception { + public ResponseEntity> getCurrentAssignments() throws Exception { User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException("User not found")); Optional com = this.commisionService.getNewestCommision(user); diff --git a/restservice/src/main/java/com/plannaplan/controllers/CoursesController.java b/restservice/src/main/java/com/plannaplan/controllers/CoursesController.java index be5a014..533b182 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/CoursesController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/CoursesController.java @@ -1,15 +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.Course; -import com.plannaplan.entities.Groups; import com.plannaplan.responses.mappers.CoursesResponseMappers; import com.plannaplan.responses.models.GetCoursesResponse; +import com.plannaplan.responses.models.GetCoursesWithGroupsResponse; import com.plannaplan.services.CourseService; import org.springframework.beans.factory.annotation.Autowired; @@ -35,30 +32,33 @@ public class CoursesController { } @GetMapping("/getCoursesWithGroups") - public ResponseEntity>> getCoursesWithGroups() { - List courses = this.courseService.getAllCourses(); - List> response = new ArrayList<>(); - for (Course c : courses) { - Dictionary element = new Hashtable<>(); - element.put("id", c.getId()); - element.put("name", c.getName()); - List> groups = new ArrayList<>(); - for (Groups g : c.getGroups()) { - 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()); - group.put("type", g.getType()); - groups.add(group); - } - - element.put("groups", groups); - response.add(element); - } - + public ResponseEntity> getCoursesWithGroups() { + final List courses = this.courseService.getAllCourses(); + final List response = CoursesResponseMappers + .mapCoursesWithGrtoupsListToCoursesResponseList(courses); return new ResponseEntity<>(response, HttpStatus.OK); + + // for (Course c : courses) { + // Dictionary element = new Hashtable<>(); + // element.put("id", c.getId()); + // element.put("name", c.getName()); + // List> groups = new ArrayList<>(); + // for (Groups g : c.getGroups()) { + // 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()); + // group.put("type", g.getType()); + // groups.add(group); + // } + + // element.put("groups", groups); + // response.add(element); + // } + + // return new ResponseEntity<>(response, HttpStatus.OK); } } \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/responses/mappers/AssignmentResponseMappers.java b/restservice/src/main/java/com/plannaplan/responses/mappers/AssignmentResponseMappers.java index 7c29ace..b49ddcb 100755 --- a/restservice/src/main/java/com/plannaplan/responses/mappers/AssignmentResponseMappers.java +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/AssignmentResponseMappers.java @@ -5,16 +5,13 @@ import java.util.Objects; import java.util.stream.Collectors; import com.plannaplan.entities.Assignment; -import com.plannaplan.entities.Groups; -import com.plannaplan.responses.models.AssignmentResponse; +import com.plannaplan.responses.models.GetCurrentAssignmentsResponse; public class AssignmentResponseMappers { - public static final List mapGroupsListToAssignmentResponseList(List groups) { - return groups.stream().filter(Objects::nonNull).map(AssignmentResponse::new).collect(Collectors.toList()); - } - public static final List mapAssignmentsListToAssignmentResponseList( + public static final List mapAssignmentsListToAssignmentResponseList( List 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()); } } diff --git a/restservice/src/main/java/com/plannaplan/responses/mappers/CoursesResponseMappers.java b/restservice/src/main/java/com/plannaplan/responses/mappers/CoursesResponseMappers.java index 0e58eee..1fb2655 100755 --- a/restservice/src/main/java/com/plannaplan/responses/mappers/CoursesResponseMappers.java +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/CoursesResponseMappers.java @@ -6,9 +6,16 @@ import java.util.stream.Collectors; import com.plannaplan.entities.Course; import com.plannaplan.responses.models.GetCoursesResponse; +import com.plannaplan.responses.models.GetCoursesWithGroupsResponse; public class CoursesResponseMappers { public static final List mapCoursesListToCoursesResponseList(List courses) { return courses.stream().filter(Objects::nonNull).map(GetCoursesResponse::new).collect(Collectors.toList()); } + + public static final List mapCoursesWithGrtoupsListToCoursesResponseList( + List courses) { + return courses.stream().filter(Objects::nonNull).map(GetCoursesWithGroupsResponse::new) + .collect(Collectors.toList()); + } } diff --git a/restservice/src/main/java/com/plannaplan/responses/models/AssignmentResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/DefaultGroupResponse.java similarity index 90% rename from restservice/src/main/java/com/plannaplan/responses/models/AssignmentResponse.java rename to restservice/src/main/java/com/plannaplan/responses/models/DefaultGroupResponse.java index 414ac1d..1e1fd15 100755 --- a/restservice/src/main/java/com/plannaplan/responses/models/AssignmentResponse.java +++ b/restservice/src/main/java/com/plannaplan/responses/models/DefaultGroupResponse.java @@ -4,7 +4,7 @@ import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Groups; import com.plannaplan.types.GroupType; -public class AssignmentResponse { +public class DefaultGroupResponse { private Long id; private int day; @@ -14,7 +14,7 @@ public class AssignmentResponse { private int capacity; private GroupType type; - public AssignmentResponse(Groups group) { + public DefaultGroupResponse(Groups group) { this.id = group.getId() != null ? group.getId() : null; this.day = group.getDay() != null ? group.getDay().label : -1; this.time = group.getTimeString() != null ? group.getTimeString() : ""; @@ -24,7 +24,7 @@ public class AssignmentResponse { this.type = group.getType() != null ? group.getType() : null; } - public AssignmentResponse(Assignment assignment) { + public DefaultGroupResponse(Assignment assignment) { this(assignment.getGroup()); } diff --git a/restservice/src/main/java/com/plannaplan/responses/models/GetCoursesWithGroupsResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/GetCoursesWithGroupsResponse.java new file mode 100755 index 0000000..3105b95 --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/responses/models/GetCoursesWithGroupsResponse.java @@ -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 lectures = new ArrayList<>(); + private List 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 getClasses() { + return this.classes; + } + + public List getLectures() { + return this.lectures; + } + +} diff --git a/restservice/src/main/java/com/plannaplan/responses/models/GetCurrentAssignmentsResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/GetCurrentAssignmentsResponse.java new file mode 100755 index 0000000..858630c --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/responses/models/GetCurrentAssignmentsResponse.java @@ -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); + } + +} diff --git a/restservice/src/test/java/com/plannaplan/responses/mappers/AssignmentResponseMappersTest.java b/restservice/src/test/java/com/plannaplan/responses/mappers/AssignmentResponseMappersTest.java index 9613101..d92689b 100755 --- a/restservice/src/test/java/com/plannaplan/responses/mappers/AssignmentResponseMappersTest.java +++ b/restservice/src/test/java/com/plannaplan/responses/mappers/AssignmentResponseMappersTest.java @@ -5,20 +5,24 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Arrays; import java.util.List; +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; import com.plannaplan.entities.Groups; -import com.plannaplan.responses.models.AssignmentResponse; +import com.plannaplan.responses.models.GetCurrentAssignmentsResponse; import org.junit.Test; public class AssignmentResponseMappersTest { @Test public void shouldReturnNewList() { - final List groups = Arrays.asList(new Groups(), new Groups()); - final List response = AssignmentResponseMappers - .mapGroupsListToAssignmentResponseList(groups); + final Commision com = new Commision(); + final List groups = Arrays.asList(new Assignment(new Groups(), com), + new Assignment(new Groups(), com)); + final List response = AssignmentResponseMappers + .mapAssignmentsListToAssignmentResponseList(groups); assertTrue(response.size() == 2); assertTrue(response instanceof List); - assertTrue(response.get(0) instanceof AssignmentResponse); + assertTrue(response.get(0) instanceof GetCurrentAssignmentsResponse); } } diff --git a/restservice/src/test/java/com/plannaplan/responses/mappers/CoursesResponseMappersTest.java b/restservice/src/test/java/com/plannaplan/responses/mappers/CoursesResponseMappersTest.java index 5e99cce..9952d4b 100755 --- a/restservice/src/test/java/com/plannaplan/responses/mappers/CoursesResponseMappersTest.java +++ b/restservice/src/test/java/com/plannaplan/responses/mappers/CoursesResponseMappersTest.java @@ -7,6 +7,7 @@ import java.util.List; import com.plannaplan.entities.Course; import com.plannaplan.responses.models.GetCoursesResponse; +import com.plannaplan.responses.models.GetCoursesWithGroupsResponse; import org.junit.Test; @@ -20,4 +21,15 @@ public class CoursesResponseMappersTest { assertTrue(response.get(0) instanceof GetCoursesResponse); } + + @Test + public void shouldMapListCoursesToResponseWithGroupsList() { + final List courses = Arrays.asList(new Course(), new Course()); + final List response = CoursesResponseMappers + .mapCoursesWithGrtoupsListToCoursesResponseList(courses); + + assertTrue(response.size() == 2); + assertTrue(response.get(0) instanceof GetCoursesWithGroupsResponse); + + } } diff --git a/restservice/src/test/java/com/plannaplan/responses/models/AssignmentResponseTest.java b/restservice/src/test/java/com/plannaplan/responses/models/DefaultGroupResponseTest.java similarity index 87% rename from restservice/src/test/java/com/plannaplan/responses/models/AssignmentResponseTest.java rename to restservice/src/test/java/com/plannaplan/responses/models/DefaultGroupResponseTest.java index 137ef78..278b7b0 100755 --- a/restservice/src/test/java/com/plannaplan/responses/models/AssignmentResponseTest.java +++ b/restservice/src/test/java/com/plannaplan/responses/models/DefaultGroupResponseTest.java @@ -10,14 +10,13 @@ import com.plannaplan.types.WeekDay; import org.junit.Test; -public class AssignmentResponseTest { - +public class DefaultGroupResponseTest { @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 AssignmentResponse response = new AssignmentResponse(assignment); + final DefaultGroupResponse response = new DefaultGroupResponse(assignment); assertTrue(response.getCapacity() == 42); assertTrue(response.getDay() == 0); 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, new Lecturer("krul.", "Wladyslaw", "Potocki")); - final AssignmentResponse response = new AssignmentResponse(group); + final DefaultGroupResponse response = new DefaultGroupResponse(group); assertTrue(response.getCapacity() == 42); assertTrue(response.getDay() == 0); assertTrue(response.getLecturer().equals("krul. Wladyslaw Potocki")); @@ -44,7 +43,7 @@ public class AssignmentResponseTest { public void shouldMapEmptyGroupClassToResponse() { final Groups group = new Groups(); - final AssignmentResponse response = new AssignmentResponse(group); + final DefaultGroupResponse response = new DefaultGroupResponse(group); assertTrue(response.getCapacity() == 0); assertTrue(response.getDay() == -1); assertTrue(response.getLecturer().equals("")); diff --git a/restservice/src/test/java/com/plannaplan/responses/models/GetCoursesWithGroupsResponseTest.java b/restservice/src/test/java/com/plannaplan/responses/models/GetCoursesWithGroupsResponseTest.java new file mode 100755 index 0000000..422ca10 --- /dev/null +++ b/restservice/src/test/java/com/plannaplan/responses/models/GetCoursesWithGroupsResponseTest.java @@ -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); + } +} diff --git a/restservice/src/test/java/com/plannaplan/responses/models/GetCurrentAssignmentsResponseTest.java b/restservice/src/test/java/com/plannaplan/responses/models/GetCurrentAssignmentsResponseTest.java new file mode 100755 index 0000000..0ff4e1a --- /dev/null +++ b/restservice/src/test/java/com/plannaplan/responses/models/GetCurrentAssignmentsResponseTest.java @@ -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); + } + +}