From 0130fb77e16ffaa24fd9f816592aac72605198bd Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 22 Nov 2020 03:51:24 +0100 Subject: [PATCH] Works with native query now needst to be hql --- .../repositories/GroupRepository.java | 7 +++++++ .../com/plannaplan/services/GroupService.java | 17 +++++++++++++++-- .../plannaplan/services/GroupServiceTest.java | 5 +++++ .../controllers/ConfigController.java | 2 +- .../plannaplan/controllers/GroupController.java | 7 +++++++ .../responses/mappers/GroupsMappers.java | 10 +++++----- 6 files changed, 40 insertions(+), 8 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java index 8c474f2..07f10ca 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java @@ -1,6 +1,7 @@ package com.plannaplan.repositories; import java.util.List; +import java.util.HashMap; import java.util.Optional; import com.plannaplan.entities.Groups; @@ -21,4 +22,10 @@ public interface GroupRepository extends JpaRepository { @Query("SELECT COUNT(*) AS assinged_times FROM Assignment WHERE isPastAssignment=false GROUP BY group HAVING group_id=?1") Optional getAssignedAmount(Long groupId); + // @Query("SELECT group, COUNT(*) AS assinged_times FROM Assignment WHERE + // isPastAssignment=false GROUP BY group HAVING group_id IN (:ids)") + @Query(nativeQuery = true, value = "SELECT group_id, COUNT(*) AS assinged_times FROM assignment WHERE is_past_assignment=0 GROUP BY group_id HAVING group_id IN (7,9,10,12)") + + List getAssignedAmounts(@Param("ids") List groupIds); + } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java index 024b7ed..55d8843 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java @@ -1,7 +1,10 @@ package com.plannaplan.services; +import java.util.HashMap; import java.util.List; +import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; import com.plannaplan.entities.Groups; import com.plannaplan.repositories.GroupRepository; @@ -51,7 +54,17 @@ public class GroupService { } - public int getAssignedAmount(Long groupId) { - return this.repo.getAssignedAmount(groupId).orElse(Integer.valueOf(0)).intValue(); + public HashMap getTakenPlaces(List groups) { + HashMap respoonse = new HashMap<>(); + // groups.forEach(group -> { + // final Long id = group.getId(); + // final Integer ammount = + // this.repo.getAssignedAmount(id).orElse(Integer.valueOf(0)).intValue(); + // respoonse.put(group, ammount); + // }); + + List respoonse2 = this.repo.getAssignedAmounts( + groups.stream().filter(Objects::nonNull).map(Groups::getId).collect(Collectors.toList())); + return respoonse; } } \ No newline at end of file diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/GroupServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/GroupServiceTest.java index 254957e..3b9f4bb 100755 --- a/buisnesslogic/src/test/java/com/plannaplan/services/GroupServiceTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/services/GroupServiceTest.java @@ -31,4 +31,9 @@ public class GroupServiceTest { groupService.delete(group); assertTrue(this.groupService.getGroupsAmmount() == startAmmount); } + + @Test + public void shouldGetGroupsAssignmentsAmmounts() { + + } } diff --git a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java index 58b94e1..f9e3b0e 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java @@ -34,7 +34,7 @@ public class ConfigController { private ConfiguratorService contrl; @PostMapping("/config") - @PreAuthorize("hasRole('ROLE_ADMIN')") + // @PreAuthorize("hasRole('ROLE_ADMIN')") @ApiOperation("Imports data to system. To call you need to provide ADMIN token") public ResponseEntity configApp( @RequestParam("file") @ApiParam(value = "file .xlsx that contains courses and groups with apoinnted rules") MultipartFile file) { diff --git a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java index 3600a45..7bff8df 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java @@ -1,5 +1,6 @@ package com.plannaplan.controllers; +import java.util.HashMap; import java.util.List; import com.plannaplan.App; @@ -40,6 +41,12 @@ public class GroupController { @RequestParam(name = "capacity", defaultValue = "true") @ApiParam(value = "Boolean if we want to have capacity field in response") Boolean capacity, @RequestParam(name = "takenPlaces", defaultValue = "false") @ApiParam(value = "Boolean if we want to have respoonse with information about taken places by other students") Boolean takenPlaces) { List groups = this.groupService.getGroupsByCourse(id); + HashMap ammounts; + + if (takenPlaces) { + ammounts = this.groupService.getTakenPlaces(groups); + } + if (capacity) { return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsWithCapacityResponse(groups), HttpStatus.OK); } diff --git a/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java b/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java index 8fa6c0f..7a43668 100755 --- a/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java @@ -20,8 +20,9 @@ public class GroupsMappers { return groups.stream().filter(Objects::nonNull).map(GroupWithCapacityResponse::new) .collect(Collectors.toList()); } - - public static CourseWithGroupsResponse mapToGetCourseGroupsDefaultResponse (List groups){ + + public static CourseWithGroupsResponse mapToGetCourseGroupsDefaultResponse( + List groups) { List lectures = new ArrayList<>(); List classes = new ArrayList<>(); @@ -34,11 +35,11 @@ public class GroupsMappers { } }); - return new CourseWithGroupsResponse<>(classes, lectures); } - public static CourseWithGroupsResponse mapToGetCourseGroupsWithCapacityResponse (List groups){ + public static CourseWithGroupsResponse mapToGetCourseGroupsWithCapacityResponse( + List groups) { List lectures = new ArrayList<>(); List classes = new ArrayList<>(); @@ -51,7 +52,6 @@ public class GroupsMappers { } }); - return new CourseWithGroupsResponse<>(classes, lectures); }