Skip to content

Commit

Permalink
feat(exercise): added endpoint to get exercise by exerciseid
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberboyanmol committed Aug 27, 2024
1 parent 4b6e0b2 commit b3fa32a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/middleware/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const ROUTE_CONFIG: RouteConfig[] = [
message: 'signup is not allowed in production'
},
{ path: '/api/v1/authenticate', allowedEnvs: new Set(['development', 'staging', 'production']), skipAuth: true }
// Add more routes as needed
]

export async function authMiddleware(c: Context, next: Next) {
Expand Down
61 changes: 61 additions & 0 deletions src/modules/exercises/controllers/exercise.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { z } from 'zod'
import { ExerciseService } from '../services/exercise.service'
import Exercise from '#infra/mongodb/models/exercises/exercise.schema.js'
import { ExerciseModel } from '../models/exercise.model'
import { HTTPException } from 'hono/http-exception'

export class ExerciseController implements Routes {
public controller: OpenAPIHono
Expand Down Expand Up @@ -280,5 +281,65 @@ export class ExerciseController implements Routes {
})
}
)

this.controller.openapi(
createRoute({
method: 'get',
path: '/exercises/{exerciseId}',
tags: ['Exercises'],
summary: 'Get exercise by ID',
description: 'Retrieves a specific exercise by its unique identifier.',
operationId: 'getExerciseById',
request: {
params: z.object({
exerciseId: z.string().openapi({
title: 'Exercise ID',
description: 'The unique identifier of the exercise to retrieve.',
type: 'string',
example: 'ztAa1RK',
default: 'ztAa1RK'
})
})
},
responses: {
200: {
description: 'Successful response with the exercise details.',
content: {
'application/json': {
schema: z.object({
success: z.boolean().openapi({
description: 'Indicates whether the request was successful.',
type: 'boolean',
example: true
}),
data: ExerciseModel.openapi({
description: 'The retrieved exercise details.'
})
})
}
}
},
404: {
description: 'Exercise not found'
},
500: {
description: 'Internal server error'
}
}
}),
async (ctx) => {
const exerciseId = ctx.req.param('exerciseId')
const exercise = await this.exerciseService.getExerciseById(exerciseId)

if (!exercise) {
throw new HTTPException(404, { message: 'Exercise not found' })
}

return ctx.json({
success: true,
data: exercise
})
}
)
}
}
7 changes: 7 additions & 0 deletions src/modules/exercises/services/exercise.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
GetAutoCompleteSuggestionsArgs,
GetAutoCompleteSuggestionsUseCase
} from '../use-cases/get-autocomplete-suggestions'
import { GetExerciseByIdUseCase } from '../use-cases/get-exercises-by-id'
import { GetExercisesArgs, GetExercisesUseCase } from '../use-cases/get-exercises/get-exercise.usecase'

export interface GetExerciseSerivceArgs {
Expand All @@ -15,10 +16,12 @@ export class ExerciseService {
private readonly createExerciseUseCase: CreateExerciseUseCase
private readonly getExercisesUseCase: GetExercisesUseCase
private readonly getAutoCompleteSuggestionsUseCase: GetAutoCompleteSuggestionsUseCase
private readonly getExerciseByIdUseCase: GetExerciseByIdUseCase
constructor(private readonly exerciseModel: IExerciseModel) {
this.createExerciseUseCase = new CreateExerciseUseCase(exerciseModel)
this.getExercisesUseCase = new GetExercisesUseCase(exerciseModel)
this.getAutoCompleteSuggestionsUseCase = new GetAutoCompleteSuggestionsUseCase(exerciseModel)
this.getExerciseByIdUseCase = new GetExerciseByIdUseCase(exerciseModel)
}

createExercise = (params: CreateExerciseArgs) => {
Expand All @@ -35,4 +38,8 @@ export class ExerciseService {
getAutoCompleteSuggestions = (params: GetAutoCompleteSuggestionsArgs) => {
return this.getAutoCompleteSuggestionsUseCase.execute(params)
}

getExerciseById = (exerciseId: string) => {
return this.getExerciseByIdUseCase.execute(exerciseId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IUseCase } from '#common/types/use-case.type.js'
import { IExerciseDoc, IExerciseModel } from '#infra/mongodb/models/exercises/exercise.entity.js'

export class GetExerciseByIdUseCase implements IUseCase<string, IExerciseDoc | null> {
constructor(private readonly exerciseModel: IExerciseModel) {}

async execute(exerciseId: string): Promise<IExerciseDoc | null> {
return this.exerciseModel.findOne({
exerciseId
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './get-exercise-by-id.usecase'

0 comments on commit b3fa32a

Please sign in to comment.