Added another repository method + test

This commit is contained in:
BuildTools 2020-11-26 13:58:27 +01:00
parent 0130fb77e1
commit 2545a0e682
4 changed files with 35 additions and 10 deletions

View File

@ -1,7 +1,6 @@
package com.plannaplan.repositories;
import java.util.List;
import java.util.HashMap;
import java.util.Optional;
import com.plannaplan.entities.Groups;
@ -22,9 +21,10 @@ public interface GroupRepository extends JpaRepository<Groups, Long> {
@Query("SELECT COUNT(*) AS assinged_times FROM Assignment WHERE isPastAssignment=false GROUP BY group HAVING group_id=?1")
Optional<Number> 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)")
@Query("SELECT group, COUNT(*) AS assinged_times FROM Assignment a WHERE a.isPastAssignment=false GROUP BY a.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 (:ids)")
List<Object[]> getAssignedAmounts(@Param("ids") List<Long> groupIds);

View File

@ -56,15 +56,10 @@ public class GroupService {
public HashMap<Groups, Integer> getTakenPlaces(List<Groups> groups) {
HashMap<Groups, Integer> 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<Object[]> respoonse2 = this.repo.getAssignedAmounts(
groups.stream().filter(Objects::nonNull).map(Groups::getId).collect(Collectors.toList()));
return respoonse;
}
}

View File

@ -7,6 +7,10 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertTrue;
import java.util.List;
import javax.swing.GroupLayout.Group;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups;
@ -76,4 +80,29 @@ public class GroupRepositoryTest {
assertTrue(afterdeAssignedGroupAmmount < afterreAssignedGroupAmmount);
}
@Test
public void shouldReturnGroupAssignmentTimesList() throws InterruptedException {
final Groups testGroup = groupService.save(new Groups(43, "A-41", null, 645, WeekDay.MONDAY, null));
final Groups testGroup2 = groupService.save(new Groups(433, "A-41", null, 235, WeekDay.TUESDAY, null));
final Groups testGroup3 = groupService.save(new Groups(23, "A-41", null, 340, WeekDay.MONDAY, null));
final User user = this.userService.save(new User("Dare", "Oc",
"shouldReturnGroupAssignmentTimesList@grouprepository.test", UserRoles.STUDENT));
final Commision commision = this.commisionService.save(new Commision(user));
this.assignmentService.save(new Assignment(testGroup, commision));
this.assignmentService.save(new Assignment(testGroup2, commision));
this.assignmentService.save(new Assignment(testGroup3, commision));
Thread.sleep(1000);
List<Object[]> response = this.repository
.getAssignedAmounts(List.of(testGroup.getId(), testGroup2.getId(), testGroup3.getId()));
assertTrue("Response should have size 3", response.size() == 3);
assertTrue("Instance of firest element should be Group", response.get(0)[0] instanceof Groups);
assertTrue("Instance of firest element should be Long", response.get(0)[1] instanceof Long);
}
}

View File

@ -7,5 +7,6 @@ spring.jpa.open-in-view=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jackson.serialization.fail-on-empty-beans=false
spring.main.allow-bean-definition-overriding=true
spring.jackson.default-property-inclusion = NON_NULL
server.port=1285