Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merch store db tests #42

Merged
merged 8 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/backend/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import featurePermissions from './feature-permissions';
import newsletterRecipients from './newsletter-recipient';
import blogs from './blogs';
import geeseFeatures from './geese-features';
import products from './products'

export default {
users: {
Expand Down Expand Up @@ -42,4 +43,7 @@ export default {
geeseFeatures: {
...geeseFeatures(db),
},
products: {
...products(db),
}
};
143 changes: 143 additions & 0 deletions src/backend/db/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import {
fromProductVariation,
toProductVariation,
} from '../models/products';

const getProducts = (db) => () =>
db('merch_products')
.then((res) => {
return res;
})
.catch((err) => {
console.error(`Error in getProducts: ${err}`);
throw err;
});

const getProductVariations = (db) => () =>
db('merch_product_variations')
.then((res) => {
return res.map(toProductVariation);
})
.catch((err) => {
console.error(`Error in getProductVariations: ${err}`);
throw err;
});

const getProductById = (db) => (id) =>
db('merch_products')
.where({ id })
.then((res) => {
return res;
})
.catch((err) => {
console.error(`Error in getProductById: ${err}`);
throw err;
});

const getProductVariationsById = (db) => (variationId) =>
db('merch_product_variations')
.where({ variationId })
.then((res) => {
return res.map(toProductVariation);
})
.catch((err) => {
console.error(`Error in getProductVariationsById: ${err}`);
throw err;
});

const addProduct = (db) => (productInfo) =>
db('merch_products')
.insert(productInfo)
.returning('id')
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in addProduct: ${err}`);
throw err;
});

const addProductVariation = (db) => (productVariationInfo) =>
db('merch_product_variations')
.insert(fromProductVariation(productVariationInfo))
.returning('id')
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in addProductVariation: ${err}`);
throw err;
});

const updateProductById = (db) => (id, productInfo) =>
db('merch_products')
.where({
id,
})
.update({
productInfo,
})
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in updateProductById: ${err}`);
throw err;
});

const updateProductVariationById = (db) => (id, productVariationInfo) =>
db('merch_product_variations')
.where({
id,
})
.update({
...fromProductVariation(productVariationInfo),
})
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in updateProductVariationById: ${err}`);
throw err;
});

const deleteProductById = (db) => (id) =>
db('merch_products')
.where({
id,
})
.del()
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in deleteProductById: ${err}`);
throw err;
});

const deleteProductVariationById = (db) => (id) =>
db('merch_product_variations')
.where({
id,
})
.del()
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in deleteProductVariationsById: ${err}`);
throw err;
});

export default (db) => ({
getProducts: getProducts(db),
getProductVariations: getProductVariations(db),
getProductById: getProductById(db),
getProductVariationsById: getProductVariationsById(db),
addProduct: addProduct(db),
addProductVariation: addProductVariation(db),
updateProductById: updateProductById(db),
updateProductVariationById: updateProductVariationById(db),
deleteProductById: deleteProductById(db),
deleteProductVariationById: deleteProductVariationById(db),
});
21 changes: 21 additions & 0 deletions src/backend/models/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { parseTimeForResponse } from '../utils/db-dates';
import renameProps from '../utils/rename-props';


export const fromProductVariation = (productVariation) => ({
...renameProps(productVariation, {
variationName: 'variation_name',
productId: 'product_id',
lastUpdated: 'last_updated',
}),
last_updated: parseTimeForResponse(productVariation.lastUpdated),
});

export const toProductVariation = (productVariation) => ({
...renameProps(productVariation, {
variation_name: 'variationName',
product_id: 'productId',
last_updated: 'lastUpdated',
}),
lastUpdated: parseTimeForResponse(productVariation.last_updated),
});
Loading