Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nagypeter committed May 4, 2017
0 parents commit b63772b
Show file tree
Hide file tree
Showing 14 changed files with 387 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#Node.js-Express-MongoDB CRUD sample Application

This is a simple Node.js CRUD application using MongoDB.

It is based on
[https://github.com/ijason/NodeJS-Sample-App](https://github.com/ijason/NodeJS-Sample-App) and has the following features:

+ includes Wercker configuration
+ application changes to run on Oracle Container Cloud Service

### How to run

npm install
node app.js

### Configuration

MongoDB: `mongodb://mongodb`

Express: `app.listen(process.env.PORT || 3000);`

##History

### 1.0.0

- Initial version

## License

The Universal Permissive License (UPL), Version 1.0

92 changes: 92 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Module dependencies.
*/

var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, EmployeeProvider = require('./employeeprovider').EmployeeProvider;

var app = express();

app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {layout: false});
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.enable('trust proxy');
});

app.configure('development', function(){
app.use(express.errorHandler());
});

var employeeProvider= new EmployeeProvider();

//Routes

//index
app.get('/', function(req, res){
employeeProvider.findAll(function(error, emps){
res.render('index', {
title: 'Employees',
employees:emps
});
});
});

//new employee
app.get('/employee/new', function(req, res) {
res.render('employee_new', {
title: 'New Employee'
});
});

//save new employee
app.post('/employee/new', function(req, res){
employeeProvider.save({
name: req.param('name'),
title: req.param('title')
}, function( error, docs) {
res.redirect('/')
});
});

//update an employee
app.get('/employee/:id/edit', function(req, res) {
employeeProvider.findById(req.params.id, function(error, employee) {
res.render('employee_edit',
{
_id: employee._id.toHexString(),
name: employee.name,
title: employee.title,
});
});
});

//save updated employee
app.post('/employee/:id/edit', function(req, res) {
employeeProvider.update(req.params.id,{
name: req.param('name'),
title: req.param('title')
}, function(error, docs) {
res.redirect('/')
});
});

//delete an employee
app.get('/employee/:id/delete', function(req, res) {
employeeProvider.delete(req.params.id, function(error, docs) {
res.redirect('/')
});
});

app.listen(process.env.PORT || 3000);
108 changes: 108 additions & 0 deletions employeeprovider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON,
assert = require('assert');

EmployeeProvider = function() {
var that = this;
mongodbUri = process.env.MONGOLAB_URI || 'mongodb://mongodb';
MongoClient.connect(mongodbUri, function(err, db){
if(err) { return console.dir(err); }
that.db = db;
})
};


EmployeeProvider.prototype.getCollection= function(callback) {
this.db.collection('employees', function(error, employee_collection) {
if( error ) callback(error);
else callback(null, employee_collection);
});
};

//find all employees
EmployeeProvider.prototype.findAll = function(callback) {
this.getCollection(function(error, employee_collection) {
if( error ) callback(error)
else {
employee_collection.find().toArray(function(error, results) {
if( error ) callback(error)
else callback(null, results)
});
}
});
};

//find an employee by ID
EmployeeProvider.prototype.findById = function(id, callback) {
this.getCollection(function(error, employee_collection) {
if( error ) callback(error)
else {
employee_collection.findOne({_id: employee_collection.db.bson_serializer.ObjectID.createFromHexString(id)}, function(error, result) {
if( error ) callback(error)
else callback(null, result)
});
}
});
};


//save new employee
EmployeeProvider.prototype.save = function(employees, callback) {
this.getCollection(function(error, employee_collection) {
if( error ) callback(error)
else {
if( typeof(employees.length)=="undefined")
employees = [employees];

for( var i =0;i< employees.length;i++ ) {
employee = employees[i];
employee.created_at = new Date();
}

employee_collection.insert(employees, function() {
callback(null, employees);
});
}
});
};

