-
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.
Merge pull request #18 from Artur-Poffo/feat-get-course-with-modules-…
…and-classes Feat(Course, Module, Class): Get course with modules and classes details
- Loading branch information
Showing
25 changed files
with
216 additions
and
102 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
2 changes: 2 additions & 0 deletions
2
src/domain/course-management/application/repositories/courses-repository.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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { type Course } from '../../enterprise/entities/course' | ||
import { type CompleteCourseDTO } from '../../enterprise/entities/dtos/complete-course' | ||
import { type CourseWithStudentsDTO } from '../../enterprise/entities/dtos/course-with-students' | ||
import { type InstructorWithCoursesDTO } from '../../enterprise/entities/dtos/instructor-with-courses' | ||
|
||
export interface CoursesRepository { | ||
findById: (id: string) => Promise<Course | null> | ||
findManyByInstructorId: (instructorId: string) => Promise<Course[]> | ||
findCourseWithStudentsById: (id: string) => Promise<CourseWithStudentsDTO | null> | ||
findCompleteCourseEntityById: (id: string) => Promise<CompleteCourseDTO | null> | ||
findInstructorWithCoursesByInstructorId: (instructorId: string) => Promise<InstructorWithCoursesDTO | null> | ||
create: (course: Course) => Promise<Course> | ||
} |
2 changes: 0 additions & 2 deletions
2
src/domain/course-management/application/repositories/instructors-repository.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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import { type Instructor } from '../../enterprise/entities/instructor' | ||
import { type InstructorWithCoursesDTO } from './../../enterprise/entities/dtos/instructor-with-courses' | ||
|
||
export interface InstructorsRepository { | ||
findById: (id: string) => Promise<Instructor | null> | ||
findByEmail: (email: string) => Promise<Instructor | null> | ||
findByCpf: (cpf: string) => Promise<Instructor | null> | ||
findInstructorWithCoursesById: (id: string) => Promise<InstructorWithCoursesDTO | null> | ||
create: (instructor: Instructor) => Promise<Instructor> | ||
} |
2 changes: 2 additions & 0 deletions
2
src/domain/course-management/application/repositories/modules-repository.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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { type Class } from '../../enterprise/entities/class' | ||
import { type Module } from '../../enterprise/entities/module' | ||
import { type ModuleWithClassesDTO } from './../../enterprise/entities/dtos/module-with-classes' | ||
|
||
export interface ModulesRepository { | ||
findById: (id: string) => Promise<Module | null> | ||
findManyByCourseId: (id: string) => Promise<Module[]> | ||
findManyClassesByCourseId: (courseId: string) => Promise<Class[] | null> | ||
findModuleWithClassesById: (id: string) => Promise<ModuleWithClassesDTO | null> | ||
create: (module: Module) => Promise<Module> | ||
} |
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
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
58 changes: 58 additions & 0 deletions
58
...omain/course-management/application/use-cases/get-course-with-modules-and-classes.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,58 @@ | ||
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error' | ||
import { makeCourse } from '../../../../../test/factories/make-course' | ||
import { makeInstructor } from '../../../../../test/factories/make-instructor' | ||
import { makeModule } from '../../../../../test/factories/make-module' | ||
import { InMemoryClassesRepository } from '../../../../../test/repositories/in-memory-classes-repository' | ||
import { InMemoryCoursesRepository } from '../../../../../test/repositories/in-memory-courses-repository' | ||
import { InMemoryInstructorRepository } from '../../../../../test/repositories/in-memory-instructors-repository' | ||
import { InMemoryModulesRepository } from '../../../../../test/repositories/in-memory-modules-repository' | ||
import { GetCourseWithModulesAndClassesUseCase } from './get-course-with-modules-and-classes' | ||
|
||
let inMemoryClassesRepository: InMemoryClassesRepository | ||
let inMemoryInstructorsRepository: InMemoryInstructorRepository | ||
let inMemoryModulesRepository: InMemoryModulesRepository | ||
let inMemoryCoursesRepository: InMemoryCoursesRepository | ||
let sut: GetCourseWithModulesAndClassesUseCase | ||
|
||
describe('Get course details with modules and classes use case', () => { | ||
beforeEach(() => { | ||
inMemoryClassesRepository = new InMemoryClassesRepository() | ||
inMemoryInstructorsRepository = new InMemoryInstructorRepository() | ||
|
||
inMemoryModulesRepository = new InMemoryModulesRepository(inMemoryClassesRepository) | ||
inMemoryCoursesRepository = new InMemoryCoursesRepository(inMemoryModulesRepository, inMemoryInstructorsRepository) | ||
|
||
sut = new GetCourseWithModulesAndClassesUseCase(inMemoryCoursesRepository) | ||
}) | ||
|
||
it('should be able to get course details with modules and classes', async () => { | ||
const instructor = makeInstructor() | ||
await inMemoryInstructorsRepository.create(instructor) | ||
|
||
const course = makeCourse({ name: 'John Doe Course', instructorId: instructor.id }) | ||
await inMemoryCoursesRepository.create(course) | ||
|
||
const firstModule = makeModule({ name: 'First Module', courseId: course.id }) | ||
const secondModule = makeModule({ name: 'Second Module', courseId: course.id }) | ||
|
||
await Promise.all([ | ||
inMemoryModulesRepository.create(firstModule), | ||
inMemoryModulesRepository.create(secondModule) | ||
]) | ||
|
||
const result = await sut.exec({ | ||
courseId: course.id.toString() | ||
}) | ||
|
||
expect(result.isRight()).toBe(true) | ||
}) | ||
|
||
it('should not be able to get course details with modules and classes of a inexistent course', async () => { | ||
const result = await sut.exec({ | ||
courseId: 'inexistentCourseId' | ||
}) | ||
|
||
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-course-with-modules-and-classes.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 CompleteCourseDTO } from '../../enterprise/entities/dtos/complete-course' | ||
import { type CoursesRepository } from '../repositories/courses-repository' | ||
|
||
interface GetCourseWithModulesAndClassesUseCaseRequest { | ||
courseId: string | ||
} | ||
|
||
type GetCourseWithModulesAndClassesUseCaseResponse = Either< | ||
ResourceNotFoundError, | ||
{ | ||
course: CompleteCourseDTO | ||
} | ||
> | ||
|
||
export class GetCourseWithModulesAndClassesUseCase implements UseCase<GetCourseWithModulesAndClassesUseCaseRequest, GetCourseWithModulesAndClassesUseCaseResponse> { | ||
constructor( | ||
private readonly coursesRepository: CoursesRepository | ||
) { } | ||
|
||
async exec({ | ||
courseId | ||
}: GetCourseWithModulesAndClassesUseCaseRequest): Promise<GetCourseWithModulesAndClassesUseCaseResponse> { | ||
const courseWithModulesAndClasses = await this.coursesRepository.findCompleteCourseEntityById(courseId) | ||
|
||
if (!courseWithModulesAndClasses) { | ||
return left(new ResourceNotFoundError()) | ||
} | ||
|
||
return right({ | ||
course: courseWithModulesAndClasses | ||
}) | ||
} | ||
} |
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
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
Oops, something went wrong.