mirror of
				https://github.com/KevinMidboe/planetposen.git
				synced 2025-10-29 17:50:32 +00:00 
			
		
		
		
	Api endpoints for fetching shema for product and variation.
This commit is contained in:
		| @@ -21,9 +21,11 @@ app.use(express.json()); | ||||
| // app.use(express.urlencoded()); | ||||
|  | ||||
| router.get('/products', productsController.allProducts) | ||||
| router.get('/product/schema', productsController.getProductSchema) | ||||
| router.get('/product/:slug', productsController.productBySlug) | ||||
| router.post('/product', productsController.addNewProduct) | ||||
|  | ||||
| router.get('/variation/schema', variationsController.getVariationSchema) | ||||
| router.post('/variation/:id', variationsController.addNewVariationToProduct); | ||||
|  | ||||
| router.post('/applepay/validateSession', applePayController.validateSession) | ||||
|   | ||||
| @@ -46,8 +46,14 @@ const addNewProduct = (req, res) => { | ||||
|     .then(resp => res.send(resp)) | ||||
| } | ||||
|  | ||||
| const getProductSchema = (req, res) => { | ||||
|   return products.productSchema() | ||||
|     .then(schema => res.json(schema)) | ||||
| } | ||||
|  | ||||
| module.exports = { | ||||
|   allProducts, | ||||
|   productBySlug, | ||||
|   addNewProduct | ||||
|   addNewProduct, | ||||
|   getProductSchema | ||||
| }; | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| const { saveNewVariation } = require('src/variation.js') | ||||
| const { saveNewVariation, variationSchema } = require('src/variation.js') | ||||
| const Products = require('src/products'); | ||||
| const products = new Products(); | ||||
|  | ||||
| @@ -35,6 +35,13 @@ const addNewVariationToProduct = async (req, res) => { | ||||
|     .catch(err => handleError(err, res)) | ||||
| } | ||||
|  | ||||
|  | ||||
| const getVariationSchema = (req, res) => { | ||||
|   return variationSchema() | ||||
|     .then(schema => res.json(schema)) | ||||
| } | ||||
|  | ||||
| module.exports = { | ||||
|   addNewVariationToProduct | ||||
|   addNewVariationToProduct, | ||||
|   getVariationSchema | ||||
| }; | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| const Product = require('schemas/Product'); | ||||
| const { slugify } = require('src/utils'); | ||||
| const Variation = require('schemas/Variation'); | ||||
| const { slugify, nulledSchema } = require('src/utils'); | ||||
|  | ||||
| class Products { | ||||
|   constructor() { | ||||
| @@ -29,6 +30,13 @@ class Products { | ||||
|     return Product.find().populate('variations'); | ||||
|   } | ||||
|  | ||||
|   productSchema() { | ||||
|     const product = { ...Product.schema.obj }; | ||||
|     const productSchema = nulledSchema(product); | ||||
|  | ||||
|     return Promise.resolve(productSchema); | ||||
|   } | ||||
|  | ||||
|   getBySlug(slug) { | ||||
|     return Product.findOne({ urlSlug: slug }).populate('variations'); | ||||
|   } | ||||
|   | ||||
| @@ -14,6 +14,12 @@ const slugify = (string) => { | ||||
|     .replace(/-+$/, '') // Trim - from end of text | ||||
| } | ||||
|  | ||||
| const nulledSchema = schema => Object.keys(schema).reduce((accumulator, current) => { | ||||
|   accumulator[current] = ""; | ||||
|   return accumulator; | ||||
| }, {}); | ||||
|  | ||||
| module.exports = { | ||||
|   slugify | ||||
|   slugify, | ||||
|   nulledSchema | ||||
| } | ||||
|   | ||||
| @@ -1,4 +1,5 @@ | ||||
| const Variation = require('schemas/Variation'); | ||||
| const { nulledSchema } = require('src/utils'); | ||||
|  | ||||
| const updateVariation = () => { | ||||
|   return | ||||
| @@ -17,6 +18,15 @@ const saveNewVariation = async (variation) => { | ||||
|   return newVariation; | ||||
| } | ||||
|  | ||||
| module.exports = { | ||||
|   saveNewVariation | ||||
| const variationSchema = () => { | ||||
|   const variation = { ...Variation.schema.obj }; | ||||
|   const variationSchema = nulledSchema(variation); | ||||
|  | ||||
|   return Promise.resolve(variationSchema); | ||||
| } | ||||
|  | ||||
|  | ||||
| module.exports = { | ||||
|   saveNewVariation, | ||||
|   variationSchema | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user