lab6 fix
This commit is contained in:
parent
299a62b364
commit
89fce66638
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -1,13 +1,19 @@
|
||||
package ru.ip.labs.labs.films.controller;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.security.access.annotation.Secured;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ip.labs.labs.films.dto.UserDto;
|
||||
import ru.ip.labs.labs.films.dto.UserInfoDTO;
|
||||
import ru.ip.labs.labs.films.dto.UsersPageDTO;
|
||||
import ru.ip.labs.labs.films.models.User;
|
||||
import ru.ip.labs.labs.films.models.UserRole;
|
||||
import ru.ip.labs.labs.films.service.UserService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
@ -26,6 +32,19 @@ public class UserController {
|
||||
User user = userService.findByLogin(userDetails.getUsername());
|
||||
return user.getRole().toString();
|
||||
}
|
||||
@GetMapping("/users")
|
||||
@Secured({UserRole.AsString.ADMIN})
|
||||
public UsersPageDTO getUsers(@RequestParam(defaultValue = "1") int page,
|
||||
@RequestParam(defaultValue = "5") int size) {
|
||||
final Page<UserDto> users = userService.findAllPages(page, size)
|
||||
.map(UserDto::new);
|
||||
final int totalPages = users.getTotalPages();
|
||||
final List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
|
||||
.boxed()
|
||||
.toList();
|
||||
return new UsersPageDTO(users, pageNumbers, totalPages);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping(URL_SIGNUP)
|
||||
public UserInfoDTO signup(@RequestBody @Valid UserDto userDto) {
|
||||
|
@ -1,5 +1,8 @@
|
||||
package ru.ip.labs.labs.films.dto;
|
||||
|
||||
import ru.ip.labs.labs.films.models.User;
|
||||
import ru.ip.labs.labs.films.models.UserRole;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
public class UserDto {
|
||||
@ -7,8 +10,16 @@ public class UserDto {
|
||||
private String login;
|
||||
@NotEmpty
|
||||
private String password;
|
||||
private UserRole role;
|
||||
private String passwordConfirm;
|
||||
|
||||
public UserDto(User user) {
|
||||
this.login = user.getLogin();
|
||||
this.password = user.getPassword();
|
||||
this.role = user.getRole();
|
||||
}
|
||||
|
||||
public UserDto() {}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
@ -21,4 +32,7 @@ public class UserDto {
|
||||
return passwordConfirm;
|
||||
}
|
||||
|
||||
public UserRole getRole() {
|
||||
return role;
|
||||
}
|
||||
}
|
||||
|
29
src/main/java/ru/ip/labs/labs/films/dto/UsersPageDTO.java
Normal file
29
src/main/java/ru/ip/labs/labs/films/dto/UsersPageDTO.java
Normal file
@ -0,0 +1,29 @@
|
||||
package ru.ip.labs.labs.films.dto;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UsersPageDTO {
|
||||
private Page<UserDto> users;
|
||||
private List<Integer> pageNumbers;
|
||||
private int totalPages;
|
||||
|
||||
public UsersPageDTO(Page<UserDto> users, List<Integer> pageNumbers, int totalPages) {
|
||||
this.users = users;
|
||||
this.pageNumbers = pageNumbers;
|
||||
this.totalPages = totalPages;
|
||||
}
|
||||
|
||||
public Page<UserDto> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public List<Integer> getPageNumbers() {
|
||||
return pageNumbers;
|
||||
}
|
||||
|
||||
public int getTotalPages() {
|
||||
return totalPages;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user