Skip to content

Commit

Permalink
add pagination and sorting to api
Browse files Browse the repository at this point in the history
  • Loading branch information
Sprokof committed Feb 4, 2024
1 parent 96a345e commit 8b62076
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 52 deletions.
16 changes: 13 additions & 3 deletions src/main/java/ru/investbook/api/AbstractRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@

import jakarta.persistence.GeneratedValue;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -42,10 +47,15 @@ public abstract class AbstractRestController<ID, Pojo, Entity> {
protected final JpaRepository<Entity, ID> repository;
protected final EntityConverter<Entity, Pojo> converter;

protected List<Pojo> get() {
return repository.findAll()


protected List<Pojo> get(int page, int size, String sortBy, String sortDir) {
Sort sort = sortDir.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortBy).ascending()
: Sort.by(sortBy).descending();
return repository.findAll(PageRequest.of(page, size, sort))
.getContent()
.stream()
.map(converter::fromEntity)
.map(converter :: fromEntity)
.collect(Collectors.toList());
}

Expand Down
17 changes: 14 additions & 3 deletions src/main/java/ru/investbook/api/EventCashFlowRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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 ru.investbook.converter.EntityConverter;
import ru.investbook.entity.EventCashFlowEntity;
Expand All @@ -51,11 +52,21 @@ public EventCashFlowRestController(JpaRepository<EventCashFlowEntity, Integer> r
super(repository, converter);
}

@Override
@GetMapping
@Operation(summary = "Отобразить все", description = "Отображает все выплаты по всем счетам")
public List<EventCashFlow> get() {
return super.get();
public List<EventCashFlow> get(@RequestParam(value = "page", defaultValue = ApiUtil.DEFAULT_PAGE, required = false)
@Parameter(description = "номер страницы")
int pageNo,
@RequestParam(value = "size", defaultValue = ApiUtil.DEFAULT_PAGE_SIZE, required = false)
@Parameter(description = "количество записей на странице")
int pageSize,
@RequestParam(value = "sortBy", defaultValue = ApiUtil.DEFAULT_EVENT_CASH_FLOW_SORT_BY, required = false)
@Parameter(description = "атрибут сортировки")
String sortBy,
@RequestParam(value = "sortDir", defaultValue = ApiUtil.DEFAULT_SORT_DIRECTION, required = false)
@Parameter(description = "направление сортировки")
String sortDir) {
return super.get(pageNo, pageSize, sortBy, sortDir);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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 ru.investbook.converter.ForeignExchangeRateConverter;
import ru.investbook.entity.ForeignExchangeRateEntity;
Expand Down Expand Up @@ -60,11 +61,21 @@ public ForeignExchangeRateRestController(ForeignExchangeRateRepository repositor
this.foreignExchangeRateService = foreignExchangeRateService;
}

@Override
@GetMapping
@Operation(summary = "Отобразить все", description = "Отображает все загруженные в БД информацию по обменным курсам")
protected List<ForeignExchangeRate> get() {
return super.get();
protected List<ForeignExchangeRate> get(@RequestParam(value = "page", defaultValue = ApiUtil.DEFAULT_PAGE, required = false)
@Parameter(description = "номер страницы")
int pageNo,
@RequestParam(value = "size", defaultValue = ApiUtil.DEFAULT_PAGE_SIZE, required = false)
@Parameter(description = "количество записей на странице")
int pageSize,
@RequestParam(value = "sortBy", defaultValue = ApiUtil.DEFAULT_FOREIGN_EXCHANGE_RATE_SORT_BY, required = false)
@Parameter(description = "атрибут сортировки")
String sortBy,
@RequestParam(value = "sortDir", defaultValue = ApiUtil.DEFAULT_SORT_DIRECTION, required = false)
@Parameter(description = "направление сортировки")
String sortDir) {
return super.get(pageNo, pageSize, sortBy, sortDir);
}

@GetMapping("/currency-pairs/{currency-pair}")
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/ru/investbook/api/IssuerRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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 ru.investbook.converter.EntityConverter;
import ru.investbook.entity.IssuerEntity;
Expand All @@ -48,11 +49,21 @@ public IssuerRestController(JpaRepository<IssuerEntity, Integer> repository, Ent
super(repository, converter);
}

@Override
@GetMapping
@Operation(summary = "Отобразить всех")
public List<Issuer> get() {
return super.get();
public List<Issuer> get(@RequestParam(value = "page", defaultValue = ApiUtil.DEFAULT_PAGE, required = false)
@Parameter(description = "номер страницы")
int pageNo,
@RequestParam(value = "size", defaultValue = ApiUtil.DEFAULT_PAGE_SIZE, required = false)
@Parameter(description = "количество записей на странице")
int pageSize,
@RequestParam(value = "sortBy", defaultValue = ApiUtil.DEFAULT_ISSUER_SORT_BY, required = false)
@Parameter(description = "атрибут сортировки")
String sortBy,
@RequestParam(value = "sortDir", defaultValue = ApiUtil.DEFAULT_SORT_DIRECTION, required = false)
@Parameter(description = "направление сортировки")
String sortDir) {
return super.get(pageNo, pageSize, sortBy, sortDir);
}

@Override
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/ru/investbook/api/PortfolioCashRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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 ru.investbook.converter.EntityConverter;
import ru.investbook.entity.PortfolioCashEntity;
Expand All @@ -49,11 +50,21 @@ public PortfolioCashRestController(JpaRepository<PortfolioCashEntity, Integer> r
super(repository, converter);
}

@Override
@GetMapping
@Operation(summary = "Отобразить все", description = "Отображает всю имеющуюся информацию обо всех счетах")
public List<PortfolioCash> get() {
return super.get();
public List<PortfolioCash> get(@RequestParam(value = "page", defaultValue = ApiUtil.DEFAULT_PAGE, required = false)
@Parameter(description = "номер страницы")
int pageNo,
@RequestParam(value = "size", defaultValue = ApiUtil.DEFAULT_PAGE_SIZE, required = false)
@Parameter(description = "количество записей на странице")
int pageSize,
@RequestParam(value = "sortBy", defaultValue = ApiUtil.DEFAULT_PORTFOLIO_CASH_SORT_BY, required = false)
@Parameter(description = "атрибут сортировки")
String sortBy,
@RequestParam(value = "sortDir", defaultValue = ApiUtil.DEFAULT_SORT_DIRECTION, required = false)
@Parameter(description = "направление сортировки")
String sortDir) {
return super.get(pageNo, pageSize, sortBy, sortDir);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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 ru.investbook.converter.EntityConverter;
import ru.investbook.entity.PortfolioPropertyEntity;
Expand All @@ -49,11 +50,21 @@ public PortfolioPropertyRestController(JpaRepository<PortfolioPropertyEntity, In
super(repository, converter);
}

@Override
@GetMapping
@Operation(summary = "Отобразить все", description = "Отображает всю имеющуюся информацию обо всех счетах")
public List<PortfolioProperty> get() {
return super.get();
public List<PortfolioProperty> get(@RequestParam(value = "page", defaultValue = ApiUtil.DEFAULT_PAGE, required = false)
@Parameter(description = "номер страницы")
int pageNo,
@RequestParam(value = "size", defaultValue = ApiUtil.DEFAULT_PAGE_SIZE, required = false)
@Parameter(description = "количество записей на странице")
int pageSize,
@RequestParam(value = "sortBy", defaultValue = ApiUtil.DEFAULT_PORTFOLIO_PROPERTY_SORT_BY, required = false)
@Parameter(description = "атрибут сортировки")
String sortBy,
@RequestParam(value = "sortDir", defaultValue = ApiUtil.DEFAULT_SORT_DIRECTION, required = false)
@Parameter(description = "направление сортировки")
String sortDir) {
return super.get(pageNo, pageSize, sortBy, sortDir);
}

@Override
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/ru/investbook/api/PortfolioRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.spacious_team.broker.pojo.Portfolio;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -31,6 +33,7 @@
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 ru.investbook.converter.PortfolioConverter;
import ru.investbook.entity.PortfolioEntity;
Expand All @@ -50,11 +53,21 @@ public PortfolioRestController(PortfolioRepository repository, PortfolioConverte
this.repository = repository;
}

@Override
@GetMapping
@GetMapping()
@Operation(summary = "Отобразить все")
public List<Portfolio> get() {
return super.get();
public List<Portfolio> get(@RequestParam(value = "page", defaultValue = ApiUtil.DEFAULT_PAGE, required = false)
@Parameter(description = "номер страницы")
int pageNo,
@RequestParam(value = "size", defaultValue = ApiUtil.DEFAULT_PAGE_SIZE, required = false)
@Parameter(description = "количество записей на странице")
int pageSize,
@RequestParam(value = "sortBy", defaultValue = ApiUtil.DEFAULT_PORTFOLIO_SORT_BY, required = false)
@Parameter(description = "атрибут сортировки")
String sortBy,
@RequestParam(value = "sortDir", defaultValue = ApiUtil.DEFAULT_SORT_DIRECTION, required = false)
@Parameter(description = "направление сортировки")
String sortDir) {
return super.get(pageNo, pageSize, sortBy, sortDir);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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 ru.investbook.converter.SecurityDescriptionConverter;
import ru.investbook.entity.SecurityDescriptionEntity;
Expand All @@ -50,11 +51,21 @@ public SecurityDescriptionRestController(SecurityDescriptionRepository repositor
this.repository = repository;
}

@Override
@GetMapping
@Operation(summary = "Отобразить все", description = "Отобразить информацию по всем инструментам")
public List<SecurityDescription> get() {
return super.get();
public List<SecurityDescription> get(@RequestParam(value = "page", defaultValue = ApiUtil.DEFAULT_PAGE, required = false)
@Parameter(description = "номер страницы")
int pageNo,
@RequestParam(value = "size", defaultValue = ApiUtil.DEFAULT_PAGE_SIZE, required = false)
@Parameter(description = "количество записей на странице")
int pageSize,
@RequestParam(value = "sortBy", defaultValue = ApiUtil.DEFAULT_SECURITY_DESCRIPTION_SORT_BY, required = false)
@Parameter(description = "атрибут сортировки")
String sortBy,
@RequestParam(value = "sortDir", defaultValue = ApiUtil.DEFAULT_SORT_DIRECTION, required = false)
@Parameter(description = "направление сортировки")
String sortDir) {
return super.get(pageNo, pageSize, sortBy, sortDir);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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 ru.investbook.converter.EntityConverter;
import ru.investbook.entity.SecurityEventCashFlowEntity;
Expand All @@ -57,11 +58,21 @@ public SecurityEventCashFlowRestController(JpaRepository<SecurityEventCashFlowEn
this.positionsFactory = positionsFactory;
}

@Override
@GetMapping
@Operation(summary = "Отобразить все", description = "Отображает все выплаты по всем счетам")
public List<SecurityEventCashFlow> get() {
return super.get();
public List<SecurityEventCashFlow> get(@RequestParam(value = "page", defaultValue = ApiUtil.DEFAULT_PAGE, required = false)
@Parameter(description = "номер страницы")
int pageNo,
@RequestParam(value = "size", defaultValue = ApiUtil.DEFAULT_PAGE_SIZE, required = false)
@Parameter(description = "количество записей на странице")
int pageSize,
@RequestParam(value = "sortBy", defaultValue = ApiUtil.DEFAULT_SECURITY_EVENT_CASH_FLOW_SORT_BY, required = false)
@Parameter(description = "атрибут сортировки")
String sortBy,
@RequestParam(value = "sortDir", defaultValue = ApiUtil.DEFAULT_SORT_DIRECTION, required = false)
@Parameter(description = "направление сортировки")
String sortDir) {
return super.get(pageNo, pageSize, sortBy, sortDir);
}

@Override
Expand Down
Loading

0 comments on commit 8b62076

Please sign in to comment.