-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from andela/feature/165144730/get-categories
#165144730 Implements get article categories
- Loading branch information
Showing
4 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |