Added response mappers

This commit is contained in:
Filip Izydorczyk
2020-10-19 12:13:02 +02:00
parent 0ad97a8e3f
commit 67c8b9bf25
6 changed files with 78 additions and 7 deletions

View File

@ -1,8 +1,10 @@
package com.plannaplan.repositories;
import java.util.List;
import java.util.Optional;
import com.plannaplan.entities.User;
import com.plannaplan.types.UserRoles;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
@ -16,4 +18,10 @@ public interface UserRepository extends JpaRepository<User, Long> {
@Query("FROM User WHERE token = ?1")
Optional<User> getByToken(@Param("token") String token);
@Query("FROM User WHERE (name LIKE %?1% OR surname LIKE %?1%)")
List<User> searchForUsers(@Param("query") String query);
@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);
}

View File

@ -1,11 +1,13 @@
package com.plannaplan.services;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.repositories.UserRepository;
import com.plannaplan.types.UserRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -23,7 +25,7 @@ public class UserService {
User user = this.repo.getByAuthority(authority.replace("\n", "").trim())
.orElseThrow(() -> new UserNotFoundException("Can not find user with given authority"));
String token = UUID.randomUUID().toString();
final String token = UUID.randomUUID().toString();
user.setToken(token);
this.repo.save(user);
return token;
@ -43,4 +45,8 @@ public class UserService {
return this.repo.getByToken(token);
}
public List<User> searchForStudents(String query) {
return this.repo.searchForUsers(query, UserRoles.STUDENT);
}
}