Git Product home page Git Product logo

ecommerce's Introduction

Ecommerce - CRUD

This is an ecommerce site that I made using Node.js (Express / EJS), MySQL database & used the four basic functions of persistent storage known as CRUD such as retrieve instead of read, modify instead of update, or destroy instead of delete.

CRUD is done like so:
// create product form
exports.createProductForm = function (req, res) {
    res.render('create-product', {'title': 'Create Product'});
}

// create
exports.createProduct = function (req, res) {
    db.query(`INSERT INTO products SET name = ?, description = ?, price = ?, image = ?`, [req.fields.name, req.fields.description, req.fields.price, req.fields.image], function(err, results) {
        if(err) {
            throw err;
        } else {
            console.log(results);
            req.flash('info', 'You have created a product!');
            res.redirect('/create-product');
        }
    })
}

// read
exports.showProducts = function(req, res) {
    db.query(`SELECT id, name, description, price, image FROM products`, function(err, results) {
        if(err) {
            throw err;
        } else {
            console.log(results);
            res.render('admin-products', { 'title': 'Products', results });
        }
    })
}

// get
exports.editProduct = function(req, res) {
    db.query(`SELECT name, description, price, image FROM products WHERE id = ?`, [req.params.id], function(err, results) {
        if(err) {
            throw err;
        } else {
            console.log(results);
            res.render('edit-product', { 'title': 'Edit', result: results[0] });
        }
    })
}

// update
exports.updateProduct = function (req, res) {
    db.query(`UPDATE products SET name = ?, description = ?, price = ?, image = ? WHERE id = ?`, [req.fields.name, req.fields.description, req.fields.price, req.fields.image, req.params.id], function(err, results) {
        if(err) {
            throw err;
        } else {
            console.log(results);
            req.flash('info', 'You have updated a product!');
            res.redirect('/admin/products');
        }
    })
}

// delete
exports.deleteProduct = function (req, res) {
    db.query(`DELETE FROM products WHERE id = ?`, [req.params.id], function(err, results) {
        if(err) {
            throw err;
        } else {
            console.log(results);
            req.flash('info', 'You have deleted a product!');
            res.redirect('/admin/products');
        }
    })
}

As you can see above the first block of code renders the form, and then the following crud functionalities happen below by using the HTTP methods GET and POST.

After the data is inserted into the form then it will turn out to look like this in the admin panel:

admin

Out in the products page it will look like this:

admin

ecommerce's People

Contributors

incognito95 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.