// update an employee
EmployeeProvider.prototype.update = function(employeeId, employees, callback) {
this.getCollection(function(error, employee_collection) {
if( error ) callback(error);
else {
employee_collection.update(
{_id: employee_collection.db.bson_serializer.ObjectID.createFromHexString(employeeId)},
employees,
function(error, employees) {
if(error) callback(error);
else callback(null, employees)
});
}
});
};

//delete employee
EmployeeProvider.prototype.delete = function(employeeId, callback) {
this.getCollection(function(error, employee_collection) {
if(error) callback(error);
else {
employee_collection.remove(
{_id: employee_collection.db.bson_serializer.ObjectID.createFromHexString(employeeId)},
function(error, employee){
if(error) callback(error);
else callback(null, employee)
});
}
});
};

exports.EmployeeProvider = EmployeeProvider;
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "ExpressTut",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app"
},
"dependencies": {
"express": "3.4.0",
"jade": "*",
"mongodb": "1.3.19"
}
}
Binary file added public/img/rocket.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
@import url(http://fonts.googleapis.com/css?family=Titillium+Web:300);

html,
body {
background: #eee url("../img/rocket.png") bottom left / auto 50% no-repeat;
height: 100%;
font-family: 'Titillium Web', sans-serif;
}

h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 {
font-family: 'Titillium Web', sans-serif;
text-align: center;
}

#employees {
margin: 0 auto;
padding-bottom: 20px;
width: auto;
max-width: 680px;
}

.container {
width: auto;
}

.form-signin {
max-width: 330px;
padding: 15px;
margin: 0 auto;
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}
.form-signin .form-control {
position: relative;
font-size: 16px;
height: auto;
padding: 10px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[name="name"] {
margin-bottom: -1px;
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}
.form-signin input[name="title"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}

8 changes: 8 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

/*
* GET home page.
*/

exports.index = function(req, res){
res.render('index', { title: 'Express' });
};
8 changes: 8 additions & 0 deletions routes/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

/*
* GET users listing.
*/

exports.list = function(req, res){
res.send("respond with a resource");
};
12 changes: 12 additions & 0 deletions views/employee_edit.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extends layout

block content
div.container
form.form-signin(method="post")
h2.form-singin-heading= "Edit Employee"
input(name="_id", type="hidden", value=_id)
input.form-control(type="text", name="name", id="editEmployeeName", placeholder="Name", value=name, autofocus)
input.form-control(type="text", name="title", id="editEmployeeTitle", placeholder="Title", value=title)
input(type="submit", value="Update", class="btn btn-lg btn-primary btn-block")
br
a(href="/")!= "Back to Employee List"
11 changes: 11 additions & 0 deletions views/employee_new.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extends layout

block content
div.container
form.form-signin(method="post")
h2.form-singin-heading= title
input.form-control(type="text", name="name", id="editEmployeeName", placeholder="Name", autofocus)
input.form-control(type="text", name="title", id="editEmployeeTitle", placeholder="Title")
input(type="submit", value="Save", class="btn btn-lg btn-primary btn-block")
br
a(href="/")!= "Back to Employee List"
20 changes: 20 additions & 0 deletions views/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extends layout

block content
div.container
div.page-header
h1 <strong>Hi, I'm an Express + MongoDB app running on OCCS1!</strong>
#employees
h2= title
- each employee in employees
div(class="panel panel-primary")
div.created_at(class="hidden")= employee.created_at
div.panel-heading
div.title(class="panel-title")= employee.name
div.panel-body
div(class="col-xs-10 col-md-10")= employee.title
div(class="col-xs-1 col-md-1")
a(href="/employee/" + employee._id.toHexString() + "/edit" class="btn btn-info")!= "Edit"
div(class="col-xs-1 col-md-1")
a(href="/employee/" + employee._id.toHexString() + "/delete" class="btn btn-danger")!= "X"
a(href="/employee/new" class="btn btn-default")!= "Add New Employee"
8 changes: 8 additions & 0 deletions views/layout.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
doctype html
html
head
title Node.js + Express + MongoDB App running on cloudControl
link(rel='stylesheet', href='//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css')
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content
Loading

0 comments on commit b63772b

Please sign in to comment.