Skip to content

Commit

Permalink
Feat(Instructor): Edit instructor details use case
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-Poffo committed Feb 12, 2024
1 parent ac062b2 commit 1771399
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@
### Users
- [ ] GET /users/:userId - Get user details
- [ ] POST /users - Register user
- [ ] PUT /users/:userId - Update user - make use case
- [ ] DELETE /users/:userId - Delete user
- [ ] PUT /users/:userId - Update user
- [ ] DELETE /users/:userId - Delete user - make use case

### Sessions
- [ ] POST /sessions - User authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface InstructorsRepository {
findByEmail: (email: string) => Promise<Instructor | null>
findByCpf: (cpf: string) => Promise<Instructor | null>
create: (instructor: Instructor) => Promise<Instructor>
save: (instructor: Instructor) => Promise<void>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error'
import { makeInstructor } from '../../../../../test/factories/make-instructor'
import { InMemoryInstructorRepository } from '../../../../../test/repositories/in-memory-instructors-repository'
import { EditInstructorDetailsUseCase } from './edit-instructor-details'

let inMemoryInstructorsRepository: InMemoryInstructorRepository
let sut: EditInstructorDetailsUseCase

describe('Edit instructor details use case', () => {
beforeEach(() => {
inMemoryInstructorsRepository = new InMemoryInstructorRepository()
sut = new EditInstructorDetailsUseCase(inMemoryInstructorsRepository)
})

it('should be able to edit instructor details', async () => {
const instructor = makeInstructor({
email: '[email protected]',
name: 'John Doe'
})
await inMemoryInstructorsRepository.create(instructor)

const result = await sut.exec({
email: '[email protected]',
instructorId: instructor.id.toString()
})

expect(result.isRight()).toBe(true)
expect(result.value).toMatchObject({
instructor: expect.objectContaining({
email: '[email protected]',
name: 'John Doe'
})
})
})

it('should not be able to edit a inexistent instructor', async () => {
const result = await sut.exec({
email: '[email protected]',
instructorId: 'inexistentInstructorId'
})

expect(result.isLeft()).toBe(true)
expect(result.value).toBeInstanceOf(ResourceNotFoundError)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 Instructor } from '../../enterprise/entities/instructor'
import { type InstructorsRepository } from '../repositories/instructors-repository'

interface EditInstructorDetailsUseCaseRequest {
email?: string
age?: number
summary?: string
profileImageKey?: string | null
bannerImageKey?: string | null
instructorId: string
}

type EditInstructorDetailsUseCaseResponse = Either<
ResourceNotFoundError,
{
instructor: Instructor
}
>

export class EditInstructorDetailsUseCase implements UseCase<EditInstructorDetailsUseCaseRequest, EditInstructorDetailsUseCaseResponse> {
constructor(
private readonly instructorsRepository: InstructorsRepository
) { }

async exec({
email,
age,
summary,
profileImageKey,
bannerImageKey,
instructorId
}: EditInstructorDetailsUseCaseRequest): Promise<EditInstructorDetailsUseCaseResponse> {
const instructor = await this.instructorsRepository.findById(instructorId)

if (!instructor) {
return left(new ResourceNotFoundError())
}

instructor.name = instructor.name
instructor.email = email ?? instructor.email
instructor.age = age ?? instructor.age
instructor.summary = summary ?? instructor.summary
instructor.profileImageKey = profileImageKey ?? instructor.profileImageKey
instructor.bannerImageKey = bannerImageKey ?? instructor.bannerImageKey

await this.instructorsRepository.save(instructor)

return right({
instructor
})
}
}
5 changes: 5 additions & 0 deletions test/repositories/in-memory-instructors-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ export class InMemoryInstructorRepository implements InstructorsRepository {
this.items.push(instructor)
return instructor
}

async save(instructor: Instructor): Promise<void> {
const instructorIndex = this.items.indexOf(instructor)
this.items[instructorIndex] = instructor
}
}

0 comments on commit 1771399

Please sign in to comment.