Skip to content

Commit

Permalink
Paginate routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmabulage authored Aug 26, 2024
1 parent bf901eb commit 3130cf7
Show file tree
Hide file tree
Showing 22 changed files with 404 additions and 189 deletions.
47 changes: 36 additions & 11 deletions src/controllers/admin/mentee.controller.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import type { Request, Response } from 'express'
import type Mentee from '../../entities/mentee.entity'
import type Profile from '../../entities/profile.entity'
import {
MenteeApplicationStatus,
ProfileTypes,
StatusUpdatedBy
} from '../../enums'
import type Profile from '../../entities/profile.entity'
import type Mentee from '../../entities/mentee.entity'
import type { ApiResponse } from '../../types'
import {
getAllMenteeEmailsService,
getAllMentees,
getMentee,
updateStatus
} from '../../services/admin/mentee.service'
import type { ApiResponse, PaginatedApiResponse } from '../../types'

export const getMentees = async (
req: Request,
res: Response
): Promise<ApiResponse<Mentee[]>> => {
): Promise<ApiResponse<PaginatedApiResponse<Mentee>>> => {
try {
const pageNumber = parseInt(req.query.pageNumber as string)
const pageSize = parseInt(req.query.pageSize as string)

const user = req.user as Profile
const status: MenteeApplicationStatus | undefined = req.query.status as
| MenteeApplicationStatus
Expand All @@ -32,8 +35,18 @@ export const getMentees = async (
return res.status(400).json({ message: 'Please provide a valid status' })
}

const { mentees, statusCode, message } = await getAllMentees(status)
return res.status(statusCode).json({ mentees, message })
const { items, totalItemCount, statusCode, message } = await getAllMentees({
status,
pageNumber,
pageSize
})
return res.status(statusCode).json({
items,
totalItemCount,
pageNumber,
pageSize,
message
})
} catch (err) {
if (err instanceof Error) {
console.error('Error executing query', err)
Expand Down Expand Up @@ -103,16 +116,28 @@ export const getMenteeDetails = async (
export const getAllMenteeEmails = async (
req: Request,
res: Response
): Promise<ApiResponse<string[]>> => {
): Promise<ApiResponse<PaginatedApiResponse<string>>> => {
try {
const pageNumber = parseInt(req.query.pageNumber as string)
const pageSize = parseInt(req.query.pageSize as string)

const status: MenteeApplicationStatus | undefined = req.query.status as
| MenteeApplicationStatus
| undefined
if (status && status.toUpperCase() in MenteeApplicationStatus) {
const { emails, statusCode, message } = await getAllMenteeEmailsService(
status
)
return res.status(statusCode).json({ emails, message })
const { items, statusCode, message, totalItemCount } =
await getAllMenteeEmailsService({
status,
pageNumber,
pageSize
})
return res.status(statusCode).json({
items,
pageNumber,
pageSize,
totalItemCount,
message
})
} else {
return res.status(400).json({ message: 'Invalid Status' })
}
Expand Down
31 changes: 22 additions & 9 deletions src/controllers/admin/mentor.controller.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { Request, Response } from 'express'
import type Mentor from '../../entities/mentor.entity'
import type Profile from '../../entities/profile.entity'
import { MentorApplicationStatus, ProfileTypes } from '../../enums'
import {
findAllMentorEmails,
getAllMentors,
updateMentorStatus,
getMentor
getMentor,
updateMentorStatus
} from '../../services/admin/mentor.service'
import { MentorApplicationStatus, ProfileTypes } from '../../enums'
import type Profile from '../../entities/profile.entity'
import type Mentor from '../../entities/mentor.entity'
import type { ApiResponse } from '../../types'
import {
searchMentorsByQuery,
updateAvailability
} from '../../services/mentor.service'
import type { ApiResponse, PaginatedApiResponse } from '../../types'

export const mentorStatusHandler = async (
req: Request,
Expand Down Expand Up @@ -51,8 +51,11 @@ export const mentorStatusHandler = async (
export const getAllMentorsByStatus = async (
req: Request,
res: Response
): Promise<ApiResponse<Mentor>> => {
): Promise<ApiResponse<PaginatedApiResponse<Mentor>>> => {
try {
const pageNumber = parseInt(req.query.pageNumber as string)
const pageSize = parseInt(req.query.pageSize as string)

const user = req.user as Profile
const status: MentorApplicationStatus | undefined = req.query.status as
| MentorApplicationStatus
Expand All @@ -66,8 +69,18 @@ export const getAllMentorsByStatus = async (
return res.status(400).json({ message: 'Please provide a valid status' })
}

const { mentors, statusCode, message } = await getAllMentors(status)
return res.status(statusCode).json({ mentors, message })
const { items, totalItemCount, statusCode, message } = await getAllMentors({
status,
pageNumber,
pageSize
})
return res.status(statusCode).json({
pageNumber,
pageSize,
totalItemCount,
items,
message
})
} catch (err) {
if (err instanceof Error) {
console.error('Error executing query', err)
Expand Down
25 changes: 17 additions & 8 deletions src/controllers/admin/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
import type { Request, Response } from 'express'
import { getAllUsers } from '../../services/admin/user.service'
import type Profile from '../../entities/profile.entity'
import { ProfileTypes } from '../../enums'
import type { ApiResponse } from '../../types'
import { getAllUsers } from '../../services/admin/user.service'
import type { PaginatedApiResponse } from '../../types'

export const getAllUsersHandler = async (
req: Request,
res: Response
): Promise<ApiResponse<Profile>> => {
): Promise<Response<PaginatedApiResponse<Profile>>> => {
try {
const user = req.user as Profile

const pageNumber = parseInt(req.query.pageNumber as string)
const pageSize = parseInt(req.query.pageSize as string)

if (user.type !== ProfileTypes.ADMIN) {
return res.status(403).json({ message: 'Only Admins are allowed' })
}

const users = await getAllUsers()
if (users?.length === 0) {
return res.status(404).json({ message: 'No users available' })
}
const { items, message, statusCode, totalItemCount } = await getAllUsers({
pageNumber,
pageSize
})

return res.status(200).json({ profiles: users })
return res.status(statusCode).json({
items,
message,
pageNumber,
pageSize,
totalItemCount
})
} catch (err) {
console.error('Error executing query', err)
return res.status(500).json({ error: err })
Expand Down
20 changes: 15 additions & 5 deletions src/controllers/category.controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import type { Request, Response } from 'express'
import { getAllCategories } from '../services/category.service'
import type { ApiResponse } from '../types'
import type Category from '../entities/category.entity'
import { getAllCategories } from '../services/category.service'
import { type PaginatedApiResponse } from '../types'

export const getCategories = async (
req: Request,
res: Response
): Promise<ApiResponse<Category>> => {
): Promise<Response<PaginatedApiResponse<Category>>> => {
try {
const { statusCode, categories, message } = await getAllCategories()
const pageNumber = parseInt(req.query.pageNumber as string)
const pageSize = parseInt(req.query.pageSize as string)

const { statusCode, items, totalItemCount, message } =
await getAllCategories(pageNumber, pageSize)

return res.status(statusCode).json({ categories, message })
return res.status(statusCode).json({
pageNumber,
pageSize,
totalItemCount,
items,
message
})
} catch (err) {
if (err instanceof Error) {
console.error('Error executing query', err)
Expand Down
37 changes: 25 additions & 12 deletions src/controllers/mentor.controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { Request, Response } from 'express'
import type Mentee from '../entities/mentee.entity'
import type Mentor from '../entities/mentor.entity'
import type Profile from '../entities/profile.entity'
import { MenteeApplicationStatus } from '../enums'
import { getAllMenteesByMentor } from '../services/admin/mentee.service'
import {
createMentor,
updateAvailability,
getAllMentors,
getMentor,
getAllMentors
updateAvailability
} from '../services/mentor.service'
import type Profile from '../entities/profile.entity'
import type Mentor from '../entities/mentor.entity'
import type { ApiResponse } from '../types'
import type Mentee from '../entities/mentee.entity'
import { MenteeApplicationStatus } from '../enums'
import { getAllMenteesByMentor } from '../services/admin/mentee.service'
import type { ApiResponse, PaginatedApiResponse } from '../types'

export const mentorApplicationHandler = async (
req: Request,
Expand Down Expand Up @@ -93,12 +93,25 @@ export const mentorDetailsHandler = async (
export const getAllMentorsHandler = async (
req: Request,
res: Response
): Promise<ApiResponse<Mentor>> => {
): Promise<ApiResponse<PaginatedApiResponse<Mentor>>> => {
try {
const categoryId: string = req.query.categoryId as string
const { mentors, statusCode, message } = await getAllMentors(categoryId)
const pageNumber = parseInt(req.query.pageNumber as string)
const pageSize = parseInt(req.query.pageSize as string)

return res.status(statusCode).json({ mentors, message })
const categoryId: string = req.query.categoryId as string
const { items, totalItemCount, statusCode, message } = await getAllMentors({
categoryId,
pageNumber,
pageSize
})

return res.status(statusCode).json({
pageNumber,
pageSize,
totalItemCount,
items,
message
})
} catch (err) {
if (err instanceof Error) {
console.error('Error executing query', err)
Expand Down
13 changes: 10 additions & 3 deletions src/routes/admin/mentee/mentee.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@ import {
getMenteesSchema,
updateMenteeStatusSchema
} from '../../../schemas/admin/admin.mentee-routes.schema'
import { paginationSchema } from '../../../schemas/common/pagination-request.schema'

const menteeRouter = express.Router()

menteeRouter.get(
'/emails/',
[requireAuth, requestQueryValidator(getAllMenteeEmailsSchema)],
'/emails',
[
requireAuth,
requestQueryValidator(getAllMenteeEmailsSchema.merge(paginationSchema))
],
getAllMenteeEmails
)
menteeRouter.get(
'/applications',
[requireAuth, requestQueryValidator(getMenteesSchema)],
[
requireAuth,
requestQueryValidator(getMenteesSchema.merge(paginationSchema))
],
getMentees
)
menteeRouter.get('/:menteeId', requireAuth, getMenteeDetails)
Expand Down
8 changes: 6 additions & 2 deletions src/routes/admin/mentor/mentor.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
searchMentorsSchema,
updateMentorAvailabilitySchema
} from '../../../schemas/admin/admin.mentor-routes.schema'
import { paginationSchema } from '../../../schemas/common/pagination-request.schema'

const mentorRouter = express.Router()

Expand All @@ -27,17 +28,20 @@ mentorRouter.put(
[requireAuth, requestBodyValidator(mentorStatusSchema)],
mentorStatusHandler
)
mentorRouter.get('/:mentorId', requireAuth, mentorDetailsHandler)
mentorRouter.get(
'/',
[requireAuth, requestQueryValidator(getAllMentorsByStatusSchema)],
[
requireAuth,
requestQueryValidator(getAllMentorsByStatusSchema.merge(paginationSchema))
],
getAllMentorsByStatus
)
mentorRouter.get(
'/emails',
[requireAuth, requestQueryValidator(getAllMentorEmailsSchema)],
getAllMentorEmails
)
mentorRouter.get('/:mentorId', requireAuth, mentorDetailsHandler)
mentorRouter.put(
'/:mentorId/availability',
[requireAuth, requestBodyValidator(updateMentorAvailabilitySchema)],
Expand Down
8 changes: 7 additions & 1 deletion src/routes/admin/user/user.route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express'
import { getAllUsersHandler } from '../../../controllers/admin/user.controller'
import { requireAuth } from '../../../controllers/auth.controller'
import { requestQueryValidator } from '../../../middlewares/requestValidator'
import { paginationSchema } from '../../../schemas/common/pagination-request.schema'

const userRouter = express.Router()

userRouter.get('/', requireAuth, getAllUsersHandler)
userRouter.get(
'/',
[requireAuth, requestQueryValidator(paginationSchema)],
getAllUsersHandler
)

export default userRouter
4 changes: 3 additions & 1 deletion src/routes/category/category.route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import express from 'express'
import { getCategories } from '../../controllers/category.controller'
import { requestQueryValidator } from '../../middlewares/requestValidator'
import { paginationSchema } from '../../schemas/common/pagination-request.schema'

const categoryRouter = express.Router()

categoryRouter.get('/', getCategories)
categoryRouter.get('/', requestQueryValidator(paginationSchema), getCategories)

export default categoryRouter
7 changes: 6 additions & 1 deletion src/routes/mentor/mentor.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
requestBodyValidator,
requestQueryValidator
} from '../../middlewares/requestValidator'
import { paginationSchema } from '../../schemas/common/pagination-request.schema'
import {
getMenteesByMentorSchema,
mentorApplicationSchema
Expand Down Expand Up @@ -34,6 +35,10 @@ mentorRouter.put(
mentorAvailabilityHandler
)
mentorRouter.get('/:mentorId', mentorDetailsHandler)
mentorRouter.get('/', getAllMentorsHandler)
mentorRouter.get(
'/',
requestQueryValidator(paginationSchema),
getAllMentorsHandler
)

export default mentorRouter
8 changes: 8 additions & 0 deletions src/schemas/common/pagination-request.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from 'zod'

export const paginationSchema = z.object({
pageNumber: z.coerce.number().int().positive(),
pageSize: z.coerce.number().int().positive().max(100)
})

export type PaginationRequest = z.infer<typeof paginationSchema>
Loading

0 comments on commit 3130cf7

Please sign in to comment.