Skip to content

Commit

Permalink
Merge f74da3a into fb97d65
Browse files Browse the repository at this point in the history
  • Loading branch information
czarjulius committed Aug 14, 2019
2 parents fb97d65 + f74da3a commit 7c8e766
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/controllers/categoryController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Category } from '../db/models';

/**
* @description Category Controller
* @class CategoryController
*/
class CategoryController {

/**
* @description - Get all categories
* @static
* @async
* @param {object} req - request
* @param {object} res - response
* @returns {object} category
*
*/
static async getAllCategories(req, res) {
try {
const categories = await Category.findAll({});

return res.status(200).json({
status: 200,
message: 'All categories fetched successfully',
data: [
{
categories,

}
]
});
} catch (error) {
return res.status(500).json({
status: 500,
message: error.message
});
}
}
}

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

const router = express.Router();

router.get('/categories',
CategoryController.getAllCategories);

export default router;
2 changes: 2 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import tagRoute from './tagRoute';
import reportRoute from './reportRoute';
import articleRoute from './articleRoute';
import statRoute from './statRoute';
import categoryRoute from './categoryRoute';

const router = express.Router();

Expand All @@ -23,6 +24,7 @@ router.use('/api/v1', ratingRoute);
router.use('/api/v1', bookmarkRoute);
router.use('/api/v1', tagRoute);
router.use('/api/v1', reportRoute);
router.use('/api/v1', categoryRoute);

router.use('/api/v1', statRoute);

Expand Down
38 changes: 38 additions & 0 deletions test/category.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import chaiHttp from 'chai-http';
import chai, { expect } from 'chai';
import dotenv from 'dotenv';

import app from '../src/server';

dotenv.config();

chai.use(chaiHttp);

describe('Category Controller', () => {
describe('Testing category controller', () => {
const category = '/api/v1/categories/';
it(
'should get all categories',
async () => {
const response = await chai.request(app)
.get(category)
.send();
expect(response).to.be.an('object');
expect(response).to.have.status(200);
expect(response.body).to.have.property('message');
},
);

it(
'should not get category when nothing found',
async () => {
const response = await chai.request(app)
.get(`${category}artic`)
.send();
expect(response).to.be.an('object');
expect(response).to.have.status(404);
expect(response.body).to.have.property('errors');
},
);
});
});

0 comments on commit 7c8e766

Please sign in to comment.