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
21 changes: 21 additions & 0 deletions lab-hawa/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": [ "error", "single" ],
"semi": ["error", "always"],
"linebreak-style": [ "error", "unix" ]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
117 changes: 117 additions & 0 deletions lab-hawa/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Created by https://www.gitignore.io/api/ma,node,macos,windows,linux

#!! ERROR: ma is undefined. Use list command to see defined gitignore types !!#

node_modules/

### Node ###
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity



### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


### Windows ###
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk


### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*
90 changes: 90 additions & 0 deletions lab-hawa/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
__Dual Resource Express API__
======
This server-side application creates a dual resource REST API using the MongoDB database to capture data on the different bank accounts customers may have as well as alerts they might receive. This project also allows users to interact with the database by from of POST, GET, PUT and DELETE requests via the terminal.

---
## Cloning the Repository
Clone down this repository: https://github.com/abdih17/14-mongo_express_two_resource_api.git

```
$ git clone https://github.com/abdih17/14-mongo_express_two_resource_api.git
```

## Installation

Install any dependency from the `package.json` file into the project root
directory, and start the server.

```sh
$ cd lab-hawa
$ npm i
$ npm start
```

You should receive the following result: `Server up: 3000` or whichever port number you have preset as your environment variables.

You will also need to make another tab within the terminal to begin the process of running the Mongo database.

```sh
$ mongod
```

At the end of the message you should see `waiting for connections on port 27017`.

## Alert: POST, GET, PUT and DELETE Requests

**POST Request:**
The POST request must include content and timestamp parameters.

>**In an new terminal window, send a POST request by using the command:**
>`http POST localhost:3000/api/alert content="<content>" timestamp="<timestamp date>"`.

A successful response should return a JSON object with values you entered along with a unique **id number** and a status code of **200**.

**GET Request:**

>**In a new terminal window, send a `GET` request by using the command:**
>`http localhost:3000/api/alert?id=<id>`.

**PUT REQUEST:**

>**In a new terminal window, send a `GET` request by using the command:**
>`http localhost:3000/api/alert?id=<id> content="<update content>" timestamp="<update timestamp date>"`.


**DELETE Request:**

>**In a new terminal window, send a `DELETE` request by using the command:**
>`http DELETE localhost:8000/api/alert/<id>`

## Bank Account: POST, GET, PUT and DELETE Requests

**POST Request:**
The POST request must include name, bankAccount, and bankAccountName parameters.

>**In an new terminal window, send a POST request by using the command:**
>`http POST localhost:3000/api/alert/<alert id number> name="<name>" cardNumber="<a card number>"`.

A successful response should return a JSON object with values you entered along with a unique **id number** and a status code of **200**.

**GET Request:**

>**In a new terminal window, send a `GET` request by using the command:**
>`http localhost:3000/api/bankAccount?id=<id>`.

**PUT REQUEST:**

>**In a new terminal window, send a `GET` request by using the command:**
>`http localhost:3000/api/bankAccount?id=<id> name=<update name> cardNumber=<update card number>`.


**DELETE Request:**

>**In a new terminal window, send a `DELETE` request by using the command:**
>`http DELETE localhost:8000/api/bankAccount/<id>`

## Exit the Server

Go back to the terminal where your server is running with the port number and press **Ctrl+C** in order to exit the server.

---
Empty file.
23 changes: 23 additions & 0 deletions lab-hawa/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const gulp = require('gulp');
const eslint = require('gulp-eslint');
const mocha = require('gulp-mocha');

gulp.task('test', function(){
gulp.src('./test/*-test.js', {read: false})
.pipe(mocha({reporter: 'spec'}));
});

gulp.task('lint', function(){
return gulp.src(['**/*.js','!node_modules/**'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});

gulp.task('dev', function(){
gulp.watch(['**/*.js','!node_modules/**'], ['test', 'lint']);
});

gulp.task('default', ['dev']);
28 changes: 28 additions & 0 deletions lab-hawa/lib/error-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const createError = require('http-errors');
const debug = require('debug')('bankAccount:error-middleware');

module.exports = function(err, req, res, next) {
debug('error-middleware');

console.error('msg:', err.message);
console.error('name:', err.name);

if (err.status) {
res.status(err.status).send(err.name);
next();
return;
}

if (err.name === 'ValidationError') {
err = createError(400, err.message);
res.status(err.status).send(err.name);
next();
return;
}

err = createError(500, err.message);
res.status(err.status).send(err.name);
next();
};
36 changes: 36 additions & 0 deletions lab-hawa/model/alert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const mongoose = require('mongoose');
const createError = require('http-errors');
const debug = require('debug')('bankAccount:alert');
const Schema = mongoose.Schema;

const BankAccount = require('./bankAccount.js');

const alertSchema = Schema({
content: { type: String, required: true },
timestamp: { type: Date, default: Date.now },
bankAccounts: [{ type: Schema.Types.ObjectId, ref: 'bankAccount' }]
});

const Alert = module.exports = mongoose.model('alert', alertSchema);

Alert.findByIdAndAddBankAccount = function(id, bankAccount) {
debug('findByIdAndAddBankAccount');

return Alert.findById(id)
.catch( err => Promise.reject(createError(404, err.message)))
.then( alert => {
bankAccount.alertID = alert._id;
this.tempAlert = alert;
return new BankAccount(bankAccount).save();
})
.then( bankAccount => {
this.tempAlert.bankAccounts.push(bankAccount._id);
this.tempbankAccount = bankAccount;
return this.tempAlert.save();
})
.then( () => {
return this.tempbankAccount;
});
};
12 changes: 12 additions & 0 deletions lab-hawa/model/bankAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const bankAccountSchema = Schema({
name: { type: String, required: true },
cardNumber: { type: Number, required: true },
alertID: { type: Schema.Types.ObjectId, required: true }
});

module.exports = mongoose.model('bankAccount', bankAccountSchema);
31 changes: 31 additions & 0 deletions lab-hawa/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "lab-hawa",
"version": "1.0.0",
"description": "",
"main": "gulpfile.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "DEBUG='bankAccount*' mocha",
"start": "DEBUG='bankAccount*' node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bluebird": "^3.4.7",
"body-parser": "^1.15.2",
"cors": "^2.8.1",
"debug": "^2.5.1",
"express": "^4.14.0",
"http-errors": "^1.5.1",
"mongoose": "^4.7.4",
"morgan": "^1.7.0"
},
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^3.2.0",
"superagent": "^3.3.1"
}
}
Loading