-
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 #20 from Artur-Poffo/feat-get-course-with-modules
Feat(Course, Module): Get course details with their modules
- Loading branch information
Showing
6 changed files
with
141 additions
and
1 deletion.
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
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
64 changes: 64 additions & 0 deletions
64
src/domain/course-management/application/use-cases/get-course-with-modules.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,64 @@ | ||
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 { InMemoryEnrollmentsRepository } from '../../../../../test/repositories/in-memory-enrollments-repository' | ||
import { InMemoryInstructorRepository } from '../../../../../test/repositories/in-memory-instructors-repository' | ||
import { InMemoryModulesRepository } from '../../../../../test/repositories/in-memory-modules-repository' | ||
import { InMemoryStudentsRepository } from '../../../../../test/repositories/in-memory-students-repository' | ||
import { GetCourseWithModulesUseCase } from './get-course-with-modules' | ||
|
||
let inMemoryClassesRepository: InMemoryClassesRepository | ||
let inMemoryEnrollmentsRepository: InMemoryEnrollmentsRepository | ||
let inMemoryStudentsRepository: InMemoryStudentsRepository | ||
let inMemoryInstructorsRepository: InMemoryInstructorRepository | ||
let inMemoryModulesRepository: InMemoryModulesRepository | ||
let inMemoryCoursesRepository: InMemoryCoursesRepository | ||
let sut: GetCourseWithModulesUseCase | ||
|
||
describe('Get course details with modules use case', () => { | ||
beforeEach(() => { | ||
inMemoryClassesRepository = new InMemoryClassesRepository() | ||
inMemoryInstructorsRepository = new InMemoryInstructorRepository() | ||
inMemoryEnrollmentsRepository = new InMemoryEnrollmentsRepository() | ||
inMemoryStudentsRepository = new InMemoryStudentsRepository() | ||
|
||
inMemoryModulesRepository = new InMemoryModulesRepository(inMemoryClassesRepository) | ||
inMemoryCoursesRepository = new InMemoryCoursesRepository(inMemoryModulesRepository, inMemoryInstructorsRepository, inMemoryEnrollmentsRepository, inMemoryStudentsRepository) | ||
|
||
sut = new GetCourseWithModulesUseCase(inMemoryCoursesRepository) | ||
}) | ||
|
||
it('should be able to get course details with modules', 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 of a inexistent course', async () => { | ||
const result = await sut.exec({ | ||
courseId: 'inexistentCourseId' | ||
}) | ||
|
||
expect(result.isLeft()).toBe(true) | ||
expect(result.value).toBeInstanceOf(ResourceNotFoundError) | ||
}) | ||
}) |
42 changes: 42 additions & 0 deletions
42
src/domain/course-management/application/use-cases/get-course-with-modules.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,42 @@ | ||
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 CourseWithModulesDTO } from '../../enterprise/entities/dtos/course-with-modules' | ||
import { type CoursesRepository } from '../repositories/courses-repository' | ||
|
||
interface GetCourseWithModulesUseCaseRequest { | ||
courseId: string | ||
} | ||
|
||
type GetCourseWithModulesUseCaseResponse = Either< | ||
ResourceNotFoundError, | ||
{ | ||
courseWithModules: CourseWithModulesDTO | ||
} | ||
> | ||
|
||
export class GetCourseWithModulesUseCase implements UseCase<GetCourseWithModulesUseCaseRequest, GetCourseWithModulesUseCaseResponse> { | ||
constructor( | ||
private readonly coursesRepository: CoursesRepository | ||
) { } | ||
|
||
async exec({ | ||
courseId | ||
}: GetCourseWithModulesUseCaseRequest): Promise<GetCourseWithModulesUseCaseResponse> { | ||
const course = await this.coursesRepository.findById(courseId) | ||
|
||
if (!course) { | ||
return left(new ResourceNotFoundError()) | ||
} | ||
|
||
const courseWithTheirModules = await this.coursesRepository.findCourseWithSModulesById(course.id.toString()) | ||
|
||
if (!courseWithTheirModules) { | ||
return left(new ResourceNotFoundError()) | ||
} | ||
|
||
return right({ | ||
courseWithModules: courseWithTheirModules | ||
}) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/domain/course-management/enterprise/entities/dtos/course-with-modules.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,7 @@ | ||
import { type Module } from '../module' | ||
import { type CourseDTO } from './course' | ||
|
||
export interface CourseWithModulesDTO { | ||
course: CourseDTO | ||
modules: 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