Added categories to products
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
|||||||
ProductGroup,
|
ProductGroup,
|
||||||
PrintProduct,
|
PrintProduct,
|
||||||
ProductBlacklist,
|
ProductBlacklist,
|
||||||
|
Category,
|
||||||
} from './product-types';
|
} from './product-types';
|
||||||
import { Maybe } from './types';
|
import { Maybe } from './types';
|
||||||
|
|
||||||
@@ -15,6 +16,24 @@ export class ProductAPI extends SQLDataSource {
|
|||||||
super(config);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
getBlacklisting(row: any): Array<ProductBlacklist> {
|
getBlacklisting(row: any): Array<ProductBlacklist> {
|
||||||
if (!row.blacklisting) {
|
if (!row.blacklisting) {
|
||||||
return [];
|
return [];
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ export enum ProductGroup {
|
|||||||
FRAMED_PRINT = 8,
|
FRAMED_PRINT = 8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Category {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
export interface ProductBlacklist {
|
export interface ProductBlacklist {
|
||||||
id: number;
|
id: number;
|
||||||
groupId: number;
|
groupId: number;
|
||||||
|
|||||||
@@ -7,6 +7,12 @@ const ProductBlacklist: IResolverObject = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const Product: IResolverObject = {
|
||||||
|
async categories({ id }, args, { dataSources }) {
|
||||||
|
return dataSources.productApi.getCategories(id);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
async function getProducts(_, { limit, visible, browsable }, { dataSources }) {
|
async function getProducts(_, { limit, visible, browsable }, { dataSources }) {
|
||||||
return dataSources.productApi.getProducts(limit, visible, browsable);
|
return dataSources.productApi.getProducts(limit, visible, browsable);
|
||||||
}
|
}
|
||||||
@@ -17,6 +23,7 @@ async function getProduct(_, { id }, { dataSources }) {
|
|||||||
|
|
||||||
export const productTypeDefs = {
|
export const productTypeDefs = {
|
||||||
ProductBlacklist,
|
ProductBlacklist,
|
||||||
|
Product,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const productQueryTypeDefs = {
|
export const productQueryTypeDefs = {
|
||||||
|
|||||||
+1
-24
@@ -124,6 +124,7 @@ const typeDefs = gql`
|
|||||||
updated: DateTime
|
updated: DateTime
|
||||||
printProducts: [PrintProduct]
|
printProducts: [PrintProduct]
|
||||||
blacklisting: [ProductBlacklist]
|
blacklisting: [ProductBlacklist]
|
||||||
|
categories: [Category]
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ProductGroup {
|
enum ProductGroup {
|
||||||
@@ -157,42 +158,18 @@ const typeDefs = gql`
|
|||||||
|
|
||||||
export { typeDefs };
|
export { typeDefs };
|
||||||
/**
|
/**
|
||||||
|
|
||||||
|
|
||||||
type Category {
|
|
||||||
id: ID!
|
|
||||||
name: String
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
type Designer {
|
type Designer {
|
||||||
id: ID!
|
id: ID!
|
||||||
name: String
|
name: String
|
||||||
orderRows(fromDate: Date, toDate: Date): [OrderRow]
|
orderRows(fromDate: Date, toDate: Date): [OrderRow]
|
||||||
}
|
}
|
||||||
|
|
||||||
type PrintProduct {
|
|
||||||
insertedDate: DateTime!
|
|
||||||
updatedDate: DateTime
|
|
||||||
}
|
|
||||||
|
|
||||||
type Product {
|
type Product {
|
||||||
categories: [Category]
|
|
||||||
designer: Designer
|
designer: Designer
|
||||||
insertedDate: DateTime!
|
|
||||||
updatedDate: DateTime
|
|
||||||
}
|
|
||||||
|
|
||||||
type ProductBlacklist {
|
|
||||||
market: Market
|
|
||||||
insertedDate: DateTime!
|
|
||||||
updatedDate: DateTime
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Query {
|
type Query {
|
||||||
product(id: Int!): Product
|
|
||||||
designer(id: Int!): Designer
|
designer(id: Int!): Designer
|
||||||
designers: [Designer]
|
designers: [Designer]
|
||||||
}
|
}
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user