From ca01b20221ee94123d6758e5b27c55233e812bb6 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Fri, 30 Oct 2020 14:53:33 +0100 Subject: [PATCH 1/2] Added endpoint - waiting for other branch to merge to master --- .../com/plannaplan/services/UserService.java | 4 ++ .../controllers/CommisionController.java | 67 +++++++++++-------- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java index 4eaa65e..73a0644 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java @@ -49,4 +49,8 @@ public class UserService { return this.repo.searchForUsers(query, UserRoles.STUDENT); } + public Optional getById(Long userId) { + return this.repo.findById(userId); + } + } \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java index ea7d2aa..f6e0c5f 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java @@ -2,6 +2,7 @@ package com.plannaplan.controllers; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.util.List; @@ -21,6 +22,7 @@ import com.plannaplan.services.GroupService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -30,40 +32,51 @@ import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("/api/" + App.API_VERSION + "/commisions") public class CommisionController extends TokenBasedController { - @Autowired - private CommisionService commisionService; + @Autowired + private CommisionService commisionService; - @Autowired - private GroupService groupServcicxe; + @Autowired + private GroupService groupServcicxe; - @Autowired - private AssignmentService assignmentService; + @Autowired + private AssignmentService assignmentService; - public CommisionController() { - } + public CommisionController() { + } - @PostMapping("/add") - public ResponseEntity addCommision(@RequestBody List groups) throws UserNotFoundException { + @PostMapping("/add") + public ResponseEntity addCommision(@RequestBody List groups) throws UserNotFoundException { - User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); - Commision com = new Commision(user); - this.commisionService.save(com); + User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); + Commision com = new Commision(user); + this.commisionService.save(com); - groups.stream().forEach((groupId) -> { - Groups group = this.groupServcicxe.getGroupById(groupId).orElseThrow(() -> new NullPointerException()); - Assignment a = new Assignment(group, com); - this.assignmentService.save(a); - }); + groups.stream().forEach((groupId) -> { + Groups group = this.groupServcicxe.getGroupById(groupId) + .orElseThrow(() -> new NullPointerException()); + Assignment a = new Assignment(group, com); + this.assignmentService.save(a); + }); - return new ResponseEntity<>("Succes", HttpStatus.OK); - } + return new ResponseEntity<>("Succes", HttpStatus.OK); + } - @GetMapping("/getAllCommisions") - public ResponseEntity> getAlCommisions() throws UserNotFoundException { - User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); - List result = CommisionResponseMappers - .mapToResponse(this.commisionService.getUsersCommisions(user)); - return new ResponseEntity<>(result, HttpStatus.OK); - } + @GetMapping("/getAllCommisions") + public ResponseEntity> getAlCommisions() throws UserNotFoundException { + User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); + List result = CommisionResponseMappers + .mapToResponse(this.commisionService.getUsersCommisions(user)); + return new ResponseEntity<>(result, HttpStatus.OK); + } + + @PreAuthorize("hasRole('ROLE_DEANERY')") + @GetMapping("/user/{id}") + public ResponseEntity> getCommision(@PathVariable(name = "id") Long userId) + throws UserNotFoundException { + User user = this.userService.getById(userId).orElseThrow(() -> new NullPointerException()); + List result = CommisionResponseMappers + .mapToResponse(this.commisionService.getUsersCommisions(user)); + return new ResponseEntity<>(result, HttpStatus.OK); + } } From b4f51d438244f93e1388b77eabcd8bf8cdba8303 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 31 Oct 2020 15:24:17 +0100 Subject: [PATCH 2/2] Added tests --- .../controllers/CommisionControllerTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java index ab32320..f80b0d5 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java @@ -46,6 +46,7 @@ public class CommisionControllerTest { private static final String ADD_COMMISION_ENDPOINT = "/api/v1/commisions/add"; private static final String GET_COMMISIONS_ENDPOINT = "/api/v1/commisions/getAllCommisions"; + private static final String GET_SOMEONE_COMMISIONS_ENDPOINT = "/api/v1/commisions/user"; private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); @@ -161,6 +162,28 @@ public class CommisionControllerTest { .andExpect(status().is4xxClientError()); } + @Test + public void shouldGetStudentCommisionsListByDeanary() throws Exception { + this.checkUsers(); + + final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(GET_SOMEONE_COMMISIONS_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString()) + .header("Authorization", "Bearer " + token)).andExpect(status().isOk()); + } + + @Test + public void shouldFailStudentCommisionsListByOtherStudent() throws Exception { + this.checkUsers(); + + final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(GET_SOMEONE_COMMISIONS_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString()) + .header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError()); + } + private void checkUsers() { if (CommisionControllerTest.user == null) { CommisionControllerTest.user = new User(null, null, TEST_COMMISIONS_STUDENT_EMAIL, UserRoles.STUDENT);