Fixed order-change
This commit is contained in:
parent
d996bf679d
commit
f4f01fe8c0
@ -1,6 +1,6 @@
|
|||||||
import { HttpService } from "@nestjs/axios";
|
import { HttpService } from "@nestjs/axios";
|
||||||
import { CACHE_MANAGER, CallHandler, ExecutionContext, Inject, Injectable, NestInterceptor } from "@nestjs/common";
|
import { CACHE_MANAGER, CallHandler, ExecutionContext, Inject, Injectable, NestInterceptor } from "@nestjs/common";
|
||||||
import { Observable, map, take, switchMap } from "rxjs";
|
import { Observable, map, take, switchMap, of } from "rxjs";
|
||||||
import { PageDto } from "../domain/dtos";
|
import { PageDto } from "../domain/dtos";
|
||||||
import { EsQueryDto } from "../domain/dtos/elastic/es-query.dto";
|
import { EsQueryDto } from "../domain/dtos/elastic/es-query.dto";
|
||||||
import { RequestDto } from "../domain/dtos/request.dto";
|
import { RequestDto } from "../domain/dtos/request.dto";
|
||||||
@ -50,6 +50,11 @@ export class PageInterceptor implements NestInterceptor {
|
|||||||
const limit = !query.limit ? 10 : query.limit;
|
const limit = !query.limit ? 10 : query.limit;
|
||||||
const order = !query.order ? Order.DESC : query.order;
|
const order = !query.order ? Order.DESC : query.order;
|
||||||
|
|
||||||
|
const prev_page = await this.cacheManager.get('prev_page');
|
||||||
|
if (prev_page) {
|
||||||
|
if (offset == prev_page[1] && limit == prev_page[2] && order == prev_page[3]) return of(prev_page[0]);
|
||||||
|
}
|
||||||
|
|
||||||
// Contruct a body for querying Elasticsearch
|
// Contruct a body for querying Elasticsearch
|
||||||
request.es_query = new EsQueryDto();
|
request.es_query = new EsQueryDto();
|
||||||
request.es_query.query = {
|
request.es_query.query = {
|
||||||
@ -60,14 +65,6 @@ export class PageInterceptor implements NestInterceptor {
|
|||||||
};
|
};
|
||||||
request.es_query.from = offset;
|
request.es_query.from = offset;
|
||||||
request.es_query.size = limit;
|
request.es_query.size = limit;
|
||||||
request.es_query.sort = [
|
|
||||||
{ "_score": { "order": order } },
|
|
||||||
];
|
|
||||||
|
|
||||||
const prev_page = await this.cacheManager.get('prev_page');
|
|
||||||
if (prev_page) {
|
|
||||||
if (offset == prev_page[1] && limit == prev_page[2]) return prev_page[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
return next.handle().pipe(
|
return next.handle().pipe(
|
||||||
switchMap(async (res) => {
|
switchMap(async (res) => {
|
||||||
@ -81,10 +78,12 @@ export class PageInterceptor implements NestInterceptor {
|
|||||||
let data = res.hits.hits;
|
let data = res.hits.hits;
|
||||||
// Omitting the redundant info and leaving only the document
|
// Omitting the redundant info and leaving only the document
|
||||||
data = data.map((el) => el._source);
|
data = data.map((el) => el._source);
|
||||||
|
// Change the order if set
|
||||||
|
if (order == Order.ASC) data.reverse();
|
||||||
|
|
||||||
// Cache and return the page
|
// Cache and return the page
|
||||||
const page: PageDto = new PageDto(data, meta);
|
const page: PageDto = new PageDto(data, meta);
|
||||||
await this.cacheManager.set('prev_page', [page, offset, limit]);
|
await this.cacheManager.set('prev_page', [page, offset, limit, order]);
|
||||||
return page;
|
return page;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user