148 - Product category mutations
This commit is contained in:
@@ -536,4 +536,32 @@ export class ProductAPI extends BaseSQLDataSource {
|
|||||||
promises.push(query);
|
promises.push(query);
|
||||||
return Promise.all(promises);
|
return Promise.all(promises);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async addCategoriesToProduct(
|
||||||
|
productId,
|
||||||
|
categoryIds,
|
||||||
|
): Promise<Promise<void>[]> {
|
||||||
|
const promises = categoryIds.map((categoryId) =>
|
||||||
|
this.knex('product_category').insert({
|
||||||
|
product_id: productId,
|
||||||
|
category_id: categoryId,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return Promise.all(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeCategoriesFromProduct(
|
||||||
|
productId,
|
||||||
|
categoryIds,
|
||||||
|
): Promise<Promise<void>[]> {
|
||||||
|
const promises = categoryIds.map((categoryId) =>
|
||||||
|
this.knex('product_category')
|
||||||
|
.where({
|
||||||
|
product_id: productId,
|
||||||
|
category_id: categoryId,
|
||||||
|
})
|
||||||
|
.del(),
|
||||||
|
);
|
||||||
|
return Promise.all(promises);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -180,4 +180,24 @@ export const productMutationTypeDefs = {
|
|||||||
);
|
);
|
||||||
return (<ProductAPI>dataSources.productApi).getProduct(productId);
|
return (<ProductAPI>dataSources.productApi).getProduct(productId);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async addCategoriesToProduct(_, { productId, categoryIds }, { dataSources }) {
|
||||||
|
await (<ProductAPI>dataSources.productApi).addCategoriesToProduct(
|
||||||
|
productId,
|
||||||
|
categoryIds,
|
||||||
|
);
|
||||||
|
return (<ProductAPI>dataSources.productApi).getProduct(productId);
|
||||||
|
},
|
||||||
|
|
||||||
|
async removeCategoriesFromProduct(
|
||||||
|
_,
|
||||||
|
{ productId, categoryIds },
|
||||||
|
{ dataSources },
|
||||||
|
) {
|
||||||
|
await (<ProductAPI>dataSources.productApi).removeCategoriesFromProduct(
|
||||||
|
productId,
|
||||||
|
categoryIds,
|
||||||
|
);
|
||||||
|
return (<ProductAPI>dataSources.productApi).getProduct(productId);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -459,4 +459,7 @@ type Mutation {
|
|||||||
removeRelatedProducts(productId: Int!, relatedProductIds: [Int]!): Product
|
removeRelatedProducts(productId: Int!, relatedProductIds: [Int]!): Product
|
||||||
|
|
||||||
addKeyword(name: String!, type: KeywordType): Keyword
|
addKeyword(name: String!, type: KeywordType): Keyword
|
||||||
|
|
||||||
|
addCategoriesToProduct(productId: Int!, categoryIds: [Int]!): Product
|
||||||
|
removeCategoriesFromProduct(productId: Int!, categoryIds: [Int]!): Product
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user