Added response mappers
This commit is contained in:
parent
0ad97a8e3f
commit
67c8b9bf25
@ -1,8 +1,10 @@
|
|||||||
package com.plannaplan.repositories;
|
package com.plannaplan.repositories;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import com.plannaplan.entities.User;
|
import com.plannaplan.entities.User;
|
||||||
|
import com.plannaplan.types.UserRoles;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
@ -16,4 +18,10 @@ public interface UserRepository extends JpaRepository<User, Long> {
|
|||||||
|
|
||||||
@Query("FROM User WHERE token = ?1")
|
@Query("FROM User WHERE token = ?1")
|
||||||
Optional<User> getByToken(@Param("token") String token);
|
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);
|
||||||
}
|
}
|
@ -1,11 +1,13 @@
|
|||||||
package com.plannaplan.services;
|
package com.plannaplan.services;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import com.plannaplan.entities.User;
|
import com.plannaplan.entities.User;
|
||||||
import com.plannaplan.exceptions.UserNotFoundException;
|
import com.plannaplan.exceptions.UserNotFoundException;
|
||||||
import com.plannaplan.repositories.UserRepository;
|
import com.plannaplan.repositories.UserRepository;
|
||||||
|
import com.plannaplan.types.UserRoles;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -23,7 +25,7 @@ public class UserService {
|
|||||||
User user = this.repo.getByAuthority(authority.replace("\n", "").trim())
|
User user = this.repo.getByAuthority(authority.replace("\n", "").trim())
|
||||||
.orElseThrow(() -> new UserNotFoundException("Can not find user with given authority"));
|
.orElseThrow(() -> new UserNotFoundException("Can not find user with given authority"));
|
||||||
|
|
||||||
String token = UUID.randomUUID().toString();
|
final String token = UUID.randomUUID().toString();
|
||||||
user.setToken(token);
|
user.setToken(token);
|
||||||
this.repo.save(user);
|
this.repo.save(user);
|
||||||
return token;
|
return token;
|
||||||
@ -43,4 +45,8 @@ public class UserService {
|
|||||||
return this.repo.getByToken(token);
|
return this.repo.getByToken(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<User> searchForStudents(String query) {
|
||||||
|
return this.repo.searchForUsers(query, UserRoles.STUDENT);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -32,7 +32,7 @@ public class App {
|
|||||||
filip.setEmail("filizy@st.amu.edu.pl");
|
filip.setEmail("filizy@st.amu.edu.pl");
|
||||||
filip.setName("Filip");
|
filip.setName("Filip");
|
||||||
filip.setSurname("Izydorczyk");
|
filip.setSurname("Izydorczyk");
|
||||||
filip.setRole(UserRoles.STUDENT);
|
filip.setRole(UserRoles.DEANERY);
|
||||||
this.userService.save(filip);
|
this.userService.save(filip);
|
||||||
|
|
||||||
User hub = new User();
|
User hub = new User();
|
||||||
|
@ -1,10 +1,15 @@
|
|||||||
package com.plannaplan.controllers;
|
package com.plannaplan.controllers;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import com.plannaplan.App;
|
import com.plannaplan.App;
|
||||||
import com.plannaplan.entities.Course;
|
import com.plannaplan.entities.Course;
|
||||||
|
import com.plannaplan.entities.User;
|
||||||
import com.plannaplan.responses.mappers.CoursesResponseMappers;
|
import com.plannaplan.responses.mappers.CoursesResponseMappers;
|
||||||
|
import com.plannaplan.responses.mappers.UserResponseMappers;
|
||||||
import com.plannaplan.responses.models.GetCoursesResponse;
|
import com.plannaplan.responses.models.GetCoursesResponse;
|
||||||
import com.plannaplan.responses.models.GetCoursesWithGroupsResponse;
|
import com.plannaplan.responses.models.GetCoursesWithGroupsResponse;
|
||||||
|
import com.plannaplan.responses.models.SearchForStudentsResponse;
|
||||||
import com.plannaplan.services.CourseService;
|
import com.plannaplan.services.CourseService;
|
||||||
import com.plannaplan.services.UserService;
|
import com.plannaplan.services.UserService;
|
||||||
|
|
||||||
@ -21,16 +26,18 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@CrossOrigin
|
@CrossOrigin
|
||||||
@RequestMapping("/api/" + App.API_VERSION + "/students")
|
@RequestMapping("/api/" + App.API_VERSION + "/users")
|
||||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||||
public class StudentsController {
|
public class UsersController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
@GetMapping("/searchForUsers")
|
@GetMapping("/searchForStudents")
|
||||||
@PreAuthorize("hasRole('ROLE_DEANERY')")
|
@PreAuthorize("hasRole('ROLE_DEANERY')")
|
||||||
|
|
||||||
public ResponseEntity<String> configApp(@RequestParam("query") String query) {
|
public ResponseEntity<List<SearchForStudentsResponse>> configApp(@RequestParam("query") String query) {
|
||||||
return new ResponseEntity<>(HttpStatus.OK);
|
final List<User> searches = this.userService.searchForStudents(query);
|
||||||
|
final List<SearchForStudentsResponse> response = UserResponseMappers.mapToDefaultResponse(searches);
|
||||||
|
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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<SearchForStudentsResponse> mapToDefaultResponse(List<User> groups) {
|
||||||
|
return groups.stream().filter(Objects::nonNull).map(SearchForStudentsResponse::new)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user