add api get scrape items
This commit is contained in:
parent
05d87c367e
commit
cd0de7331b
|
|
@ -1,16 +1,10 @@
|
||||||
|
import { WebBidsService } from '@/modules/bids/services/web-bids.service';
|
||||||
|
import AppResponse from '@/response/app-response';
|
||||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
import { paginate, PaginateQuery } from 'nestjs-paginate';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { ScrapItem } from '../entities/scrap-item.entity';
|
import { ScrapItem } from '../entities/scrap-item.entity';
|
||||||
import AppResponse from '@/response/app-response';
|
|
||||||
import {
|
|
||||||
FilterOperator,
|
|
||||||
FilterSuffix,
|
|
||||||
paginate,
|
|
||||||
PaginateQuery,
|
|
||||||
} from 'nestjs-paginate';
|
|
||||||
import { Column } from 'nestjs-paginate/lib/helper';
|
|
||||||
import { WebBidsService } from '@/modules/bids/services/web-bids.service';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ScrapItemsService {
|
export class ScrapItemsService {
|
||||||
|
|
@ -78,7 +72,7 @@ export class ScrapItemsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async index(query: PaginateQuery) {
|
async index(query: PaginateQuery) {
|
||||||
const data = await paginate(query, this.scrapItemRepo, {
|
const { data, meta } = await paginate(query, this.scrapItemRepo, {
|
||||||
sortableColumns: ['id'],
|
sortableColumns: ['id'],
|
||||||
searchableColumns: ['id'],
|
searchableColumns: ['id'],
|
||||||
defaultLimit: 15,
|
defaultLimit: 15,
|
||||||
|
|
@ -87,16 +81,26 @@ export class ScrapItemsService {
|
||||||
},
|
},
|
||||||
defaultSortBy: [['updated_at', 'DESC']],
|
defaultSortBy: [['updated_at', 'DESC']],
|
||||||
maxLimit: 100,
|
maxLimit: 100,
|
||||||
|
relations: { scrap_config: { web_bid: true } },
|
||||||
});
|
});
|
||||||
|
|
||||||
const sources = await this.webService.webBidRepo.find({
|
const sources = await this.webService.webBidRepo.find({
|
||||||
select: { origin_url: true },
|
select: { origin_url: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
return AppResponse.toPagination<ScrapItem>(
|
const newData = data.map(({ scrap_config, ...item }) => {
|
||||||
{ ...data, bonus: { sources } },
|
return {
|
||||||
true,
|
...item,
|
||||||
ScrapItem,
|
source: scrap_config.web_bid.origin_url,
|
||||||
);
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return AppResponse.toPaginationAny({
|
||||||
|
data: newData,
|
||||||
|
meta,
|
||||||
|
bonus: {
|
||||||
|
sources: sources.map((item) => item.origin_url),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,32 @@ export default class AppResponse {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static toPaginationAny(
|
||||||
|
{ data, meta, bonus }: any,
|
||||||
|
transform = true,
|
||||||
|
dtoClass?: new () => any,
|
||||||
|
) {
|
||||||
|
const transformedData =
|
||||||
|
transform && dtoClass
|
||||||
|
? data.map((item) => plainToClass(dtoClass, item))
|
||||||
|
: data;
|
||||||
|
|
||||||
|
return this.toResponse(transformedData, {
|
||||||
|
bonus: {
|
||||||
|
current_page: meta.currentPage,
|
||||||
|
from: meta.currentPage * meta.itemsPerPage - (meta.itemsPerPage - 1),
|
||||||
|
to:
|
||||||
|
meta.currentPage * meta.itemsPerPage > meta.totalItems
|
||||||
|
? meta.totalItems
|
||||||
|
: meta.currentPage * meta.itemsPerPage,
|
||||||
|
last_page: meta.totalPages,
|
||||||
|
per_page: meta.itemsPerPage,
|
||||||
|
total: meta.totalItems,
|
||||||
|
...bonus,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public static processFilters<T>(
|
public static processFilters<T>(
|
||||||
queryFilters: any,
|
queryFilters: any,
|
||||||
filterableColumns: {
|
filterableColumns: {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue