Skip to content

Commit

Permalink
Feat(Course, Route): Register course route
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-Poffo committed Feb 20, 2024
1 parent 2f71bf4 commit cb6d515
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/infra/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fastify from 'fastify'
import { readFileSync } from 'fs'
import { ZodError } from 'zod'
import { env } from './env'
import { courseRoutes } from './http/routes/course'
import { userRoutes } from './http/routes/user'

export const app = fastify()
Expand Down Expand Up @@ -36,6 +37,7 @@ app.register(fastifyCookie)
// API Routes

app.register(userRoutes)
app.register(courseRoutes)

// Custom error handler

Expand Down
2 changes: 0 additions & 2 deletions src/infra/http/controllers/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export async function authenticateUserController(request: FastifyRequest, reply:

const { accessToken } = result.value

console.log(accessToken)

return await
reply
.status(200)
Expand Down
37 changes: 37 additions & 0 deletions src/infra/http/controllers/register-course.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error'
import { CourseAlreadyExistsInThisAccountError } from '@/domain/course-management/application/use-cases/errors/course-already-exists-in-this-account-error'
import { makeRegisterCourseUseCase } from '@/infra/use-cases/factories/make-register-course-use-case'
import { type FastifyReply, type FastifyRequest } from 'fastify'
import { z } from 'zod'

const registerCourseBodySchema = z.object({
name: z.string(),
description: z.string()
})

export async function registerCourseController(request: FastifyRequest, reply: FastifyReply) {
const { name, description } = registerCourseBodySchema.parse(request.body)

const registerCourseUseCase = makeRegisterCourseUseCase()

const result = await registerCourseUseCase.exec({
name,
description,
instructorId: request.user.sub
})

if (result.isLeft()) {
const error = result.value

switch (error.constructor) {
case ResourceNotFoundError:
return await reply.status(404).send({ message: error.message })
case CourseAlreadyExistsInThisAccountError:
return await reply.status(409).send({ message: error.message })
default:
return await reply.status(500).send({ message: error.message })
}
}

return await reply.status(201).send()
}
17 changes: 2 additions & 15 deletions src/infra/http/controllers/register-user.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { InstructorAlreadyExistsError } from '@/domain/course-management/application/use-cases/errors/instructor-already-exists-error'
import { StudentAlreadyExistsError } from '@/domain/course-management/application/use-cases/errors/student-already-exists-error'
import { makeInstructorMapper } from '@/infra/database/prisma/mappers/factories/make-instructor-mapper'
import { makeStudentMapper } from '@/infra/database/prisma/mappers/factories/make-student-mapper'
import { makeRegisterInstructorUseCase } from '@/infra/use-cases/factories/make-register-instructor-use-case'
import { makeRegisterStudentUseCase } from '@/infra/use-cases/factories/make-register-student-use-case'
import { type FastifyReply, type FastifyRequest } from 'fastify'
import { z } from 'zod'
import { UserPresenter } from '../presenters/user-presenter'

const registerUserBodySchema = z.object({
name: z.string(),
Expand Down Expand Up @@ -44,12 +41,7 @@ export async function registerUserController(request: FastifyRequest, reply: Fas
}
}

const studentMapper = makeStudentMapper()
const user = await studentMapper.toPrisma(result.value.student)

return await reply.status(201).send({
user: UserPresenter.toHTTP(user)
})
return await reply.status(201).send()
}

if (role === 'INSTRUCTOR') {
Expand All @@ -75,11 +67,6 @@ export async function registerUserController(request: FastifyRequest, reply: Fas
}
}

const instructorMapper = makeInstructorMapper()
const user = await instructorMapper.toPrisma(result.value.instructor)

return await reply.status(201).send({
user: UserPresenter.toHTTP(user)
})
return await reply.status(201).send()
}
}
15 changes: 15 additions & 0 deletions src/infra/http/presenters/course-presenter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { type Prisma } from '@prisma/client'

export class CoursePresenter {
static toHTTP(course: Prisma.CourseUncheckedCreateInput) {
return {
id: course.id,
name: course.name,
description: course.description,
coverImageKey: course.coverImageKey,
bannerImageKey: course.bannerImageKey,
createdAt: course.createdAt,
instructorId: course.instructorId
}
}
}
8 changes: 8 additions & 0 deletions src/infra/http/routes/course.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { type FastifyInstance } from 'fastify'
import { registerCourseController } from '../controllers/register-course'
import { verifyJwt } from '../middlewares/verify-jwt'
import { verifyUserRole } from '../middlewares/verify-user-role'

export async function courseRoutes(app: FastifyInstance) {
app.post('/courses', { onRequest: [verifyJwt, verifyUserRole('INSTRUCTOR')] }, registerCourseController)
}

0 comments on commit cb6d515

Please sign in to comment.