148 - Product category mutations

This commit is contained in:
Anders Gustafsson
2021-09-16 11:04:23 +02:00
parent c4915cb15f
commit 2a2281ab5d
3 changed files with 51 additions and 0 deletions
+28
View File
@@ -536,4 +536,32 @@ export class ProductAPI extends BaseSQLDataSource {
promises.push(query);
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);
}
}