Skip to content

Commit

Permalink
Implement Get mentor categories endpoint (Public endpoint) (#68)
Browse files Browse the repository at this point in the history
Co-authored-by: Krishnadeva <[email protected]>
  • Loading branch information
sathudeva7 and Krishnadeva authored Oct 9, 2023
1 parent 6764e21 commit e6d2520
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<Express> => {
try {
Expand Down
20 changes: 20 additions & 0 deletions src/controllers/category.controller.ts
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 })
}
}
}
21 changes: 21 additions & 0 deletions src/routes/category/category.route.test.ts
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')
})
})
8 changes: 8 additions & 0 deletions src/routes/category/category.route.ts
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
33 changes: 33 additions & 0 deletions src/services/category.service.ts
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')
}
}

0 comments on commit e6d2520

Please sign in to comment.