From f97e22fa5f32ed1503447ec301d54529fc14fdc7 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Thu, 8 Oct 2020 16:41:13 +0200 Subject: [PATCH] Made ASsignm,ents Conmtroller more readable --- .../controllers/AssignmentsController.java | 32 ++++--------------- .../mappers/AssignmentResponseMappers.java | 20 ++++++++++++ .../responses/models/AssignmentResponse.java | 17 ++++++---- .../AssignmentResponseMappersTest.java | 24 ++++++++++++++ .../models/AssignmentResponseTest.java | 28 ++++++++++++++++ 5 files changed, 90 insertions(+), 31 deletions(-) create mode 100755 restservice/src/main/java/com/plannaplan/responses/mappers/AssignmentResponseMappers.java create mode 100755 restservice/src/test/java/com/plannaplan/responses/mappers/AssignmentResponseMappersTest.java diff --git a/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java b/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java index dcb26b9..f23c9c7 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java @@ -4,17 +4,15 @@ import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.ArrayList; -import java.util.Dictionary; -import java.util.Hashtable; import java.util.List; import java.util.Optional; import com.plannaplan.App; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; -import com.plannaplan.entities.Groups; import com.plannaplan.entities.User; +import com.plannaplan.responses.mappers.AssignmentResponseMappers; +import com.plannaplan.responses.models.AssignmentResponse; import com.plannaplan.services.AssignmentService; import com.plannaplan.services.CommisionService; @@ -35,30 +33,14 @@ public class AssignmentsController extends TokenBasedController { private AssignmentService assignmentService; @GetMapping("/getCurrentAssignments") - public ResponseEntity> getCurrentAssignments() throws Exception { - User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); + public ResponseEntity> getCurrentAssignments() throws Exception { + User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException("User not found")); Optional com = this.commisionService.getNewestCommision(user); + if (com.isPresent()) { List respone = this.assignmentService.getCommisionAssignments(com.get()); - - List finalResponse = new ArrayList<>(); - for (Assignment a : respone) { - Dictionary elem = new Hashtable<>(); - Dictionary groupInfo = new Hashtable<>(); - elem.put("id", a.getId()); - Groups g = a.getGroup(); - groupInfo.put("id", g.getId()); - groupInfo.put("day", g.getDay().label); - groupInfo.put("time", g.getTimeString()); - groupInfo.put("lecturer", g.getLecturer().toString()); - groupInfo.put("room", g.getRoom()); - groupInfo.put("capacity", g.getCapacity()); - groupInfo.put("type", g.getType()); - elem.put("group", groupInfo); - finalResponse.add(elem); - } - - return new ResponseEntity<>(finalResponse, HttpStatus.OK); + return new ResponseEntity<>(AssignmentResponseMappers.mapAssignmentsListToAssignmentResponseList(respone), + HttpStatus.OK); } return new ResponseEntity<>(null, HttpStatus.OK); diff --git a/restservice/src/main/java/com/plannaplan/responses/mappers/AssignmentResponseMappers.java b/restservice/src/main/java/com/plannaplan/responses/mappers/AssignmentResponseMappers.java new file mode 100755 index 0000000..7c29ace --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/AssignmentResponseMappers.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.Assignment; +import com.plannaplan.entities.Groups; +import com.plannaplan.responses.models.AssignmentResponse; + +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( + List assignments) { + return assignments.stream().filter(Objects::nonNull).map(AssignmentResponse::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/AssignmentResponse.java index 82dee10..414ac1d 100755 --- a/restservice/src/main/java/com/plannaplan/responses/models/AssignmentResponse.java +++ b/restservice/src/main/java/com/plannaplan/responses/models/AssignmentResponse.java @@ -1,5 +1,6 @@ package com.plannaplan.responses.models; +import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Groups; import com.plannaplan.types.GroupType; @@ -14,13 +15,17 @@ public class AssignmentResponse { private GroupType type; public AssignmentResponse(Groups group) { - this.id = group.getId(); - this.day = group.getDay().label; - this.time = group.getTimeString(); - this.lecturer = group.getLecturer().toString(); - this.room = group.getRoom(); + this.id = group.getId() != null ? group.getId() : null; + this.day = group.getDay() != null ? group.getDay().label : -1; + 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(); + this.type = group.getType() != null ? group.getType() : null; + } + + public AssignmentResponse(Assignment assignment) { + this(assignment.getGroup()); } public GroupType getType() { diff --git a/restservice/src/test/java/com/plannaplan/responses/mappers/AssignmentResponseMappersTest.java b/restservice/src/test/java/com/plannaplan/responses/mappers/AssignmentResponseMappersTest.java new file mode 100755 index 0000000..9613101 --- /dev/null +++ b/restservice/src/test/java/com/plannaplan/responses/mappers/AssignmentResponseMappersTest.java @@ -0,0 +1,24 @@ +package com.plannaplan.responses.mappers; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Arrays; +import java.util.List; + +import com.plannaplan.entities.Groups; +import com.plannaplan.responses.models.AssignmentResponse; + +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); + + assertTrue(response.size() == 2); + assertTrue(response instanceof List); + assertTrue(response.get(0) instanceof AssignmentResponse); + } +} diff --git a/restservice/src/test/java/com/plannaplan/responses/models/AssignmentResponseTest.java b/restservice/src/test/java/com/plannaplan/responses/models/AssignmentResponseTest.java index c69311b..137ef78 100755 --- a/restservice/src/test/java/com/plannaplan/responses/models/AssignmentResponseTest.java +++ b/restservice/src/test/java/com/plannaplan/responses/models/AssignmentResponseTest.java @@ -2,6 +2,7 @@ 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; @@ -11,6 +12,20 @@ import org.junit.Test; public class AssignmentResponseTest { + @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); + 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, @@ -25,4 +40,17 @@ public class AssignmentResponseTest { assertTrue(response.getType() == GroupType.CLASS); } + @Test + public void shouldMapEmptyGroupClassToResponse() { + final Groups group = new Groups(); + + final AssignmentResponse response = new AssignmentResponse(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); + } + }