-
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(Class, Routes): Implement get class details route
- Loading branch information
1 parent
fc18648
commit c036c57
Showing
6 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/domain/course-management/application/use-cases/get-class-details.spec.ts
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,39 @@ | ||
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error' | ||
import { makeClass } from '../../../../../test/factories/make-class' | ||
import { InMemoryClassesRepository } from '../../../../../test/repositories/in-memory-classes-repository' | ||
import { GetClassDetailsUseCase } from './get-class-details' | ||
|
||
let inMemoryClassesRepository: InMemoryClassesRepository | ||
let sut: GetClassDetailsUseCase | ||
|
||
describe('Get module details use case', () => { | ||
beforeEach(() => { | ||
inMemoryClassesRepository = new InMemoryClassesRepository() | ||
sut = new GetClassDetailsUseCase(inMemoryClassesRepository) | ||
}) | ||
|
||
it('should be able to get class details', async () => { | ||
const classToFind = makeClass({ name: 'John Doe Class' }) | ||
await inMemoryClassesRepository.create(classToFind) | ||
|
||
const result = await sut.exec({ | ||
classId: classToFind.id.toString() | ||
}) | ||
|
||
expect(result.isRight()).toBe(true) | ||
expect(result.value).toMatchObject({ | ||
class: expect.objectContaining({ | ||
name: 'John Doe Class' | ||
}) | ||
}) | ||
}) | ||
|
||
it('should not be able to get class details of a inexistent class', async () => { | ||
const result = await sut.exec({ | ||
classId: 'inexistentClassId' | ||
}) | ||
|
||
expect(result.isLeft()).toBe(true) | ||
expect(result.value).toBeInstanceOf(ResourceNotFoundError) | ||
}) | ||
}) |
36 changes: 36 additions & 0 deletions
36
src/domain/course-management/application/use-cases/get-class-details.ts
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,36 @@ | ||
import { left, right, type Either } from '@/core/either' | ||
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error' | ||
import { type UseCase } from '@/core/use-cases/use-case' | ||
import { type Class } from '../../enterprise/entities/class' | ||
import { type ClassesRepository } from '../repositories/classes-repository' | ||
|
||
interface GetClassDetailsUseCaseRequest { | ||
classId: string | ||
} | ||
|
||
type GetClassDetailsUseCaseResponse = Either< | ||
ResourceNotFoundError, | ||
{ | ||
class: Class | ||
} | ||
> | ||
|
||
export class GetClassDetailsUseCase implements UseCase<GetClassDetailsUseCaseRequest, GetClassDetailsUseCaseResponse> { | ||
constructor( | ||
private readonly classesRepository: ClassesRepository | ||
) { } | ||
|
||
async exec({ | ||
classId | ||
}: GetClassDetailsUseCaseRequest): Promise<GetClassDetailsUseCaseResponse> { | ||
const classToFind = await this.classesRepository.findById(classId) | ||
|
||
if (!classToFind) { | ||
return left(new ResourceNotFoundError()) | ||
} | ||
|
||
return right({ | ||
class: classToFind | ||
}) | ||
} | ||
} |
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,38 @@ | ||
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error' | ||
import { makeClassMapper } from '@/infra/database/prisma/mappers/factories/make-class-mapper' | ||
import { makeGetClassDetailsUseCase } from '@/infra/use-cases/factories/make-get-class-details-use-case' | ||
import { type FastifyReply, type FastifyRequest } from 'fastify' | ||
import { z } from 'zod' | ||
import { ClassPresenter } from '../presenters/class-presenter' | ||
|
||
const getClassDetailsParamsSchema = z.object({ | ||
classId: z.string().uuid() | ||
}) | ||
|
||
export async function getClassDetailsController(request: FastifyRequest, reply: FastifyReply) { | ||
const { classId } = getClassDetailsParamsSchema.parse(request.params) | ||
|
||
const getClassDetailsUseCase = makeGetClassDetailsUseCase() | ||
|
||
const result = await getClassDetailsUseCase.exec({ | ||
classId | ||
}) | ||
|
||
if (result.isLeft()) { | ||
const error = result.value | ||
|
||
switch (error.constructor) { | ||
case ResourceNotFoundError: | ||
return await reply.status(404).send({ message: error.message }) | ||
default: | ||
return await reply.status(500).send({ message: error.message }) | ||
} | ||
} | ||
|
||
const classMapper = makeClassMapper() | ||
const classToSend = await classMapper.toPrisma(result.value.class) | ||
|
||
return await reply.status(200).send({ | ||
class: ClassPresenter.toHTTP(classToSend) | ||
}) | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/infra/use-cases/factories/make-get-class-details-use-case.ts
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,12 @@ | ||
import { GetClassDetailsUseCase } from '@/domain/course-management/application/use-cases/get-class-details' | ||
import { makePrismaClassesRepository } from '@/infra/database/prisma/repositories/factories/make-prisma-classes-repository' | ||
|
||
export function makeGetClassDetailsUseCase() { | ||
const prismaClassesRepository = makePrismaClassesRepository() | ||
|
||
const getClassDetailsUseCase = new GetClassDetailsUseCase( | ||
prismaClassesRepository | ||
) | ||
|
||
return getClassDetailsUseCase | ||
} |