Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nimay-gupta committed Apr 8, 2021
2 parents 1a6fd57 + ecefcbb commit 7921447
Show file tree
Hide file tree
Showing 21 changed files with 2,591 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Lab5-Nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Online store web app using node.js express framework and postgresql database

* routing get and post requests to controller
* querying database in model (Standard MVC arch)

## Requirements
1. Node.js
2. node modules installation through npm
22 changes: 22 additions & 0 deletions Lab5-Nodejs/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');

const adminRo = require('./routes/admin');
const nonadminRo = require('./routes/nonadmin');
const pool = require('./utils/database');


const app = express();
app.set('view engine', 'ejs');
app.set('views', 'views');
app.use(bodyParser.urlencoded({ extended:true}));
app.use(express.static(path.join(__dirname,'public')));


app.use('/admin',adminRo);
app.use('/',nonadminRo);


app.listen(3000);
127 changes: 127 additions & 0 deletions Lab5-Nodejs/controllers/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const Prod = require('../models/prod');


exports.get_test = (req,res,next) => {


res.render('admin/add_product', {
pageTitle: 'Add Product',
path: '/admin/add-product',
editing: false
});


};

exports.post_test = (req,res,next) => {
const title = req.body.title;
const image = req.body.image;
const price = req.body.price;
const quantity = req.body.quantity;
const product = new Prod( title, image, price,quantity);
product
.add_prod()
.then(() => {
res.redirect('/admin/add-product');
})
.catch(err => console.log(err));
};


exports.get_prods_test = (req,res,next) => {

const a = Prod.get_all();
a.then(value => {res.render('prods', {pageTitle: 'Products', path: '/prods', editing: false, articles:value.rows});});



};

exports.post_prods_test = (req,res,next) => {
// .catch(err => console.log(err));
};




exports.get_cart_test = (req,res,next) => {

const a = Prod.get_all_cart();
var x;
const b = Prod.get_credit();

a.then(value => {x = value; return Prod.get_credit();})
.then(val => {res.render('cart', {pageTitle: 'Cart', path: '/cart', editing: false, articles:x.rows, count: x.rowCount, cred: val.rows[0].credit });});


};

exports.post_cart_test = (req,res,next) => {

const product_id = req.body.product_id;


const nice = Prod.get_with_prod_id(product_id);
nice.then(val => {if(val.rowCount != 0)
{return Prod.get_in_cart_id(product_id).then(value => {Prod.update_in_prod(product_id); if(value.rowCount != 0) {return Prod.update_in_cart(product_id)} else return Prod.insert_in_cart(product_id)})
.then(() => {return res.redirect('/cart')}); }
else {return res.redirect('/prods')}});

};



exports.get_orders_test = (req,res,next) => {

const ord = Prod.get_all_orders();
ord.then(value => {res.render('orders', {pageTitle: 'Orders', path: '/orders', editing: false, articles:value.rows});});



};

exports.post_orders_test = (req,res,next) => {

const cred = Prod.remcred();
var x,y;
var pur;
var pro_list = [];
var res_list = [];
var id_list = [];
var newlist = [];

cred.then(val => { x = val.rows[0].credit; return Prod.sum_price_cart()})
.then(value => {if(x < value.rows[0].sum) {return res.redirect('/cart') }
else {pur = value.rows[0].sum; return Prod.get_full_cart().then(ui => {y = ui.rowCount;
// console.log(y);
for (i = 0; i < y; i++) {
var id = ui.rows[i].item_id;
id_list.push(id);
pro_list.push(Prod.present_in_orders(id))} return Promise.all(pro_list)} )
.then(chec => {
for (i = 0; i < chec.length; i++) {

if(chec[i].rowCount == 0)
{// console.log("id"+id_list[i]);
newlist.push(Prod.insert_order(id_list[i]));
}
else
{ // console.log("id"+id_list[i]);
newlist.push(Prod.update_order(id_list[i]));
}
}

return Promise.all(newlist);

})
.then(()=>{return Prod.empty_cart()})
.then(() => {return Prod.update_credit(pur)})
.then(()=>{res.redirect('/orders')});}})





};


119 changes: 119 additions & 0 deletions Lab5-Nodejs/models/prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@

const pool= require('../utils/database');
module.exports = class Prod{

constructor( title, image, price, quantity){
this.title = title;
this.image = image;
this.price = price;
this.quantity = quantity;
}

add_prod(){
return pool.query('INSERT INTO products(title, price, image, quantity) VALUES ($1, $2, $3, $4);', [this.title, this.price, this.image, this.quantity]);
}

/* add_prod_to_cart(){
return pool.query('INSERT INTO cart(user_id, item_id, quantity) VALUES ($1, $2, $3);', [this.userid, this.itemid, this.quantity]);
}
*/
static get_all_cart()
{
return pool.query('SELECT u.credit,p.title,p.price,c.quantity,p.image from users u,products p,cart c where u.user_id = c.user_id and p.id = c.item_id');
}

static get_with_prod_id(prod_id)
{
const f = pool.query('SELECT * from products p where p.id = $1 and p.quantity > 0;',[prod_id]);
return f;
//f.then(value => {console.log(value.rowCount)});
//return f.then(value => {if(value.rowCount == 0) return false; else return true; });
}
static get_in_cart_id(prod_id)
{ //f.then(value => {console.log(value.rowCount)});
return pool.query('SELECT * from cart c where c.item_id = $1;',[prod_id]);
//return f.then(value => {if(value.rowCount == 0) return false; else return true; });

}

static update_in_cart(prod_id)
{
return pool.query('UPDATE cart c set quantity = quantity + 1 where c.item_id = $1;',[prod_id]);
}

static update_in_prod(prod_id)
{
return pool.query('UPDATE products p set quantity = quantity-1 where p.id = $1;',[prod_id]);
}

static insert_in_cart(prod_id)
{
return pool.query('INSERT INTO cart(user_id,item_id,quantity) VALUES ($1,$2,$3);', [1,prod_id,1]);
}

static get_all_orders(prod_id)
{
return pool.query('SELECT u.credit,p.title,p.price,o.quantity,p.image from users u,products p,orders o where u.user_id = o.user_id and p.id = o.item_id');
}

static remcred()
{
return pool.query('SELECT credit from users');
}

static sum_price_cart()
{
return pool.query('SELECT sum(p.price*c.quantity) as sum from cart c, products p where p.id = c.item_id;')
}

static empty_cart()
{
return pool.query('DELETE FROM cart;')
}

static buy_all()
{
return pool.query('INSERT INTO orders SELECT * FROM CART ')
}

static update_credit(amount)
{
return pool.query('UPDATE users set credit = credit - $1;',[amount])
}

static get_full_cart()
{
return pool.query('SELECT * from cart;')
}

static get_full_cred(id)
{
return pool.query('SELECT * FROM CART WHERE item_id = id;')
}

static update_order(id)
{
return pool.query('update orders set quantity = orders.quantity+c.quantity from cart c where orders.item_id = $1 and orders.item_id = c.item_id',[id])
}

static insert_order(id)
{
return pool.query('insert into orders (select * from cart where item_id = $1)',[id])
}

static present_in_orders(id)
{
return pool.query('SELECT * FROM orders where item_id = $1;',[id])
}

static get_credit()
{
return pool.query('SELECT * FROM users;')
}

static get_all(){
return pool.query('SELECT * FROM products');

}

};
Loading

0 comments on commit 7921447

Please sign in to comment.