P5 7619 refactor products to return list (#11)
This commit is contained in:
@@ -10,6 +10,8 @@ import {
|
||||
ProductWallpaperType,
|
||||
ProductFields,
|
||||
Orientation,
|
||||
ProductsFilter,
|
||||
ProductsFilterInput,
|
||||
} from '../types/product-types';
|
||||
import { Maybe } from '../types/types';
|
||||
import { convertToPossibleType } from './utils';
|
||||
@@ -67,6 +69,9 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
}
|
||||
|
||||
getProductFields(row: any): ProductFields {
|
||||
if (!row.fields) {
|
||||
return null; // 53460 for example
|
||||
}
|
||||
const fields = Object.keys(row.fields).reduce((mem, key) => {
|
||||
mem[camelcase(key)] = convertToPossibleType(row.fields[key]);
|
||||
return mem;
|
||||
@@ -76,7 +81,7 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
}
|
||||
|
||||
getOrientation(fields: ProductFields): string {
|
||||
if (!fields.width || !fields.height) {
|
||||
if (!fields || !fields.width || !fields.height) {
|
||||
return Orientation[Orientation.UNKNOWN];
|
||||
}
|
||||
if (fields.width > fields.height) {
|
||||
@@ -100,13 +105,17 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
return row;
|
||||
}
|
||||
|
||||
async getProducts(
|
||||
limit: Maybe<number>,
|
||||
visible: Maybe<boolean>,
|
||||
browsable: Maybe<boolean>,
|
||||
): Promise<Array<Product>> {
|
||||
limit = limit ?? 100;
|
||||
const query = sql.products(limit, visible, browsable);
|
||||
async getProductsTotal(input: ProductsFilterInput): Promise<number> {
|
||||
const query = sql.productsTotal(input);
|
||||
const res = await this.cachedRaw(query)
|
||||
.cache(MINUTE * 60)
|
||||
.then((data) => data.rows);
|
||||
return convertToPossibleType(res[0]['count']); // Optimize later to return Promise
|
||||
}
|
||||
|
||||
async getProducts(input: ProductsFilterInput): Promise<Array<Product>> {
|
||||
// TODO: handle offset
|
||||
const query = sql.products(input);
|
||||
|
||||
return this.knex
|
||||
.raw(query)
|
||||
|
||||
Reference in New Issue
Block a user