import { badRequest , HttpResponse , serverError } from '@/application/helpers' ; import { ValidationComposite , Validator } from '@/application/validation' ; export abstract class Controller { abstract perform ( httpRequest ) ; buildValidators ( httpRequest ) { return [ ] ; } async handle ( httpRequest ) { const error = this . validate ( httpRequest ) ; if ( error !== undefined ) return badRequest ( error ) ; try { return await this . perform ( httpRequest ) ; } catch ( error ) { return serverError ( error ) ; } } private validate ( httpRequest ) { const validators = this . buildValidators ( httpRequest ) ; return new ValidationComposite ( validators ) . validate ( ) ; } }