-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(Course, Route): Register course route
- Loading branch information
1 parent
2f71bf4
commit cb6d515
Showing
6 changed files
with
64 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |