Skip to content

Commit

Permalink
merge delete and save picture controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
rmanguinho committed Aug 24, 2021
1 parent 4a2f47a commit 73a8a81
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 69 deletions.
16 changes: 0 additions & 16 deletions src/application/controllers/delete-picture.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/application/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './controller'
export * from './facebook-login'
export * from './delete-picture'
export * from './save-picture'
9 changes: 5 additions & 4 deletions src/application/controllers/save-picture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ import { HttpResponse, ok } from '@/application/helpers'
import { ValidationBuilder as Builder, Validator } from '@/application/validation'
import { ChangeProfilePicture } from '@/domain/use-cases'

type HttpRequest = { file: { buffer: Buffer, mimeType: string }, userId: string }
type Model = Error | { initials?: string, pictureUrl?: string }
type HttpRequest = { file?: { buffer: Buffer, mimeType: string }, userId: string }
type Model = { initials?: string, pictureUrl?: string }

export class SavePictureController extends Controller {
constructor (private readonly changeProfilePicture: ChangeProfilePicture) {
super()
}

override async perform ({ file, userId }: HttpRequest): Promise<HttpResponse<Model>> {
const data = await this.changeProfilePicture({ id: userId, file })
return ok(data)
const { initials, pictureUrl } = await this.changeProfilePicture({ id: userId, file })
return ok({ initials, pictureUrl })
}

override buildValidators ({ file }: HttpRequest): Validator[] {
if (file === undefined) return []
return [
...Builder.of({ value: file, fieldName: 'file' })
.required()
Expand Down
6 changes: 0 additions & 6 deletions src/main/factories/application/controllers/delete-picture.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/factories/application/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './facebook-login'
export * from './delete-picture'
export * from './save-picture'
6 changes: 6 additions & 0 deletions src/main/factories/application/controllers/save-picture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { makeChangeProfilePicture } from '@/main/factories/domain/use-cases'
import { SavePictureController } from '@/application/controllers'

export const makeSavePictureController = (): SavePictureController => {
return new SavePictureController(makeChangeProfilePicture())
}
4 changes: 2 additions & 2 deletions src/main/routes/user.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { adaptExpressRoute as adapt } from '@/main/adapters'
import { makeDeletePictureController } from '@/main/factories/application/controllers'
import { makeSavePictureController } from '@/main/factories/application/controllers'
import { auth } from '@/main/middlewares'

import { Router } from 'express'

export default (router: Router): void => {
router.delete('/users/picture', auth, adapt(makeDeletePictureController()))
router.delete('/users/picture', auth, adapt(makeSavePictureController()))
}
34 changes: 0 additions & 34 deletions tests/application/controllers/delete-picture.spec.ts

This file was deleted.

8 changes: 7 additions & 1 deletion tests/application/controllers/save-picture.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('SavePictureController', () => {
expect(sut).toBeInstanceOf(Controller)
})

it('should build Validators correctly', async () => {
it('should build Validators correctly on save', async () => {
const validators = sut.buildValidators({ file, userId })

expect(validators).toEqual([
Expand All @@ -36,6 +36,12 @@ describe('SavePictureController', () => {
])
})

it('should build Validators correctly on delete', async () => {
const validators = sut.buildValidators({ file: undefined, userId })

expect(validators).toEqual([])
})

it('should call ChangeProfilePicture with correct input', async () => {
await sut.handle({ file, userId })

Expand Down
8 changes: 4 additions & 4 deletions tests/main/routes/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ describe('User Routes', () => {
expect(status).toBe(403)
})

it('should return 204', async () => {
const { id } = await pgUserRepo.save({ email: 'any_email' })
it('should return 200 with valid data', async () => {
const { id } = await pgUserRepo.save({ email: 'any_email', name: 'Rodrigo manguinho' })
const authorization = sign({ key: id }, env.jwtSecret)

const { status, body } = await request(app)
.delete('/api/users/picture')
.set({ authorization })

expect(status).toBe(204)
expect(body).toEqual({})
expect(status).toBe(200)
expect(body).toEqual({ pictureUrl: undefined, initials: 'RM' })
})
})
})

0 comments on commit 73a8a81

Please sign in to comment.