import { Request , Response } from "express" ; import { getRepository } from "typeorm" ; import * as Yup from 'yup' ; import orphanagesView from '../views/orphanages_view' ; import Orphanage from "../models/Orphanage" ; export default { async index ( request , response ) { const orphanagesRepository = getRepository ( Orphanage ) ; const orphanages = await orphanagesRepository . find ( { relations : [ 'images' ] } ) ; return response . json ( orphanagesView . renderMany ( orphanages ) ) ; } , async show ( request , response ) { const { id } = request . params ; const orphanagesRepository = getRepository ( Orphanage ) ; const orphanage = await orphanagesRepository . findOneOrFail ( id , { relations : [ 'images' ] } ) ; return response . json ( orphanagesView . render ( orphanage ) ) ; } , async create ( request , response ) { const { name , latitude , longitude , about , instructions , opening_hours , open_on_weekends , } = request . body ; const orphanagesRepository = getRepository ( Orphanage ) ; const requestImages = request . files as Express . Multer . File [ ] ; const images = requestImages . map ( image => { return { path : image . filename , } ; } ) ; const data = { name , latitude , longitude , about , instructions , opening_hours , open_on_weekends , images , } ; const schema = Yup . object ( ) . shape ( { name : Yup . string ( ) . required ( ) , latitude : Yup . number ( ) . required ( ) , longitude : Yup . number ( ) . required ( ) , about : Yup . string ( ) . required ( ) . max ( 300 ) , instructions : Yup . string ( ) . required ( ) , opening_hours : Yup . string ( ) . required ( ) , open_on_weekends : Yup . boolean ( ) , images : Yup . array ( Yup . object ( ) . shape ( { path : Yup . string ( ) . required ( ) , } ) ) . required ( ) . min ( 1 ) , } ) ; await schema . validate ( data , { abortEarly : false , } ) ; const orphanage = orphanagesRepository . create ( data ) ; await orphanagesRepository . save ( orphanage ) ; return response . status ( 201 ) . json ( orphanagesView . render ( orphanage ) ) ; } } ;