lab-3 adding book and users pagination
This commit is contained in:
parent
03b6da09d5
commit
c71d75685b
@ -50,9 +50,11 @@ public class BookController {
|
||||
|
||||
@GetMapping
|
||||
public List<BookDto> getAll(
|
||||
@RequestParam(name = "typeId", defaultValue = "0") Long typeId,
|
||||
@RequestParam(name = "authorId", defaultValue = "0") Long authorId) {
|
||||
return itemService.getAllbyFilter(typeId, authorId).stream().map(this::toDto).toList();
|
||||
@RequestParam(name = "typeId", defaultValue = "0") Long typeId,
|
||||
@RequestParam(name = "authorId", defaultValue = "0") Long authorId,
|
||||
@RequestParam(name = "page", defaultValue = "0") int page,
|
||||
@RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) {
|
||||
return itemService.getAll(typeId, authorId).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
@ -3,13 +3,20 @@ package com.ip.library.books.repository;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
import com.ip.library.books.model.BookEntity;
|
||||
|
||||
public interface BookRepository extends CrudRepository<BookEntity, Long> {
|
||||
public interface BookRepository extends
|
||||
CrudRepository<BookEntity, Long>,
|
||||
PagingAndSortingRepository<BookEntity, Long> {
|
||||
Optional<BookEntity> findByNameIgnoreCase(String name);
|
||||
List<BookEntity> findByTypeId(long typeId);
|
||||
List<BookEntity> findByTypeId(long typeId, Pageable pageable);
|
||||
List<BookEntity> findByAuthorId(long authorId);
|
||||
List<BookEntity> findByAuthorId(long authorId, Pageable pageable);
|
||||
List<BookEntity> findByTypeIdAndAuthorId(long typeId, long authorId);
|
||||
List<BookEntity> findByTypeIdAndAuthorId(long typeId, long authorId, Pageable pageable);
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package com.ip.library.books.service;
|
||||
import java.util.List;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@ -27,12 +28,12 @@ public class BookService {
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<BookEntity> getAll() {
|
||||
public List<BookEntity> getAll(){
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<BookEntity> getAllbyFilter(long typeId, long authorId) {
|
||||
public List<BookEntity> getAll(long typeId, long authorId) {
|
||||
if (typeId <= 0L && authorId <= 0L) {
|
||||
return getAll();
|
||||
}
|
||||
@ -45,6 +46,21 @@ public class BookService {
|
||||
return repository.findByTypeIdAndAuthorId(typeId, authorId);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<BookEntity> getAll(long typeId, long authorId, int page, int size) {
|
||||
PageRequest pageRequest = PageRequest.of(page, size);
|
||||
if (typeId <= 0L && authorId <= 0L) {
|
||||
return StreamSupport.stream(repository.findAll(pageRequest).spliterator(), false).toList();
|
||||
}
|
||||
if (typeId <= 0L){
|
||||
return repository.findByAuthorId(authorId, pageRequest);
|
||||
}
|
||||
if (authorId <= 0L){
|
||||
return repository.findByTypeId(typeId, pageRequest);
|
||||
}
|
||||
return repository.findByTypeIdAndAuthorId(typeId, authorId, pageRequest);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public BookEntity get(long id) {
|
||||
return repository.findById(id)
|
||||
|
@ -0,0 +1,97 @@
|
||||
package com.ip.library.core.api;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PageDto<D> {
|
||||
private List<D> items = new ArrayList<>();
|
||||
private int itemsCount;
|
||||
private int currentPage;
|
||||
private int currentSize;
|
||||
private int totalPages;
|
||||
private long totalItems;
|
||||
private boolean isFirst;
|
||||
private boolean isLast;
|
||||
private boolean hasNext;
|
||||
private boolean hasPrevious;
|
||||
|
||||
public List<D> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(List<D> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public int getItemsCount() {
|
||||
return itemsCount;
|
||||
}
|
||||
|
||||
public void setItemsCount(int itemsCount) {
|
||||
this.itemsCount = itemsCount;
|
||||
}
|
||||
|
||||
public int getCurrentPage() {
|
||||
return currentPage;
|
||||
}
|
||||
|
||||
public void setCurrentPage(int currentPage) {
|
||||
this.currentPage = currentPage;
|
||||
}
|
||||
|
||||
public int getCurrentSize() {
|
||||
return currentSize;
|
||||
}
|
||||
|
||||
public void setCurrentSize(int currentSize) {
|
||||
this.currentSize = currentSize;
|
||||
}
|
||||
|
||||
public int getTotalPages() {
|
||||
return totalPages;
|
||||
}
|
||||
|
||||
public void setTotalPages(int totalPages) {
|
||||
this.totalPages = totalPages;
|
||||
}
|
||||
|
||||
public long getTotalItems() {
|
||||
return totalItems;
|
||||
}
|
||||
|
||||
public void setTotalItems(long totalItems) {
|
||||
this.totalItems = totalItems;
|
||||
}
|
||||
|
||||
public boolean isFirst() {
|
||||
return isFirst;
|
||||
}
|
||||
|
||||
public void setFirst(boolean isFirst) {
|
||||
this.isFirst = isFirst;
|
||||
}
|
||||
|
||||
public boolean isLast() {
|
||||
return isLast;
|
||||
}
|
||||
|
||||
public void setLast(boolean isLast) {
|
||||
this.isLast = isLast;
|
||||
}
|
||||
|
||||
public boolean isHasNext() {
|
||||
return hasNext;
|
||||
}
|
||||
|
||||
public void setHasNext(boolean hasNext) {
|
||||
this.hasNext = hasNext;
|
||||
}
|
||||
|
||||
public boolean isHasPrevious() {
|
||||
return hasPrevious;
|
||||
}
|
||||
|
||||
public void setHasPrevious(boolean hasPrevious) {
|
||||
this.hasPrevious = hasPrevious;
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.ip.library.core.api;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
public class PageDtoMapper {
|
||||
private PageDtoMapper() {}
|
||||
|
||||
public static <D, E> PageDto<D> toDto(Page<E> page, Function<E, D> mapper) {
|
||||
final PageDto<D> dto = new PageDto<>();
|
||||
dto.setItems(page.getContent().stream().map(mapper::apply).toList());
|
||||
dto.setItemsCount(page.getNumberOfElements());
|
||||
dto.setCurrentPage(page.getNumber());
|
||||
dto.setCurrentSize(page.getSize());
|
||||
dto.setTotalPages(page.getTotalPages());
|
||||
dto.setTotalItems(page.getTotalElements());
|
||||
dto.setFirst(page.isFirst());
|
||||
dto.setLast(page.isLast());
|
||||
dto.setHasNext(page.hasNext());
|
||||
dto.setHasPrevious(page.hasPrevious());
|
||||
return dto;
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ public class Constants {
|
||||
|
||||
public static final String API_URL = "/api/1.0";
|
||||
|
||||
private Constants() {
|
||||
}
|
||||
public static final String DEFAULT_PAGE_SIZE = "5";
|
||||
|
||||
private Constants() {}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.ip.library.users.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@ -10,8 +8,11 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.ip.library.core.api.PageDto;
|
||||
import com.ip.library.core.api.PageDtoMapper;
|
||||
import com.ip.library.core.configuration.Constants;
|
||||
import com.ip.library.users.model.UserEntity;
|
||||
import com.ip.library.users.service.UserService;
|
||||
@ -38,8 +39,10 @@ public class UserController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<UserDto> getAll() {
|
||||
return userService.getAll().stream().map(this::toDto).toList();
|
||||
public PageDto<UserDto> getAll(
|
||||
@RequestParam(name = "page", defaultValue = "0") int page,
|
||||
@RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) {
|
||||
return PageDtoMapper.toDto(userService.getAll(page, size), this::toDto);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
@ -3,9 +3,12 @@ package com.ip.library.users.repository;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
import com.ip.library.users.model.UserEntity;
|
||||
|
||||
public interface UserRepository extends CrudRepository<UserEntity, Long> {
|
||||
public interface UserRepository extends
|
||||
CrudRepository<UserEntity, Long>,
|
||||
PagingAndSortingRepository<UserEntity, Long> {
|
||||
Optional<UserEntity> findByLoginIgnoreCase(String login);
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package com.ip.library.users.service;
|
||||
import java.util.List;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@ -31,6 +33,11 @@ public class UserService {
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Page<UserEntity> getAll(int page, int size) {
|
||||
return repository.findAll(PageRequest.of(page, size));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public UserEntity get(long id) {
|
||||
return repository.findById(id)
|
||||
|
Loading…
Reference in New Issue
Block a user