Skip to content

Commit

Permalink
Implemented Update mentor categories endpoint (Admin) #42
Browse files Browse the repository at this point in the history
  • Loading branch information
Krishnadeva committed Oct 9, 2023
1 parent 99ccd6a commit 26a9a8a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/controllers/admin/category.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { Request, Response } from 'express'
import type Profile from '../../entities/profile.entity'
import { ProfileTypes } from '../../enums'
import { createCategory } from '../../services/admin/category.service'
import {
changeCategory,
createCategory
} from '../../services/admin/category.service'

export const addCategory = async (
req: Request,
Expand All @@ -25,3 +28,28 @@ export const addCategory = async (
res.status(500).json({ error: err })
}
}

export const updateCategory = async (
req: Request,
res: Response
): Promise<void> => {
try {
const user = req.user as Profile
const { categoryName } = req.body
const { categoryId } = req.params

if (user.type !== ProfileTypes.ADMIN) {
res.status(403).json({ message: 'Only Admins are allowed' })
} else {
const { category, statusCode, message } = await changeCategory(
categoryId,
categoryName
)

res.status(statusCode).json({ category, message })
}
} catch (err) {
console.error('Error executing query', err)
res.status(500).json({ error: err })
}
}
23 changes: 23 additions & 0 deletions src/routes/admin/category/category.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,27 @@ describe('Admin category routes', () => {
.send({ categoryName: 'Computer Science' })
.expect(403)
})

it('should update a category', async () => {
const response = await adminAgent
.put('/api/admin/categories/0058ab92-1c82-4af1-9f84-c60a3e922245')
.send({ categoryName: 'Science' })
.expect(201)

expect(response.body).toHaveProperty('category')
})

it('should return 404 when an invalid category id was provided', async () => {
await adminAgent
.put('/api/admin/categories/0058ab92-1c82-4af1-9f84-c60a3e922244')
.send({ categoryName: 'Computer Science' })
.expect(404)
})

it('should only allow admins to update a category', async () => {
await agent
.put('/api/admin/categories/0058ab92-1c82-4af1-9f84-c60a3e922245')
.send({ categoryName: 'Science' })
.expect(403)
})
})
6 changes: 5 additions & 1 deletion src/routes/admin/category/category.route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import express from 'express'
import { requireAuth } from '../../../controllers/auth.controller'
import { addCategory } from '../../../controllers/admin/category.controller'
import {
addCategory,
updateCategory
} from '../../../controllers/admin/category.controller'

const categoryRouter = express.Router()

categoryRouter.post('/', requireAuth, addCategory)
categoryRouter.put('/:categoryId', requireAuth, updateCategory)

export default categoryRouter
38 changes: 38 additions & 0 deletions src/services/admin/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,41 @@ export const createCategory = async (
throw new Error('Error creating category')
}
}

export const changeCategory = async (
categoryId: string,
categoryName: string
): Promise<{
statusCode: number
category?: Category | null
message: string
}> => {
try {
const categoryRepository = dataSource.getRepository(Category)

const category = await categoryRepository.findOne({
where: { uuid: categoryId }
})

if (!category) {
return {
statusCode: 404,
message: 'Category not found'
}
}

await categoryRepository.update(
{ uuid: categoryId },
{ category: categoryName }
)

return {
statusCode: 201,
category,
message: 'Category updated successfully'
}
} catch (err) {
console.error('Error updating category', err)
throw new Error('Error updating category')
}
}

0 comments on commit 26a9a8a

Please sign in to comment.