2021-05-10 23:02:54 +03:00
|
|
|
import {paginatedResponse} from '../utils';
|
|
|
|
|
|
|
|
export default function mockProducts(server) {
|
2022-02-23 17:21:28 +03:00
|
|
|
server.post('/products/');
|
2021-05-10 23:02:54 +03:00
|
|
|
|
|
|
|
server.get('/products/', paginatedResponse('products'));
|
|
|
|
|
|
|
|
server.get('/products/:id/', function ({products}, {params}) {
|
|
|
|
let {id} = params;
|
|
|
|
let product = products.find(id);
|
|
|
|
|
|
|
|
return product || new Response(404, {}, {
|
|
|
|
errors: [{
|
|
|
|
type: 'NotFoundError',
|
|
|
|
message: 'Product not found.'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-02-19 01:36:01 +03:00
|
|
|
server.put('/products/:id/', function ({products, productBenefits}, {params}) {
|
|
|
|
const attrs = this.normalizedRequestAttrs();
|
|
|
|
const product = products.find(params.id);
|
|
|
|
|
|
|
|
const benefitAttrs = attrs.benefits;
|
|
|
|
delete attrs.benefits;
|
|
|
|
|
|
|
|
product.update(attrs);
|
|
|
|
|
|
|
|
benefitAttrs.forEach((benefit) => {
|
|
|
|
if (benefit.id) {
|
|
|
|
const productBenefit = productBenefits.find(benefit.id);
|
|
|
|
productBenefit.product = product;
|
|
|
|
productBenefit.save();
|
|
|
|
} else {
|
|
|
|
product.createProductBenefit(benefit);
|
|
|
|
product.save();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return product.save();
|
|
|
|
});
|
2021-05-10 23:02:54 +03:00
|
|
|
|
|
|
|
server.del('/products/:id/');
|
|
|
|
}
|