From 11ec43911e9baa7e0d5da99fc35a33fd2f58feee Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Sun, 17 Jan 2021 20:36:46 +0100 Subject: [PATCH 1/2] new endpoint --- .../controllers/CommisionController.java | 23 ++++++ .../mappers/AssignmentResponseMappers.java | 14 ++++ .../models/AssignmentDetailedResponse.java | 71 +++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100755 restservice/src/main/java/com/plannaplan/responses/models/AssignmentDetailedResponse.java diff --git a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java index 583acd4..7961630 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java @@ -42,6 +42,7 @@ import java.util.HashMap; import java.util.Iterator; import com.plannaplan.responses.mappers.AssignmentResponseMappers; +import com.plannaplan.responses.models.AssignmentDetailedResponse; import com.plannaplan.responses.models.AssignmentResponse; /** @@ -161,6 +162,28 @@ public class CommisionController extends TokenBasedController { } return new ResponseEntity<>(result, HttpStatus.OK); + + } + + /** + * @return list of user latests assignmets + * @throws UserNotFoundException if user was not found bny token + */ + @GetMapping("/user/assignments") + @ApiOperation("Return list of latest user commision assignments. User is recognized via token") + public ResponseEntity> getAllAssignmets() throws UserNotFoundException { + final User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); + + final Optional latestCommision = this.commisionService.getNewestCommision(user); + + if (latestCommision.isEmpty()) { + return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK); + } + + final List response = AssignmentResponseMappers + .mapAssignmetnToDetialedResponse(latestCommision.get().getAssignments()); + + return new ResponseEntity<>(response, 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 index eb54a19..5302068 100755 --- a/restservice/src/main/java/com/plannaplan/responses/mappers/AssignmentResponseMappers.java +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/AssignmentResponseMappers.java @@ -4,10 +4,13 @@ import java.util.List; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Course; import com.plannaplan.entities.Groups; +import com.plannaplan.responses.models.AssignmentDetailedResponse; import com.plannaplan.responses.models.AssignmentResponse; import com.plannaplan.types.GroupType; @@ -69,4 +72,15 @@ public class AssignmentResponseMappers { } return response; } + + /** + * map assignmetn to detailed response + * + * @param assignments list of assignments to map + * @return list of responses + */ + public static final List mapAssignmetnToDetialedResponse(List assignments) { + return assignments.stream().filter(Objects::nonNull).map(AssignmentDetailedResponse::new) + .collect(Collectors.toList()); + } } diff --git a/restservice/src/main/java/com/plannaplan/responses/models/AssignmentDetailedResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/AssignmentDetailedResponse.java new file mode 100755 index 0000000..0c86f5b --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/responses/models/AssignmentDetailedResponse.java @@ -0,0 +1,71 @@ +package com.plannaplan.responses.models; + +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Groups; + +/** + * Assignment detailed response for api + */ +public class AssignmentDetailedResponse { + public Long id; + public String name; + private int day; + private String time; + private String endTime; + private String lecturer; + + /** + * @param assignment Assignment instance to map + */ + public AssignmentDetailedResponse(Assignment assignment) { + final Groups group = assignment.getGroup(); + this.id = assignment.getId(); + this.name = group.getCourseId().getName(); + this.day = group.getDay().label; + this.time = group.getTimeString(); + this.endTime = group.getEndTimeString(); + this.lecturer = group.getLecturer().toString(); + } + + /** + * @return day as a value from 0-6 + */ + public int getDay() { + return day; + } + + /** + * @return lecturer string fromated + */ + public String getLecturer() { + return lecturer; + } + + /** + * @return time formated string + */ + public String getEndTime() { + return endTime; + } + + /** + * @return time formated string + */ + public String getTime() { + return time; + } + + /** + * @return name of course that group belongs to + */ + public String getName() { + return this.name; + } + + /** + * @return db id of assignment + */ + public Long getId() { + return this.id; + } +} From 2d28abefeaf79ed7f5b02dc3241a40a77a889542 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Mon, 18 Jan 2021 14:19:09 +0100 Subject: [PATCH 2/2] test --- .../controllers/CommisionControllerTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java index f09a5e1..251ebfa 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java @@ -47,6 +47,7 @@ public class CommisionControllerTest extends AbstractControllerTest { private static final String GET_COMMISIONS_ENDPOINT = "/api/v1/commisions/user"; private static final String GET_SOMEONE_COMMISIONS_ENDPOINT = "/api/v1/commisions/user"; private static final String GET_USER_SCHEDULE_ENDPOINT = "/api/v1/commisions/user/schedule"; + private static final String GET_ASSIGNMENTS_ENDPOINT = "/api/v1/commisions/user/assignments"; private static final String EXPORT_DATA = "/api/v1/commisions/export/csv"; @@ -307,4 +308,16 @@ public class CommisionControllerTest extends AbstractControllerTest { mockMvc.perform(get(EXPORT_DATA).header("Authorization", "Bearer " + token)).andExpect(status().isOk()) .andReturn().getResponse().getContentAsString().contains("user_id, zaj_cykl_id, gr_nr")); } + + @Test + public void shouldRetrunOkForAssignmentEnpoint() throws Exception { + this.checkUsers(); + final User admin = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null); + final String token = this.service.login(admin).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + + mockMvc.perform(get(GET_ASSIGNMENTS_ENDPOINT).header("Authorization", "Bearer " + token)) + .andExpect(status().isOk()); + } }