Skip to content

Commit

Permalink
Added basic Product model
Browse files Browse the repository at this point in the history
  • Loading branch information
rituparna-ui committed Jul 19, 2022
1 parent e6440b6 commit cb7e8e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions controllers/products.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const products = [];
const Product = require('../models/product');

exports.getAddProduct = (req, res, next) => {
res.render('add-product', {
Expand All @@ -11,11 +11,13 @@ exports.getAddProduct = (req, res, next) => {
};

exports.postAddProduct = (req, res, next) => {
products.push({ title: req.body.title });
const product = new Product(req.body.title);
product.save();
res.redirect('/');
};

exports.getProducts = (req, res, next) => {
const products = Product.fetchAll();
res.render('shop', {
prods: products,
pageTitle: 'Shop',
Expand Down
15 changes: 15 additions & 0 deletions models/product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const products = [];

module.exports = class Product {
constructor(t) {
this.title = t;
}

save() {
products.push(this);
}

static fetchAll() {
return products;
}
}

0 comments on commit cb7e8e4

Please sign in to comment.