Major refactor

This commit is contained in:
Niklas Fondberg
2021-07-06 15:57:22 +02:00
parent 6bca11c62d
commit f66dbe33d7
21 changed files with 299 additions and 73 deletions
+17 -31
View File
@@ -5,9 +5,8 @@ import {
ProductGroup,
PrintProduct,
ProductBlacklist,
Category,
} from './product-types';
import { Maybe } from './types';
} from '../types/product-types';
import { Maybe } from '../types/types';
const MINUTE = 60;
@@ -16,35 +15,10 @@ export class ProductAPI extends SQLDataSource {
super(config);
}
async getCategories(productId: number): Promise<Array<Category>> {
const query = /* sql */ `
SELECT product_category.category_id, categories.name
FROM product_category JOIN categories ON product_category.category_id = categories.id
WHERE product_category.product_id = ?
`;
const res = await this.knex.raw(query, productId).then((data) =>
data.rows.map((o) => {
return {
...o,
id: o.category_id,
};
}),
);
return res;
}
async getMaterial(printId: number): Promise<any> {
// SELECT materialid, material, (price / 100::float) as price FROM "product-materials"
}
// TODO: get product details
async getProductDetails(id: number): Promise<any> {
// SELECT * FROM "product-fields";
// SELECT * FROM "product-products_fields" WHERE productid = 58941;
return null;
}
getBlacklisting(row: any): Array<ProductBlacklist> {
if (!row.blacklisting) {
return [];
@@ -70,6 +44,7 @@ export class ProductAPI extends SQLDataSource {
row.blacklisting = this.getBlacklisting(row);
row.printProducts = this.getPrintProducts(row);
row.designerId = row.designerid;
row.fields_json = row.fields;
return row;
}
@@ -89,9 +64,20 @@ export class ProductAPI extends SQLDataSource {
async getProduct(id: number): Promise<Maybe<Product>> {
const query = sql.product(id);
const res = await this.knex
.raw(query)
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
const res = await this.knex.raw(query).then((data) =>
data.rows.map((row) => {
const prod = this.createProductFromRow(row);
return prod;
}),
);
return res.find(Boolean);
}
async getCategoryProducts(categoryId: number): Promise<Array<Product>> {
const query = sql.categoryProducts(categoryId);
return await this.knex
.raw(query)
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
}
}