diff --git a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java index 03c835c..ba5b603 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java @@ -33,6 +33,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.util.Assert; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; @RestController @CrossOrigin @@ -96,10 +97,20 @@ public class CommisionController extends TokenBasedController { @GetMapping("/user") @ApiOperation("Return list of user all commisions (history of schedules)") - public ResponseEntity> getAlCommisions() throws UserNotFoundException { + public ResponseEntity> getAlCommisions( + @RequestParam(name = "groups", defaultValue = "false") @ApiParam(value = "Boolean if we want to display wiht commision's group ids") Boolean groups) + throws UserNotFoundException { User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); - List result = CommisionResponseMappers - .mapToResponse(this.commisionService.getUsersCommisions(user)); + + List result; + + if (!groups) { + result = CommisionResponseMappers.mapToResponse(this.commisionService.getUsersCommisions(user)); + } else { + result = CommisionResponseMappers + .mapToResponseWithGroups(this.commisionService.getUsersCommisions(user)); + } + return new ResponseEntity<>(result, HttpStatus.OK); } diff --git a/restservice/src/main/java/com/plannaplan/responses/mappers/CommisionResponseMappers.java b/restservice/src/main/java/com/plannaplan/responses/mappers/CommisionResponseMappers.java index 3303e96..7ce70d2 100755 --- a/restservice/src/main/java/com/plannaplan/responses/mappers/CommisionResponseMappers.java +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/CommisionResponseMappers.java @@ -6,9 +6,15 @@ import java.util.stream.Collectors; import com.plannaplan.entities.Commision; import com.plannaplan.responses.models.CommisionResponse; +import com.plannaplan.responses.models.CommisionWithGroupsResponse; public class CommisionResponseMappers { public static final List mapToResponse(List commisions) { return commisions.stream().filter(Objects::nonNull).map(CommisionResponse::new).collect(Collectors.toList()); } + + public static final List mapToResponseWithGroups(List commisions) { + return commisions.stream().filter(Objects::nonNull).map(CommisionWithGroupsResponse::new) + .collect(Collectors.toList()); + } } diff --git a/restservice/src/main/java/com/plannaplan/responses/models/CommisionWithGroupsResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/CommisionWithGroupsResponse.java new file mode 100755 index 0000000..435f754 --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/responses/models/CommisionWithGroupsResponse.java @@ -0,0 +1,36 @@ +package com.plannaplan.responses.models; + +import java.util.List; +import java.util.Objects; +import java.util.function.Function; +import java.util.stream.Collectors; + +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel(description = "Response shows information about commision and its groups.", value = "CommisionWithGroupsResponse") +public class CommisionWithGroupsResponse extends CommisionResponse { + + @ApiModelProperty(value = "List of groups ids in databse that belongs to commision") + private List groups; + + public CommisionWithGroupsResponse(Commision commision) { + super(commision); + this.groups = commision.getAssignments().stream().filter(Objects::nonNull) + .map(new Function() { + + @Override + public Long apply(Assignment arg0) { + return arg0.getGroup().getId(); + } + }).collect(Collectors.toList()); + } + + public List getGroups() { + return groups; + } + +} diff --git a/restservice/src/test/java/com/plannaplan/responses/mappers/CommisionResponseMappersTest.java b/restservice/src/test/java/com/plannaplan/responses/mappers/CommisionResponseMappersTest.java index e378617..a46b58a 100755 --- a/restservice/src/test/java/com/plannaplan/responses/mappers/CommisionResponseMappersTest.java +++ b/restservice/src/test/java/com/plannaplan/responses/mappers/CommisionResponseMappersTest.java @@ -2,11 +2,16 @@ package com.plannaplan.responses.mappers; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.lang.reflect.Field; 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.CommisionResponse; +import com.plannaplan.responses.models.CommisionWithGroupsResponse; +import com.plannaplan.types.WeekDay; import org.junit.Test; @@ -20,4 +25,33 @@ public class CommisionResponseMappersTest { assertTrue(resposne.get(0).getCommisionDate().equals(coms.get(0).getCommisionDate().toString())); assertTrue(resposne.get(1).getCommisionDate().equals(coms.get(1).getCommisionDate().toString())); } + + @Test + public void shouldMapListWithEntityToListOfResponsesWithGroups() + throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { + + final Field reader = Commision.class.getDeclaredField("assignments"); + final Field groupReader = Groups.class.getDeclaredField("id"); + reader.setAccessible(true); + groupReader.setAccessible(true); + + final Commision com1 = new Commision(null); + final Groups group1 = new Groups(43, "BRAK", null, 840, WeekDay.MONDAY, null); + + groupReader.set(group1, Long.valueOf(8)); + reader.set(com1, Arrays.asList(new Assignment(group1, com1))); + + final Commision com2 = new Commision(null); + final Groups group2 = new Groups(124, "BRAK", null, 900, WeekDay.WEDNESDAY, null); + + groupReader.set(group2, Long.valueOf(9)); + reader.set(com2, Arrays.asList(new Assignment(group2, com2))); + + final List resposne = CommisionResponseMappers + .mapToResponseWithGroups(Arrays.asList(com1, com2)); + + assertTrue(resposne.size() == 2); + assertTrue(resposne.get(0).getGroups().get(0) == 8); + assertTrue(resposne.get(1).getGroups().get(0) == 9); + } } diff --git a/restservice/src/test/java/com/plannaplan/responses/models/CommisionWithGroupsResponseTest.java b/restservice/src/test/java/com/plannaplan/responses/models/CommisionWithGroupsResponseTest.java new file mode 100755 index 0000000..e9ef476 --- /dev/null +++ b/restservice/src/test/java/com/plannaplan/responses/models/CommisionWithGroupsResponseTest.java @@ -0,0 +1,34 @@ +package com.plannaplan.responses.models; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.lang.reflect.Field; +import java.util.Arrays; + +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; +import com.plannaplan.entities.Groups; +import com.plannaplan.types.WeekDay; + +import org.junit.Test; + +public class CommisionWithGroupsResponseTest { + @Test + public void shouldMapCommisionToResponse() + throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { + final Field reader = Commision.class.getDeclaredField("assignments"); + final Field groupReader = Groups.class.getDeclaredField("id"); + reader.setAccessible(true); + groupReader.setAccessible(true); + + final Commision com = new Commision(null); + final Groups group = new Groups(43, "BRAK", null, 840, WeekDay.MONDAY, null); + + groupReader.set(group, Long.valueOf(8)); + reader.set(com, Arrays.asList(new Assignment(group, com))); + + final CommisionWithGroupsResponse comResponse = new CommisionWithGroupsResponse(com); + + assertTrue(comResponse.getGroups().get(0) == 8); + } +}