Added getting sorted students

This commit is contained in:
Filip Izydorczyk 2021-01-02 14:35:00 +01:00
parent ff9aa64470
commit 4b096a50bf
4 changed files with 52 additions and 7 deletions

View File

@ -35,13 +35,13 @@ import org.springframework.stereotype.Repository;
@Repository @Repository
public interface UserRepository extends JpaRepository<User, Long> { public interface UserRepository extends JpaRepository<User, Long> {
@Query("FROM User WHERE email = ?1 OR usosId = ?1")
/** /**
* return user by given authority * return user by given authority
* *
* @param authority user usosId or email * @param authority user usosId or email
* @return optional with user if found * @return optional with user if found
*/ */
@Query("FROM User WHERE email = ?1 OR usosId = ?1")
Optional<User> getByAuthority(@Param("authority") String authority); Optional<User> getByAuthority(@Param("authority") String authority);
@Query("FROM User WHERE email = ?1") @Query("FROM User WHERE email = ?1")
@ -56,7 +56,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
@Query("FROM User WHERE (name LIKE %?1% OR surname LIKE %?1%)") @Query("FROM User WHERE (name LIKE %?1% OR surname LIKE %?1%)")
List<User> searchForUsers(@Param("query") String query); List<User> searchForUsers(@Param("query") String query);
@Query("FROM User WHERE (name LIKE %?1% OR surname LIKE %?1%) AND role=?2")
/** /**
* search for user with given query * search for user with given query
* *
@ -64,6 +63,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
* @param role limits results by role * @param role limits results by role
* @return list opf results * @return list opf results
*/ */
@Query("FROM User WHERE (name LIKE %?1% OR surname LIKE %?1%) AND role=?2")
List<User> searchForUsers(@Param("query") String query, @Param("role") UserRoles role); List<User> searchForUsers(@Param("query") String query, @Param("role") UserRoles role);
@Query("FROM User WHERE role=?1") @Query("FROM User WHERE role=?1")

View File

@ -5,13 +5,15 @@ import java.util.Optional;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import com.plannaplan.entities.User;
import com.plannaplan.repositories.AssignmentRepository; import com.plannaplan.repositories.AssignmentRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
* Service of Assignment which can save assignments, diplay assignments, get ammount of assigments. * Service of Assignment which can save assignments, diplay assignments, get
* ammount of assigments.
*/ */
@Service @Service
@ -19,11 +21,15 @@ public class AssignmentService {
@Autowired @Autowired
private AssignmentRepository repo; private AssignmentRepository repo;
@Autowired
private UserService userService;
public AssignmentService() { public AssignmentService() {
} }
/** /**
* Save given assignment * Save given assignment
*
* @param assignment assignment to save * @param assignment assignment to save
* @return assignment saved assignment with database id * @return assignment saved assignment with database id
*/ */
@ -32,16 +38,14 @@ public class AssignmentService {
} }
/* /*
* getCommisionAssignments * getCommisionAssignments Return id of the commision
* Return id of the commision
*/ */
public List<Assignment> getCommisionAssignments(Commision com) { public List<Assignment> getCommisionAssignments(Commision com) {
return this.repo.getByCommision(com.getId()); return this.repo.getByCommision(com.getId());
} }
/* /*
* getAssignmentsAmmount * getAssignmentsAmmount Return count assignments ammount
* Return count assignments ammount
*/ */
public long getAssignmentsAmmount() { public long getAssignmentsAmmount() {
return this.repo.count(); return this.repo.count();
@ -49,10 +53,19 @@ public class AssignmentService {
/** /**
* Get assigmnent by id * Get assigmnent by id
*
* @param id id of assigmnent * @param id id of assigmnent
* @return Optional of assignment * @return Optional of assignment
*/ */
public Optional<Assignment> getById(Long id) { public Optional<Assignment> getById(Long id) {
return this.repo.findById(id); return this.repo.findById(id);
} }
public void callAcceptAlgorythm() {
final List<User> students = this.userService.getStudentsSortedByRanking();
students.forEach(e -> {
System.out.println(e.getRanking());
});
}
} }

View File

@ -3,6 +3,7 @@ package com.plannaplan.services;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
import com.plannaplan.entities.User; import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException; import com.plannaplan.exceptions.UserNotFoundException;
@ -153,4 +154,15 @@ public class UserService {
return this.repo.getAllByRole(UserRoles.ADMIN).size() > 0; return this.repo.getAllByRole(UserRoles.ADMIN).size() > 0;
} }
/**
* get students sorted by their ranking
*
* @return list of students
*/
public List<User> getStudentsSortedByRanking() {
return this.repo.getAllByRole(UserRoles.STUDENT).stream().sorted((u1, u2) -> {
return -1 * u1.getRanking().compareTo(u2.getRanking());
}).collect(Collectors.toList());
}
} }

View File

@ -2,12 +2,19 @@ package com.plannaplan.services;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.MethodMode;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.stream.IntStream;
import javax.management.relation.Role;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
@ -56,6 +63,19 @@ public class AssignmentServiceTest {
assertTrue("Returned list size should be 1", response.size() == 1); assertTrue("Returned list size should be 1", response.size() == 1);
} }
@Test
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldPerformAcceptAlgorythm() {
final Random generator = new Random();
IntStream.range(0, 1700).forEach(i -> {
this.userService.save(new User(null, null,
"shouldPerformAcceptAlgorythm-" + UUID.randomUUID().toString() + "@AssignmentService.test", null,
UserRoles.STUDENT, generator.nextInt(400) + 100));
});
this.service.callAcceptAlgorythm();
System.out.println("-X_-x-x-x-_X-x-x-x-X_-x-x-x-");
}
private void addAssignmentToCommision(Commision com) { private void addAssignmentToCommision(Commision com) {
Assignment a = new Assignment(null, com); Assignment a = new Assignment(null, com);
this.service.save(a); this.service.save(a);