Skip to content

Commit

Permalink
feat(kafka): implement fetch all categories
Browse files Browse the repository at this point in the history
- ensure all categories in the system are fetched

[Delivers #164143948]
  • Loading branch information
wombolo committed Feb 21, 2019
1 parent f8e44c7 commit 20dd420
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/controllers/ArtsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ class ArtsController {

const slugifiedTitle = slugify(title);

const checkCategory = await Category.findOne({ where: { id: 1 } });
if (!checkCategory) {
await Category.create({ categoryName: 'Architecture' });
}

const createArticle = await Art
.create({
artistId,
Expand Down
49 changes: 49 additions & 0 deletions src/controllers/CategoriesController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import models from '../db/models';
import Response from '../helpers/response';

const {
Category
} = models;

/** Arts Controller Class */
class CategoriesController {
/**
* @desc GET /api/v1/categories
* @param {object} req
* @param {object} res
* @memberof CategoriesController
* @returns {Object} All Articles
*/
static async getCategories(req, res) {
try {
const categories = await Category.findAndCountAll({
order: [
['categoryName', 'ASC'],
],
attributes: [
'id',
'categoryName'
]
});

const response = new Response(
'Ok',
200,
'All Categories',
{
categories
}
);
return res.status(response.code).json(response);
} catch (err) {
const response = new Response(
'Internal Server Error',
500,
`${err}`,
);
return res.status(response.code).json(response);
}
}
}

export default CategoriesController;
8 changes: 8 additions & 0 deletions src/routes/categoriesRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from 'express';
import CategoriesController from '../controllers/CategoriesController';

const categoriesRoute = express.Router();

categoriesRoute.get('/', CategoriesController.getCategories);

export default categoriesRoute;
2 changes: 2 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from 'express';
import swaggerJSDoc from 'swagger-jsdoc';
import swaggerUi from 'swagger-ui-express';
import artsRoute from './artsRoute';
import categoriesRoute from './categoriesRoute';
import authRouter from './authRouter';
import commentReaction from './commentReactionRouter';
import ratingRouter from './ratingRouter';
Expand All @@ -26,6 +27,7 @@ router.use(
);
router.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
router.use('/arts', artsRoute);
router.use('/categories', categoriesRoute);
router.use('/artsreport', reportRouter);
router.use('/arts/comments/', [commentRouter, commentReaction]);
router.use('/auth', socialRouter);
Expand Down
23 changes: 23 additions & 0 deletions test/controllers/categoriesControllerTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'babel-polyfill';
import chai from 'chai';
import chaiHttp from 'chai-http';
import { app } from '../../src/index';

chai.use(chaiHttp);
const { expect } = chai;


describe('Categories Endpoint API Test', () => {
describe('Categories GET REQUESTS', () => {
it('it should get all categories', (done) => {
chai.request(app)
.get('/api/v1/categories')
.end((err, res) => {
expect(res.body.messages).eql('All Categories');
expect(res.body.data).to.have.property('categories');
expect(res.status).to.equal(200);
done(err);
});
});
});
});

0 comments on commit 20dd420

Please sign in to comment.