diff --git a/src/app.ts b/src/app.ts index 17f92196..a5336096 100644 --- a/src/app.ts +++ b/src/app.ts @@ -7,6 +7,7 @@ import authRouter from './routes/auth/auth.route' import profileRouter from './routes/profile/profile.route' import adminRouter from './routes/admin/admin.route' import mentorRouter from './routes/mentor/mentor.route' +import categoryRouter from './routes/category/category.route' import passport from 'passport' import './configs/passport' import cookieParser from 'cookie-parser' @@ -26,6 +27,7 @@ app.use('/api/auth', authRouter) app.use('/api/me', profileRouter) app.use('/api/admin', adminRouter) app.use('/api/mentors', mentorRouter) +app.use('/api/categories', categoryRouter) export const startServer = async (port: number): Promise => { try { diff --git a/src/controllers/category.controller.ts b/src/controllers/category.controller.ts new file mode 100644 index 00000000..cfb6889b --- /dev/null +++ b/src/controllers/category.controller.ts @@ -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 => { + 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 }) + } + } +} diff --git a/src/routes/category/category.route.test.ts b/src/routes/category/category.route.test.ts new file mode 100644 index 00000000..d3617be8 --- /dev/null +++ b/src/routes/category/category.route.test.ts @@ -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') + }) +}) diff --git a/src/routes/category/category.route.ts b/src/routes/category/category.route.ts new file mode 100644 index 00000000..3c9a95e3 --- /dev/null +++ b/src/routes/category/category.route.ts @@ -0,0 +1,8 @@ +import express from 'express' +import { getCategories } from '../../controllers/category.controller' + +const categoryRouter = express.Router() + +categoryRouter.get('/', getCategories) + +export default categoryRouter diff --git a/src/services/category.service.ts b/src/services/category.service.ts new file mode 100644 index 00000000..a451aaf9 --- /dev/null +++ b/src/services/category.service.ts @@ -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') + } +}