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-12-evan/.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"
}
115 changes: 115 additions & 0 deletions lab-12-evan/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@

# Created by https://www.gitignore.io/api/windows,node,linux,macos

### 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


### Node ###
node_modules
# 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



### 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*


### 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
25 changes: 25 additions & 0 deletions lab-12-evan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Musical Atists RESTful api

## Overview
This is a RESTful API built with Express. The API gives the ability to add, view, update, and delete artists. The application utilizes Express to abstract the the http server process and Express.Router for handling requests.

## Getting started
To get started using this API
- Clone this repository
- Run `npm i`
- Once dependencies have been installed, run `node server.js`
- As long as there are no issues, you should be alerted thate the server is up and running.
- `npm test` will give you lots of detailed information about what's going on with app, provided by `morgan` and `debug`

## Usage
To make use of this API, by adding artists you like


* Download [httpie](http://httpie.org)

* To add and additional artist:
* `http POST localhost:3000/api/artist? name=artistName genre=genre topHits=songName,songName`

* To view an artist:
* `http localhost:3000/api/artist/uniqueIDassignedToTheArtist`

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"30fb2800-c7ab-11e6-a939-fd2b384dbb82","name":"steveAoki","genre":"EDM","topHits":"Aoki"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"424f09f0-c7ab-11e6-a939-fd2b384dbb82","name":"Odesza","genre":"EDM","topHits":"howDidIGetHere"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"9b035670-c7a4-11e6-abf8-675fa887f7fe","name":"exampleArtist","genre":"exampleGenre","topHits":"exampleTopHits"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"9b0d8fa0-c7a4-11e6-abf8-675fa887f7fe","name":"exampleArtist","genre":"exampleGenre","topHits":"exampleTopHits"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"c65ee2d0-c7a4-11e6-81e5-f792bef12936","name":"exampleArtist","genre":"exampleGenre","topHits":"exampleTopHits"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"ca401740-c739-11e6-a234-21623108acce","name":"Odeza","genre":"EDM","topHits":"HowDidIGetHere"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"ec5d7570-c739-11e6-a234-21623108acce","name":"SteveAoki","genre":"EDM","topHits":"AokisHouse"}
26 changes: 26 additions & 0 deletions lab-12-evan/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

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

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

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

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

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

module.exports = function(req, res, next) {
res.append('Access-Control-Allow-Origin', '*');
res.append('Access-Control-Allow-Headers', '*');
next();
};
20 changes: 20 additions & 0 deletions lab-12-evan/lib/ERRORS-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const createError = require('http-errors');
const debug = require('debug')('artist:ERROR-middleware');

module.exports = function(err, req, res, next) {

if(err.status) {
debug('user error');

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

debug('sever error');
err = createError(500, err.message);
res.status(err.status).send(err.name);
next();
};
55 changes: 55 additions & 0 deletions lab-12-evan/lib/artist_storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'), { suffix: 'Prom'});
const createError = require('http-errors');
const debug = require('debug')('artist:artist_storage');

module.exports = exports = {};

exports.newArtist = function(schemaName, artist) {
debug('newArtist');

if(!schemaName) return Promise.reject(createError(400, 'expected schema name'));
if(!artist) return Promise.reject(createError(400, 'expected an artist'));

let json = JSON.stringify(artist);
return fs.writeFileProm(`${__dirname}/../data/${schemaName}/${artist.id}.json`, json)
.then( () => artist)
.catch( err => Promise.reject(createError(500, err.message)));
};

exports.getArtist = function(schemaName, id) {
debug('getArtist');

if(!schemaName) return Promise.reject(createError(400, 'expected schema name'));
if(!id) return Promise.reject(createError(400, 'expected an id'));

return fs.readFileProm(`${__dirname}/../data/${schemaName}/${id}.json`)
.then( data => {
try {
let artist = JSON.parse(data.toString())
return artist;
}
catch(err) {
return Promise.reject(createError(500, err.message));
}
})
.catch( err => Promise.reject(createError(404, err.message)));
};

exports.deleteArtist = function(schemaName, id) {
debug('deleteArtist');

if(!schemaName) return Promise.reject(createError(400, 'expected a schema name'));
if(!id) return Promise.reject(createError(400, 'expected an id'));

return fs.unlinkProm(`${__dirname}/../data/${schemaName}/${id}.json`)
.catch( err => Promise.reject(createError(404, err.message)));
};

exports.allIDs = function(schemaName) {
return fs.readdirProm(`${__dirname}/../data/${schemaName}`)
.then( files => files.map( name => name.split('.json')[0]))
.catch( err => Promise.reject(createError(404, err.message)));
};
61 changes: 61 additions & 0 deletions lab-12-evan/model/artist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

const uuid = require('node-uuid');
const createError = require('http-errors');
const debug = require('debug')('artist:artist');
const artist_storage = require('../lib/artist_storage.js');


const Artist = module.exports = function(name, genre, topHits) {
debug('artist constructor');

if(!name) throw createError(400, 'expected name');
if(!genre) throw createError(400, 'expected a genre');
if(!topHits) throw createError(400, 'expected top hits');

this.id = uuid.v1();
this.name = name;
this.genre = genre;
this.topHits = topHits;
};

Artist.newArtist = function(_artist) {
debug('newArtist');

try {
let artist = new Artist(_artist.name, _artist.genre, _artist.topHits);
return artist_storage.newArtist('artist', artist);
}
catch(err) {
return Promise.reject(createError(400, err.message));
};
};

Artist.getArtist = function(id) {
debug('getArtist');
return artist_storage.getArtist('artist', id);
};

Artist.updateArtist = function(id, _artist) {
debug('updateArtist');

return artist_storage.getArtist('artist', id)
.catch( err => Promise.reject(createError(404, err.message)))
.then( artist => {
for(var prop in artist) {
if(prop === 'id') continue;
if(_artist[prop]) artist[prop] = _artist[prop];
};
return artist_storage.newArtist('artist', artist);
});
};

Artist.deleteArtist = function(id) {
debug('deleteArtist');
return artist_storage.deleteArtist('artist', id);
};

Artist.allIDs = function() {
debug('allIDs');
return artist_storage.allIDs('artist');
};
31 changes: 31 additions & 0 deletions lab-12-evan/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "lab-12-evan",
"version": "1.0.0",
"description": "",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "DEBUG='artist*' mocha",
"start": "DEBUG='artist*' node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bluebird": "^3.4.6",
"body-parser": "^1.15.2",
"debug": "^2.4.5",
"express": "^4.14.0",
"http-errors": "^1.5.1",
"morgan": "^1.7.0",
"node-uuid": "^1.4.7"
},
"devDependencies": {
"chai": "^3.5.0",
"gulp-eslint": "^3.0.1",
"mocha": "^3.2.0",
"superagent": "^3.3.1"
}
}
Loading