diff --git a/.gitignore b/.gitignore index 19536cc..1b6fca8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ node_modules npm-debug.log src/public/dist/* .DS_Store -./idm \ No newline at end of file +./idm +.npmrc diff --git a/artifacts/abnormal-hyena.md b/artifacts/abnormal-hyena.md new file mode 100644 index 0000000..33e7213 --- /dev/null +++ b/artifacts/abnormal-hyena.md @@ -0,0 +1,20 @@ +# abnormal-hyena @hjbowers @zezemanolo +## Description +Working on Noob's Admin page(back end and front end). + +## Project Specs +- [ ] [Issue #91:](https://github.com/GuildCrafts/noob/issues/91) Create CRUD for tags +- [ ] [Issue #92:](https://github.com/GuildCrafts/noob/issues/92) Add Admin page UI for 'tags' CRUD + +## Quality +* Making sure all tests pass. +* Make sure all new code has tests. +* Test React components as well. + +* Commit messages are concise and descriptive +* Every pull request has a description summarizing the changes made. + +* Code is easily readable with descriptive variable names. +* No stray comments and console logs left in code. +* Formatting is accounted for (whitespace, etc.) +* All frontend code has basic styling diff --git a/src/database/migrations/20170306153436_tag.js b/src/database/migrations/20170306153436_tag.js new file mode 100644 index 0000000..5033f80 --- /dev/null +++ b/src/database/migrations/20170306153436_tag.js @@ -0,0 +1,17 @@ + +exports.up = function(knex, Promise) { + + return Promise.all([ + knex.schema.createTable('tag', function(table) { + table.increments('id').primary(); + table.text('names'); + }) + ]) +}; + +exports.down = function(knex, Promise) { + + return Promise.all([ + knex.schema.dropTable('tag') + ]) +}; diff --git a/src/database/queries/tag.js b/src/database/queries/tag.js new file mode 100644 index 0000000..6a3bf31 --- /dev/null +++ b/src/database/queries/tag.js @@ -0,0 +1,21 @@ +import knex from '../knex' +import * as _ from './utilities' + +const add = attributes => + _.createRecord( 'tag', attributes ) + +const deleteAll = () => + _.deleteAll( 'tag' ) + +const getAll = () => + _.findAll( 'tag' ) + .orderBy('names', 'asc') + +const getBy = ( column, data ) => + _.findAllWhere( 'tag', column, data ) + +const expunge = ( column, data ) => + _.deleteRecord( 'tag', column, data ) + + +export { add, deleteAll, getAll, getBy, expunge } diff --git a/src/routes/tag.js b/src/routes/tag.js new file mode 100644 index 0000000..b904264 --- /dev/null +++ b/src/routes/tag.js @@ -0,0 +1,33 @@ +import express from 'express' +import path from 'path' +import groupBy from 'lodash/groupBy' +import * as tag from '../database/queries/tag' +const router = express.Router() + +router.get('/', function(req, res, next){ + tag.getAll() + .then( results => { + const tags = groupBy(results, tag => tag.names) + res.json(tags) + }) +}) + +router.post('/', function(req, res, next){ + tag.add(req.body) + const newTag = { + names: req.body.names + } + tag.add(newTag) + .then(results => { + res.json(results[0]) + }) +}) + +router.delete('/:id', function(req, res, next){ + const {id} = req.params + tag.expunge('id', id).then(result => { + res.json({message: 'Successfully deleted the tag.'}); + }) +}) + +export default router diff --git a/tests/queries_tests/tag_test.js b/tests/queries_tests/tag_test.js new file mode 100644 index 0000000..470082f --- /dev/null +++ b/tests/queries_tests/tag_test.js @@ -0,0 +1,41 @@ +import chai, { expect } from 'chai' +import * as tag from '../../src/database/queries/tag' + +describe('tag', () => { + + const fakeTags = [ + { + names: 'Massage' + }, + { + names: 'Spa' + } + ] + + beforeEach( () => + Promise.all([ + tag.deleteAll(), + tag.add(fakeTags) + ]) + ) + + it('should exist', () => + expect(tag).to.be.a('object') + ) + + it('should return all tags ordered ascending by names', () => + tag.getAll().then( tags => { + expect( fakeTags[0].names ).to.equal('Massage') + expect( fakeTags[1].names ).to.equal('Spa') + }) + ) + + it('deletes a tag by id', () => + tag.expunge('id', 1).then( _ => + tag.getBy('id', 1).then( deletedTag => + expect(deletedTag).to.deep.equal([]) + ) + ) + ) + +})