-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
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,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; |
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,9 @@ | ||
import express from 'express'; | ||
import CategoryController from '../controllers/categoryController'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/categories', | ||
CategoryController.getAllCategories); | ||
|
||
export default router; |
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,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'); | ||
}, | ||
); | ||
}); | ||
}); |