Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const express = require('express');
const {json} = require('body-parser');
const mongoose = require('mongoose');

const masterRoutes = require('./server/masterRoutes.js');

const port = 8080;
const app = express();

const mongoUri = `mongodb://localhost:27017/products`;
mongoose.connect(mongoUri);

app.use(express.static(__dirname + '/public'));
app.use(json());
////////////////

masterRoutes(app);

////////////////
app.listen(port, ()=>{
console.log(`Express running on ${port}`);
});

mongoose.connection.once(`open`, ()=>{
console.log(`Mongoose running on ${mongoUri}`);
});

25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "ecommerce",
"version": "1.0.0",
"description": "<img src=\"https://devmounta.in/img/logowhiteblue.png\" width=\"250\" align=\"right\">",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/npranto/ecommerce.git"
},
"author": "Nazmuz Shakib Pranto",
"license": "ISC",
"bugs": {
"url": "https://github.com/npranto/ecommerce/issues"
},
"homepage": "https://github.com/npranto/ecommerce#readme",
"dependencies": {
"body-parser": "^1.15.2",
"cors": "^2.7.1",
"express": "^4.14.0",
"mongoose": "^4.5.1"
}
}
62 changes: 62 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!DOCTYPE html>
<html ng-app="eCommerce">
<head>
<title>eCommerce</title>
<link rel="stylesheet" href="style.css">
</head>
<body ng-controller="mainCtrl">

<div class="home">

<div class="amazon-logo">
<img src="https://excoupuk.com/wp-content/uploads/2015/10/Amazon-Logo.png" alt="">
</div>

<div class="input-product">
<form>
<div class="spaced">
<label>Title: </label><input ng-model="product.title" type="text" placeholder="Coca Cola">
</div>
<div class="spaced">
<label>Price: </label><input ng-model="product.price" type="text" placeholder="$3.99">
</div>
<div class="spaced">
<label class="text-box">Description: </label><textarea ng-model="product.description" class="text-area text-box" placeholder="Description..."></textarea>
</div>
<div class="spaced">
<button ng-click="addProduct(product)">Add</button>
</div>
</form>
</div>

<div>
Filter: <input type="text" ng-model="searchTerm">
</div>

<div class="products">
<ul>
<li class="product-box" ng-repeat="product in products | filter : searchTerm track by product._id">
<p><span class="alter">Title:</span> {{product.title | uppercase}}</p>
<p><span class="alter">Price:</span> {{product.price | currency}}</p>
<p><span class="alter">Description:</span> {{product.description}}</p>
<button ng-click="editProduct(product._id)">Edit</button>
<button ng-click="deleteProduct(product._id)">Delete</button>
</li>
</ul>
</div>

</div>






<!-- Angular -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.js"></script>
<script src="./js/app.js"></script>
<script src="./js/controllers/mainCtrl.js"></script>
<script src="./js/mainService.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions public/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
angular.module('eCommerce', ['ui.router']);
25 changes: 25 additions & 0 deletions public/js/controllers/mainCtrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
angular.module('eCommerce')
.controller('mainCtrl', function ($scope, mainService) {

$scope.getProducts = function () {
mainService.getProducts()
.then(function (response) {
$scope.products = response.data;
console.log($scope.products);
})
}

$scope.addProduct = function (product){
mainService.addProduct(product);
}

$scope.deleteProduct = function (mongoId) {
mainService.deleteProduct(mongoId);
}

$scope.getProducts();



// end of mainCtrl
})
18 changes: 18 additions & 0 deletions public/js/mainService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
angular.module('eCommerce')
.service('mainService', function($http){
this.getProducts = function () {
return $http.get('/api/products');
}

this.addProduct = function (product) {
return $http.post('/api/products', product);
}

this.deleteProduct = function (mongoId) {
console.log(mongoId);
return $http.delete('/api/products/' + mongoId);
}


// end of mainService
})
50 changes: 50 additions & 0 deletions public/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
body{
background-color: lightgray;
margin: 0px;
padding: 0px;
}
.home{
width: auto;
height: auto;
border: 10px solid green;
padding: 10px;
}
.amazon-logo{
text-align: center;
}
.myAmazon{
text-align: right;
}
.myAmazon h3{
display: inline-block;
margin: 0px;
padding: 5px;
background-color: yellow;
border-radius: 5px;
}
.product-box{
margin: 10px;
border: 10px solid black;
padding: 10px;
}
.products p{
font-size: 20px;
}
.products ul{
list-style-type: none;
}
.alter{
color: blue;
font-weight: bold;
}
.text-box {
display:inline-block;
vertical-align:top;
}
.text-area{
margin-left: 5px;
}
.spaced{
margin-top: 10px;
margin-bottom: 10px;
}
9 changes: 9 additions & 0 deletions server/controllers/Product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const mongoose = require('mongoose');

let ProductSchema = new mongoose.Schema({
title: {type: String, unique: true, required: true, index: true},
price: {type: Number, required: true, min: 0},
description: {type: String, required: true}
});

module.exports = mongoose.model('Product', ProductSchema);
75 changes: 75 additions & 0 deletions server/controllers/mainCtrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// const mongodb = require('mongojs');
// let db = mongodb('ecommerce', ['products']);
const Product = require('./Product.js')

module.exports = {
getProducts(req, res, next){
Product.find({}, (error, products)=>{
if (error) {
return res.status(500).json(error);
}
return res.status(200).json(products);
});
},

getProductByQuery(req, res, next){
Product.find(req.query, (error, products)=>{
if (error) {
return res.status(500).json(error);
}
return res.status(201).json(products);
})
},

getProduct(req, res, next) {
Product.findById(req.params.id, (error, product)=>{
if (error) {
return res.status(500).json(error);
}
return res.status(200).json(product);
})
},

createProduct(req, res, next){
new Product(req.body).save((error, productCreated)=>{
if (error) {
return res.status(500).json(error);
}
return res.status(201).json(productCreated);
})
},

deleteProduct(req, res, next){
if (!req.params.id) {
return res.status(500).json("Need a param Id");
}
Product.findByIdAndRemove(req.params.id, (error, deletedProduct)=>{
if (error) {
return res.status(500).json(error);
}
return res.status(200).json(deletedProduct);
})
},

updateProduct(req, res, next){
if (!req.params.id) {
return res.status(500).json("Need a param Id");
}
Product.findByIdAndUpdate(req.params.id, req.body, (error, updatedProduct)=>{
if (error) {
return res.status(500).json(error);
}
return res.status(200).json(updatedProduct);
})
},
}










10 changes: 10 additions & 0 deletions server/controllers/mainRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const mainCtrl = require('./mainCtrl.js');

module.exports = (app) =>{
app.get('/api/products', mainCtrl.getProductByQuery);
app.get('/api/products', mainCtrl.getProducts);
app.get('/api/products/:id', mainCtrl.getProduct);
app.post('/api/products', mainCtrl.createProduct);
app.put('/api/products/:id', mainCtrl.updateProduct);
app.delete('/api/products/:id', mainCtrl.deleteProduct);
}
5 changes: 5 additions & 0 deletions server/masterRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const mainRoutes = require('./controllers/mainRoutes.js')

module.exports = (app) =>{
mainRoutes(app);
}