From 965b907d072303d7e08d07643193b3be668160a8 Mon Sep 17 00:00:00 2001 From: Mumshad Mannambeth Date: Fri, 7 Jul 2017 12:18:33 -0400 Subject: [PATCH] - Remove things --- client/app/main/main.component.js | 23 ---- server/api/thing/index.js | 15 -- server/api/thing/index.spec.js | 86 ------------ server/api/thing/thing.controller.js | 118 ---------------- server/api/thing/thing.events.js | 35 ----- server/api/thing/thing.integration.js | 190 -------------------------- server/api/thing/thing.model.js | 13 -- server/routes.js | 1 - 8 files changed, 481 deletions(-) delete mode 100644 server/api/thing/index.js delete mode 100644 server/api/thing/index.spec.js delete mode 100644 server/api/thing/thing.controller.js delete mode 100644 server/api/thing/thing.events.js delete mode 100644 server/api/thing/thing.integration.js delete mode 100644 server/api/thing/thing.model.js diff --git a/client/app/main/main.component.js b/client/app/main/main.component.js index 8a02193..0b480d4 100644 --- a/client/app/main/main.component.js +++ b/client/app/main/main.component.js @@ -4,33 +4,10 @@ import routing from './main.routes'; export class MainController { - awesomeThings = []; - newThing = ''; - /*@ngInject*/ constructor($http) { this.$http = $http; } - - $onInit() { - this.$http.get('/api/things') - .then(response => { - this.awesomeThings = response.data; - }); - } - - addThing() { - if(this.newThing) { - this.$http.post('/api/things', { - name: this.newThing - }); - this.newThing = ''; - } - } - - deleteThing(thing) { - this.$http.delete(`/api/things/${thing._id}`); - } } export default angular.module('app2App.main', [uiRouter]) diff --git a/server/api/thing/index.js b/server/api/thing/index.js deleted file mode 100644 index bd1da07..0000000 --- a/server/api/thing/index.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict'; - -var express = require('express'); -var controller = require('./thing.controller'); - -var router = express.Router(); - -router.get('/', controller.index); -router.get('/:id', controller.show); -router.post('/', controller.create); -router.put('/:id', controller.upsert); -router.patch('/:id', controller.patch); -router.delete('/:id', controller.destroy); - -module.exports = router; diff --git a/server/api/thing/index.spec.js b/server/api/thing/index.spec.js deleted file mode 100644 index af463fa..0000000 --- a/server/api/thing/index.spec.js +++ /dev/null @@ -1,86 +0,0 @@ -'use strict'; - -/* globals sinon, describe, expect, it */ - -var proxyquire = require('proxyquire').noPreserveCache(); - -var thingCtrlStub = { - index: 'thingCtrl.index', - show: 'thingCtrl.show', - create: 'thingCtrl.create', - upsert: 'thingCtrl.upsert', - patch: 'thingCtrl.patch', - destroy: 'thingCtrl.destroy' -}; - -var routerStub = { - get: sinon.spy(), - put: sinon.spy(), - patch: sinon.spy(), - post: sinon.spy(), - delete: sinon.spy() -}; - -// require the index with our stubbed out modules -var thingIndex = proxyquire('./index.js', { - express: { - Router() { - return routerStub; - } - }, - './thing.controller': thingCtrlStub -}); - -describe('Thing API Router:', function() { - it('should return an express router instance', function() { - expect(thingIndex).to.equal(routerStub); - }); - - describe('GET /api/things', function() { - it('should route to thing.controller.index', function() { - expect(routerStub.get - .withArgs('/', 'thingCtrl.index') - ).to.have.been.calledOnce; - }); - }); - - describe('GET /api/things/:id', function() { - it('should route to thing.controller.show', function() { - expect(routerStub.get - .withArgs('/:id', 'thingCtrl.show') - ).to.have.been.calledOnce; - }); - }); - - describe('POST /api/things', function() { - it('should route to thing.controller.create', function() { - expect(routerStub.post - .withArgs('/', 'thingCtrl.create') - ).to.have.been.calledOnce; - }); - }); - - describe('PUT /api/things/:id', function() { - it('should route to thing.controller.upsert', function() { - expect(routerStub.put - .withArgs('/:id', 'thingCtrl.upsert') - ).to.have.been.calledOnce; - }); - }); - - describe('PATCH /api/things/:id', function() { - it('should route to thing.controller.patch', function() { - expect(routerStub.patch - .withArgs('/:id', 'thingCtrl.patch') - ).to.have.been.calledOnce; - }); - }); - - describe('DELETE /api/things/:id', function() { - it('should route to thing.controller.destroy', function() { - expect(routerStub.delete - .withArgs('/:id', 'thingCtrl.destroy') - ).to.have.been.calledOnce; - }); - }); -}); diff --git a/server/api/thing/thing.controller.js b/server/api/thing/thing.controller.js deleted file mode 100644 index f8ddd9c..0000000 --- a/server/api/thing/thing.controller.js +++ /dev/null @@ -1,118 +0,0 @@ -/** - * Using Rails-like standard naming convention for endpoints. - * GET /api/things -> index - * POST /api/things -> create - * GET /api/things/:id -> show - * PUT /api/things/:id -> upsert - * PATCH /api/things/:id -> patch - * DELETE /api/things/:id -> destroy - */ - -'use strict'; - -import jsonpatch from 'fast-json-patch'; -import Thing from './thing.model'; - -function respondWithResult(res, statusCode) { - statusCode = statusCode || 200; - return function(entity) { - if(entity) { - return res.status(statusCode).json(entity); - } - return null; - }; -} - -function patchUpdates(patches) { - return function(entity) { - try { - // eslint-disable-next-line prefer-reflect - jsonpatch.apply(entity, patches, /*validate*/ true); - } catch(err) { - return Promise.reject(err); - } - - return entity.save(); - }; -} - -function removeEntity(res) { - return function(entity) { - if(entity) { - return entity.remove() - .then(() => { - res.status(204).end(); - }); - } - }; -} - -function handleEntityNotFound(res) { - return function(entity) { - if(!entity) { - res.status(404).end(); - return null; - } - return entity; - }; -} - -function handleError(res, statusCode) { - statusCode = statusCode || 500; - return function(err) { - res.status(statusCode).send(err); - }; -} - -// Gets a list of Things -export function index(req, res) { - return Thing.find().exec() - .then(respondWithResult(res)) - .catch(handleError(res)); -} - -// Gets a single Thing from the DB -export function show(req, res) { - return Thing.findById(req.params.id).exec() - .then(handleEntityNotFound(res)) - .then(respondWithResult(res)) - .catch(handleError(res)); -} - -// Creates a new Thing in the DB -export function create(req, res) { - return Thing.create(req.body) - .then(respondWithResult(res, 201)) - .catch(handleError(res)); -} - -// Upserts the given Thing in the DB at the specified ID -export function upsert(req, res) { - if(req.body._id) { - Reflect.deleteProperty(req.body, '_id'); - } - return Thing.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec() - - .then(respondWithResult(res)) - .catch(handleError(res)); -} - -// Updates an existing Thing in the DB -export function patch(req, res) { - if(req.body._id) { - Reflect.deleteProperty(req.body, '_id'); - } - return Thing.findById(req.params.id).exec() - .then(handleEntityNotFound(res)) - .then(patchUpdates(req.body)) - .then(respondWithResult(res)) - .catch(handleError(res)); -} - -// Deletes a Thing from the DB -export function destroy(req, res) { - return Thing.findById(req.params.id).exec() - .then(handleEntityNotFound(res)) - .then(removeEntity(res)) - .catch(handleError(res)); -} diff --git a/server/api/thing/thing.events.js b/server/api/thing/thing.events.js deleted file mode 100644 index 3add968..0000000 --- a/server/api/thing/thing.events.js +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Thing model events - */ - -'use strict'; - -import {EventEmitter} from 'events'; -var ThingEvents = new EventEmitter(); - -// Set max event listeners (0 == unlimited) -ThingEvents.setMaxListeners(0); - -// Model events -var events = { - save: 'save', - remove: 'remove' -}; - -// Register the event emitter to the model events -function registerEvents(Thing) { - for(var e in events) { - let event = events[e]; - Thing.post(e, emitEvent(event)); - } -} - -function emitEvent(event) { - return function(doc) { - ThingEvents.emit(`${event}:${doc._id}`, doc); - ThingEvents.emit(event, doc); - }; -} - -export {registerEvents}; -export default ThingEvents; diff --git a/server/api/thing/thing.integration.js b/server/api/thing/thing.integration.js deleted file mode 100644 index 85e0d16..0000000 --- a/server/api/thing/thing.integration.js +++ /dev/null @@ -1,190 +0,0 @@ -'use strict'; - -/* globals describe, expect, it, beforeEach, afterEach */ - -var app = require('../..'); -import request from 'supertest'; - -var newThing; - -describe('Thing API:', function() { - describe('GET /api/things', function() { - var things; - - beforeEach(function(done) { - request(app) - .get('/api/things') - .expect(200) - .expect('Content-Type', /json/) - .end((err, res) => { - if(err) { - return done(err); - } - things = res.body; - done(); - }); - }); - - it('should respond with JSON array', function() { - expect(things).to.be.instanceOf(Array); - }); - }); - - describe('POST /api/things', function() { - beforeEach(function(done) { - request(app) - .post('/api/things') - .send({ - name: 'New Thing', - info: 'This is the brand new thing!!!' - }) - .expect(201) - .expect('Content-Type', /json/) - .end((err, res) => { - if(err) { - return done(err); - } - newThing = res.body; - done(); - }); - }); - - it('should respond with the newly created thing', function() { - expect(newThing.name).to.equal('New Thing'); - expect(newThing.info).to.equal('This is the brand new thing!!!'); - }); - }); - - describe('GET /api/things/:id', function() { - var thing; - - beforeEach(function(done) { - request(app) - .get(`/api/things/${newThing._id}`) - .expect(200) - .expect('Content-Type', /json/) - .end((err, res) => { - if(err) { - return done(err); - } - thing = res.body; - done(); - }); - }); - - afterEach(function() { - thing = {}; - }); - - it('should respond with the requested thing', function() { - expect(thing.name).to.equal('New Thing'); - expect(thing.info).to.equal('This is the brand new thing!!!'); - }); - }); - - describe('PUT /api/things/:id', function() { - var updatedThing; - - beforeEach(function(done) { - request(app) - .put(`/api/things/${newThing._id}`) - .send({ - name: 'Updated Thing', - info: 'This is the updated thing!!!' - }) - .expect(200) - .expect('Content-Type', /json/) - .end(function(err, res) { - if(err) { - return done(err); - } - updatedThing = res.body; - done(); - }); - }); - - afterEach(function() { - updatedThing = {}; - }); - - it('should respond with the updated thing', function() { - expect(updatedThing.name).to.equal('Updated Thing'); - expect(updatedThing.info).to.equal('This is the updated thing!!!'); - }); - - it('should respond with the updated thing on a subsequent GET', function(done) { - request(app) - .get(`/api/things/${newThing._id}`) - .expect(200) - .expect('Content-Type', /json/) - .end((err, res) => { - if(err) { - return done(err); - } - let thing = res.body; - - expect(thing.name).to.equal('Updated Thing'); - expect(thing.info).to.equal('This is the updated thing!!!'); - - done(); - }); - }); - }); - - describe('PATCH /api/things/:id', function() { - var patchedThing; - - beforeEach(function(done) { - request(app) - .patch(`/api/things/${newThing._id}`) - .send([ - { op: 'replace', path: '/name', value: 'Patched Thing' }, - { op: 'replace', path: '/info', value: 'This is the patched thing!!!' } - ]) - .expect(200) - .expect('Content-Type', /json/) - .end(function(err, res) { - if(err) { - return done(err); - } - patchedThing = res.body; - done(); - }); - }); - - afterEach(function() { - patchedThing = {}; - }); - - it('should respond with the patched thing', function() { - expect(patchedThing.name).to.equal('Patched Thing'); - expect(patchedThing.info).to.equal('This is the patched thing!!!'); - }); - }); - - describe('DELETE /api/things/:id', function() { - it('should respond with 204 on successful removal', function(done) { - request(app) - .delete(`/api/things/${newThing._id}`) - .expect(204) - .end(err => { - if(err) { - return done(err); - } - done(); - }); - }); - - it('should respond with 404 when thing does not exist', function(done) { - request(app) - .delete(`/api/things/${newThing._id}`) - .expect(404) - .end(err => { - if(err) { - return done(err); - } - done(); - }); - }); - }); -}); diff --git a/server/api/thing/thing.model.js b/server/api/thing/thing.model.js deleted file mode 100644 index 061a817..0000000 --- a/server/api/thing/thing.model.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -import mongoose from 'mongoose'; -import {registerEvents} from './thing.events'; - -var ThingSchema = new mongoose.Schema({ - name: String, - info: String, - active: Boolean -}); - -registerEvents(ThingSchema); -export default mongoose.model('Thing', ThingSchema); diff --git a/server/routes.js b/server/routes.js index 0990bd5..6d609f3 100644 --- a/server/routes.js +++ b/server/routes.js @@ -13,7 +13,6 @@ export default function(app) { app.use('/api/custom_modules', auth.isAuthenticated(), require('./api/custom_module')); app.use('/api/ansible', auth.isAuthenticated(), require('./api/ansible')); app.use('/api/projects', auth.isAuthenticated(), require('./api/project')); - app.use('/api/things', require('./api/thing')); app.use('/api/users', require('./api/user')); app.use('/auth', require('./auth').default);