-
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.
- Loading branch information
1 parent
6d9f300
commit 8244750
Showing
5 changed files
with
108 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
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
63 changes: 63 additions & 0 deletions
63
src/domain/course-management/application/use-cases/delete-course.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,63 @@ | ||
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error' | ||
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 { DeleteCourseUseCase } from './delete-course' | ||
|
||
let inMemoryClassesRepository: InMemoryClassesRepository | ||
let inMemoryCourseTagsRepository: InMemoryCourseTagsRepository | ||
let inMemoryEnrollmentsRepository: InMemoryEnrollmentsRepository | ||
let inMemoryStudentsRepository: InMemoryStudentsRepository | ||
let inMemoryInstructorsRepository: InMemoryInstructorRepository | ||
let inMemoryModulesRepository: InMemoryModulesRepository | ||
let inMemoryCoursesRepository: InMemoryCoursesRepository | ||
let sut: DeleteCourseUseCase | ||
|
||
describe('Delete course 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 DeleteCourseUseCase(inMemoryCoursesRepository) | ||
}) | ||
|
||
it('should be able to delete a course', async () => { | ||
const course = makeCourse({ name: 'John Doe Course' }) | ||
await inMemoryCoursesRepository.create(course) | ||
|
||
const result = await sut.exec({ | ||
courseId: course.id.toString() | ||
}) | ||
|
||
expect(result.isRight()).toBe(true) | ||
expect(result.value).toMatchObject({ | ||
course: expect.objectContaining({ | ||
name: 'John Doe Course' | ||
}) | ||
}) | ||
expect(inMemoryCourseTagsRepository.items).toHaveLength(0) | ||
}) | ||
|
||
it('should not be able to delete a inexistent course', async () => { | ||
const result = await sut.exec({ | ||
courseId: 'inexistentCourseId' | ||
}) | ||
|
||
expect(result.isLeft()).toBe(true) | ||
expect(result.value).toBeInstanceOf(ResourceNotFoundError) | ||
}) | ||
}) |
38 changes: 38 additions & 0 deletions
38
src/domain/course-management/application/use-cases/delete-course.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,38 @@ | ||
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 Course } from '../../enterprise/entities/course' | ||
import { type CoursesRepository } from '../repositories/courses-repository' | ||
|
||
interface DeleteCourseUseCaseRequest { | ||
courseId: string | ||
} | ||
|
||
type DeleteCourseUseCaseResponse = Either< | ||
ResourceNotFoundError, | ||
{ | ||
course: Course | ||
} | ||
> | ||
|
||
export class DeleteCourseUseCase implements UseCase<DeleteCourseUseCaseRequest, DeleteCourseUseCaseResponse> { | ||
constructor( | ||
private readonly coursesRepository: CoursesRepository | ||
) { } | ||
|
||
async exec({ | ||
courseId | ||
}: DeleteCourseUseCaseRequest): Promise<DeleteCourseUseCaseResponse> { | ||
const course = await this.coursesRepository.findById(courseId) | ||
|
||
if (!course) { | ||
return left(new ResourceNotFoundError()) | ||
} | ||
|
||
await this.coursesRepository.delete(course) | ||
|
||
return right({ | ||
course | ||
}) | ||
} | ||
} |
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