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()); | // app.use(express.urlencoded()); | ||||||
|  |  | ||||||
| router.get('/products', productsController.allProducts) | router.get('/products', productsController.allProducts) | ||||||
|  | router.get('/product/schema', productsController.getProductSchema) | ||||||
| router.get('/product/:slug', productsController.productBySlug) | router.get('/product/:slug', productsController.productBySlug) | ||||||
| router.post('/product', productsController.addNewProduct) | router.post('/product', productsController.addNewProduct) | ||||||
|  |  | ||||||
|  | router.get('/variation/schema', variationsController.getVariationSchema) | ||||||
| router.post('/variation/:id', variationsController.addNewVariationToProduct); | router.post('/variation/:id', variationsController.addNewVariationToProduct); | ||||||
|  |  | ||||||
| router.post('/applepay/validateSession', applePayController.validateSession) | router.post('/applepay/validateSession', applePayController.validateSession) | ||||||
|   | |||||||
| @@ -46,8 +46,14 @@ const addNewProduct = (req, res) => { | |||||||
|     .then(resp => res.send(resp)) |     .then(resp => res.send(resp)) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | const getProductSchema = (req, res) => { | ||||||
|  |   return products.productSchema() | ||||||
|  |     .then(schema => res.json(schema)) | ||||||
|  | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   allProducts, |   allProducts, | ||||||
|   productBySlug, |   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 = require('src/products'); | ||||||
| const products = new Products(); | const products = new Products(); | ||||||
|  |  | ||||||
| @@ -35,6 +35,13 @@ const addNewVariationToProduct = async (req, res) => { | |||||||
|     .catch(err => handleError(err, res)) |     .catch(err => handleError(err, res)) | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | const getVariationSchema = (req, res) => { | ||||||
|  |   return variationSchema() | ||||||
|  |     .then(schema => res.json(schema)) | ||||||
|  | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   addNewVariationToProduct |   addNewVariationToProduct, | ||||||
|  |   getVariationSchema | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -1,5 +1,6 @@ | |||||||
| const Product = require('schemas/Product'); | const Product = require('schemas/Product'); | ||||||
| const { slugify } = require('src/utils'); | const Variation = require('schemas/Variation'); | ||||||
|  | const { slugify, nulledSchema } = require('src/utils'); | ||||||
|  |  | ||||||
| class Products { | class Products { | ||||||
|   constructor() { |   constructor() { | ||||||
| @@ -29,6 +30,13 @@ class Products { | |||||||
|     return Product.find().populate('variations'); |     return Product.find().populate('variations'); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   productSchema() { | ||||||
|  |     const product = { ...Product.schema.obj }; | ||||||
|  |     const productSchema = nulledSchema(product); | ||||||
|  |  | ||||||
|  |     return Promise.resolve(productSchema); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   getBySlug(slug) { |   getBySlug(slug) { | ||||||
|     return Product.findOne({ urlSlug: slug }).populate('variations'); |     return Product.findOne({ urlSlug: slug }).populate('variations'); | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -14,6 +14,12 @@ const slugify = (string) => { | |||||||
|     .replace(/-+$/, '') // Trim - from end of text |     .replace(/-+$/, '') // Trim - from end of text | ||||||
| } | } | ||||||
|  |  | ||||||
|  | const nulledSchema = schema => Object.keys(schema).reduce((accumulator, current) => { | ||||||
|  |   accumulator[current] = ""; | ||||||
|  |   return accumulator; | ||||||
|  | }, {}); | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   slugify |   slugify, | ||||||
|  |   nulledSchema | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,4 +1,5 @@ | |||||||
| const Variation = require('schemas/Variation'); | const Variation = require('schemas/Variation'); | ||||||
|  | const { nulledSchema } = require('src/utils'); | ||||||
|  |  | ||||||
| const updateVariation = () => { | const updateVariation = () => { | ||||||
|   return |   return | ||||||
| @@ -17,6 +18,15 @@ const saveNewVariation = async (variation) => { | |||||||
|   return newVariation; |   return newVariation; | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | const variationSchema = () => { | ||||||
|   saveNewVariation |   const variation = { ...Variation.schema.obj }; | ||||||
|  |   const variationSchema = nulledSchema(variation); | ||||||
|  |  | ||||||
|  |   return Promise.resolve(variationSchema); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | module.exports = { | ||||||
|  |   saveNewVariation, | ||||||
|  |   variationSchema | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user