-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Get mentor categories endpoint (Public endpoint) (#68)
Co-authored-by: Krishnadeva <[email protected]>
- Loading branch information
1 parent
6764e21
commit e6d2520
Showing
5 changed files
with
84 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
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,20 @@ | ||
import type { Request, Response } from 'express' | ||
import { getAllCategories } from '../services/category.service' | ||
|
||
export const getCategories = async ( | ||
req: Request, | ||
res: Response | ||
): Promise<void> => { | ||
try { | ||
const { statusCode, categories, message } = await getAllCategories() | ||
|
||
res.status(statusCode).json({ categories, message }) | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
console.error('Error executing query', err) | ||
res | ||
.status(500) | ||
.json({ error: 'Internal server error', message: err.message }) | ||
} | ||
} | ||
} |
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,21 @@ | ||
import { startServer } from '../../app' | ||
import type { Express } from 'express' | ||
import supertest from 'supertest' | ||
|
||
const port = Math.floor(Math.random() * (9999 - 3000 + 1)) + 3000 | ||
|
||
let server: Express | ||
let userAgent: supertest.SuperAgentTest | ||
|
||
describe('Category route', () => { | ||
beforeAll(async () => { | ||
server = await startServer(port) | ||
userAgent = supertest.agent(server) | ||
}, 5000) | ||
|
||
it('should return all categories and a success message', async () => { | ||
const response = await userAgent.get(`/api/categories`).expect(200) | ||
|
||
expect(response.body).toHaveProperty('categories') | ||
}) | ||
}) |
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,8 @@ | ||
import express from 'express' | ||
import { getCategories } from '../../controllers/category.controller' | ||
|
||
const categoryRouter = express.Router() | ||
|
||
categoryRouter.get('/', getCategories) | ||
|
||
export default categoryRouter |
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,33 @@ | ||
import { dataSource } from '../configs/dbConfig' | ||
import Category from '../entities/category.entity' | ||
|
||
export const getAllCategories = async (): Promise<{ | ||
statusCode: number | ||
categories?: string[] | null | ||
message: string | ||
}> => { | ||
try { | ||
const categoryRepository = dataSource.getRepository(Category) | ||
const allCategories: Category[] = await categoryRepository.find({ | ||
select: ['category'] | ||
}) | ||
|
||
const categories = allCategories.map((category) => category.category) | ||
|
||
if (!categories) { | ||
return { | ||
statusCode: 404, | ||
message: 'Categories not found' | ||
} | ||
} | ||
|
||
return { | ||
statusCode: 200, | ||
categories, | ||
message: 'All Categories found' | ||
} | ||
} catch (err) { | ||
console.error('Error getting mentor', err) | ||
throw new Error('Error getting mentor') | ||
} | ||
} |