-
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): Fetch recent courses use case
- Loading branch information
1 parent
cbfb65f
commit 6baf407
Showing
6 changed files
with
104 additions
and
2 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
62 changes: 62 additions & 0 deletions
62
src/domain/course-management/application/use-cases/fetch-recent-courses.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,62 @@ | ||
import { makeCourse } from '../../../../../test/factories/make-course' | ||
import { InMemoryClassesRepository } from '../../../../../test/repositories/in-memory-classes-repository' | ||
import { InMemoryCourseTagsRepository } from '../../../../../test/repositories/in-memory-course-tags-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 { FetchRecentCoursesUseCase } from './fetch-recent-courses' | ||
|
||
let inMemoryClassesRepository: InMemoryClassesRepository | ||
let inMemoryCourseTagsRepository: InMemoryCourseTagsRepository | ||
let inMemoryEnrollmentsRepository: InMemoryEnrollmentsRepository | ||
let inMemoryStudentsRepository: InMemoryStudentsRepository | ||
let inMemoryInstructorsRepository: InMemoryInstructorRepository | ||
let inMemoryModulesRepository: InMemoryModulesRepository | ||
let inMemoryCoursesRepository: InMemoryCoursesRepository | ||
let sut: FetchRecentCoursesUseCase | ||
|
||
describe('Fetch recent courses use case', () => { | ||
beforeEach(() => { | ||
inMemoryClassesRepository = new InMemoryClassesRepository() | ||
inMemoryCourseTagsRepository = new InMemoryCourseTagsRepository() | ||
inMemoryStudentsRepository = new InMemoryStudentsRepository() | ||
inMemoryInstructorsRepository = new InMemoryInstructorRepository() | ||
|
||
inMemoryModulesRepository = new InMemoryModulesRepository(inMemoryClassesRepository) | ||
|
||
inMemoryEnrollmentsRepository = new InMemoryEnrollmentsRepository( | ||
inMemoryClassesRepository, inMemoryModulesRepository, inMemoryStudentsRepository | ||
) | ||
inMemoryCoursesRepository = new InMemoryCoursesRepository(inMemoryModulesRepository, inMemoryInstructorsRepository, inMemoryEnrollmentsRepository, inMemoryStudentsRepository, inMemoryCourseTagsRepository) | ||
|
||
sut = new FetchRecentCoursesUseCase(inMemoryCoursesRepository) | ||
}) | ||
|
||
it('should be able to fetch recent courses correctly', async () => { | ||
const firstCourse = makeCourse({ name: 'John Doe Course 1', createdAt: new Date('2023-01-01') }) | ||
const secondCourse = makeCourse({ name: 'John Doe Course 2', createdAt: new Date('2023-01-02') }) | ||
|
||
await Promise.all([ | ||
inMemoryCoursesRepository.create(firstCourse), | ||
inMemoryCoursesRepository.create(secondCourse) | ||
]) | ||
|
||
const result = await sut.exec() | ||
|
||
console.log(result.value) | ||
|
||
expect(result.isRight()).toBe(true) | ||
expect(result.value).toMatchObject({ | ||
courses: [ | ||
expect.objectContaining({ | ||
name: 'John Doe Course 2' | ||
}), | ||
expect.objectContaining({ | ||
name: 'John Doe Course 1' | ||
}) | ||
] | ||
}) | ||
}) | ||
}) |
25 changes: 25 additions & 0 deletions
25
src/domain/course-management/application/use-cases/fetch-recent-courses.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,25 @@ | ||
import { right, type Either } from '@/core/either' | ||
import { type UseCase } from '@/core/use-cases/use-case' | ||
import { type Course } from '../../enterprise/entities/course' | ||
import { type CoursesRepository } from '../repositories/courses-repository' | ||
|
||
type FetchRecentCoursesUseCaseResponse = Either< | ||
null, | ||
{ | ||
courses: Course[] | ||
} | ||
> | ||
|
||
export class FetchRecentCoursesUseCase implements UseCase<null, FetchRecentCoursesUseCaseResponse> { | ||
constructor( | ||
private readonly coursesRepository: CoursesRepository | ||
) { } | ||
|
||
async exec(): Promise<FetchRecentCoursesUseCaseResponse> { | ||
const courses = await this.coursesRepository.findAll() | ||
|
||
return right({ | ||
courses | ||
}) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...omain/course-management/enterprise/entities/dtos/course-with-instructor-and-evaluation.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,8 @@ | ||
import { type CourseDTO } from './course' | ||
import { type InstructorDTO } from './instructor' | ||
|
||
export interface CourseWithInstructorAndEvaluation { | ||
course: CourseDTO | ||
instructor: InstructorDTO | ||
evaluationAverage: number | ||
} |
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