Skip to content

Commit

Permalink
Merge pull request #58 from andela/feature/165144730/get-categories
Browse files Browse the repository at this point in the history
#165144730 Implements get article categories
  • Loading branch information
aanchirinah authored Apr 6, 2019
2 parents 0eb05bd + 366d77d commit ecbe1ed
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
28 changes: 28 additions & 0 deletions server/controllers/categoriesController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import models from '../models';
import logger from '../helpers/logger';
import Response from '../helpers/responseHelper';
import { STATUS } from '../helpers/constants';

const { ArticleCategory } = models;

/**
* This class represents an article category entity
* @class CategoriesController
*/
export default class CategoriesController {
/**
* This function gets all categories in the database
* @param {Object} request - This is the request object
* @param {Object} response - This is the response object
* @returns {Object} - This function returns a response object
*/
static async getAll(request, response) {
try {
const categories = await ArticleCategory.findAll({ raw: true });
return Response.send(response, STATUS.OK, categories, 'Successful');
} catch (e) {
logger.log(e);
return Response.send(response, STATUS.BAD_REQUEST, null, 'An error occurred', false);
}
}
}
22 changes: 22 additions & 0 deletions server/routes/categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import express from 'express';
import categoriesController from '../controllers/categoriesController';

const categoriesRouter = express.Router();

/**
* /api/v1/categories:
* get:
* tags:
* - Categories
* description: Gets all categories
* produces:
* - application/json
* responses:
* 200:
* description: Categories fetched successfully
* schema:
* type: object
*/
categoriesRouter.get('/', categoriesController.getAll);

export default categoriesRouter;
3 changes: 2 additions & 1 deletion server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import reportRoute from './reportArticle';
import comments from './comments';
import roles from './roles';
import stats from './stats';

import categoriesRouter from './categories';

const router = express.Router();

Expand Down Expand Up @@ -43,5 +43,6 @@ router.use('/articles', reportRoute);
router.use('/articles', comments);
router.use('/roles', roles);
router.use('/users', stats);
router.use('/categories', categoriesRouter);

export default router;
17 changes: 17 additions & 0 deletions test/integrations/routes/categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable no-unused-expressions */
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import app from '../../../server';

chai.use(chaiHttp);

describe('Categories tests', () => {
it('should get all the article categories', async () => {
const response = await chai
.request(app)
.get('/api/v1/categories');
expect(response).to.have.status(200);
expect(response.body).to.be.an('object');
expect(response.body.data).to.be.an('array');
});
});

0 comments on commit ecbe1ed

Please sign in to comment.