diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java index 150fc49..c792ce7 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java @@ -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 { @Query("FROM User WHERE token = ?1") Optional getByToken(@Param("token") String token); + + @Query("FROM User WHERE (name LIKE %?1% OR surname LIKE %?1%)") + List searchForUsers(@Param("query") String query); + + @Query("FROM User WHERE (name LIKE %?1% OR surname LIKE %?1%) AND role=?2") + List searchForUsers(@Param("query") String query, @Param("role") UserRoles role); } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java index 3b3e070..4eaa65e 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java @@ -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 searchForStudents(String query) { + return this.repo.searchForUsers(query, UserRoles.STUDENT); + } + } \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/App.java b/restservice/src/main/java/com/plannaplan/App.java index b85cbb9..d63eba5 100755 --- a/restservice/src/main/java/com/plannaplan/App.java +++ b/restservice/src/main/java/com/plannaplan/App.java @@ -32,7 +32,7 @@ public class App { filip.setEmail("filizy@st.amu.edu.pl"); filip.setName("Filip"); filip.setSurname("Izydorczyk"); - filip.setRole(UserRoles.STUDENT); + filip.setRole(UserRoles.DEANERY); this.userService.save(filip); User hub = new User(); diff --git a/restservice/src/main/java/com/plannaplan/controllers/StudentsController.java b/restservice/src/main/java/com/plannaplan/controllers/UsersController.java similarity index 64% rename from restservice/src/main/java/com/plannaplan/controllers/StudentsController.java rename to restservice/src/main/java/com/plannaplan/controllers/UsersController.java index 596e52b..b3d2dd0 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/StudentsController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/UsersController.java @@ -1,10 +1,15 @@ package com.plannaplan.controllers; +import java.util.List; + import com.plannaplan.App; import com.plannaplan.entities.Course; +import com.plannaplan.entities.User; import com.plannaplan.responses.mappers.CoursesResponseMappers; +import com.plannaplan.responses.mappers.UserResponseMappers; import com.plannaplan.responses.models.GetCoursesResponse; import com.plannaplan.responses.models.GetCoursesWithGroupsResponse; +import com.plannaplan.responses.models.SearchForStudentsResponse; import com.plannaplan.services.CourseService; import com.plannaplan.services.UserService; @@ -21,16 +26,18 @@ import org.springframework.web.bind.annotation.RequestParam; @RestController @CrossOrigin -@RequestMapping("/api/" + App.API_VERSION + "/students") +@RequestMapping("/api/" + App.API_VERSION + "/users") @EnableGlobalMethodSecurity(prePostEnabled = true) -public class StudentsController { +public class UsersController { @Autowired private UserService userService; - @GetMapping("/searchForUsers") + @GetMapping("/searchForStudents") @PreAuthorize("hasRole('ROLE_DEANERY')") - public ResponseEntity configApp(@RequestParam("query") String query) { - return new ResponseEntity<>(HttpStatus.OK); + public ResponseEntity> configApp(@RequestParam("query") String query) { + final List searches = this.userService.searchForStudents(query); + final List response = UserResponseMappers.mapToDefaultResponse(searches); + return new ResponseEntity<>(response, HttpStatus.OK); } } diff --git a/restservice/src/main/java/com/plannaplan/responses/mappers/UserResponseMappers.java b/restservice/src/main/java/com/plannaplan/responses/mappers/UserResponseMappers.java new file mode 100755 index 0000000..f039341 --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/UserResponseMappers.java @@ -0,0 +1,15 @@ +package com.plannaplan.responses.mappers; + +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +import com.plannaplan.entities.User; +import com.plannaplan.responses.models.SearchForStudentsResponse; + +public class UserResponseMappers { + public static List mapToDefaultResponse(List groups) { + return groups.stream().filter(Objects::nonNull).map(SearchForStudentsResponse::new) + .collect(Collectors.toList()); + } +} diff --git a/restservice/src/main/java/com/plannaplan/responses/models/SearchForStudentsResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/SearchForStudentsResponse.java new file mode 100755 index 0000000..01602be --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/responses/models/SearchForStudentsResponse.java @@ -0,0 +1,35 @@ +package com.plannaplan.responses.models; + +import com.plannaplan.entities.User; + +public class SearchForStudentsResponse { + + private Long id; + private String name; + private String surname; + private String email; + + public SearchForStudentsResponse(User user) { + this.id = user.getId(); + this.name = user.getName(); + this.surname = user.getSurname(); + this.email = user.getEmail(); + } + + public String getEmail() { + return email; + } + + public String getSurname() { + return surname; + } + + public String getName() { + return name; + } + + public Long getId() { + return id; + } + +}