feat: add orphanage management functionality with image upload and validation
This commit is contained in:
74
backend/src/controllers/OrphanagesController.ts
Normal file
74
backend/src/controllers/OrphanagesController.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
|
||||
import { Request, Response } from 'express';
|
||||
import { getRepository } from 'typeorm';
|
||||
import Orphanage from '../models/Orphanage';
|
||||
import OrphanageView from '../views/orphanages_view';
|
||||
import * as Yup from 'yup';
|
||||
|
||||
export default {
|
||||
|
||||
async getAll(request: Request, response: Response) {
|
||||
return response.json(OrphanageView.renderMany(await getRepository(Orphanage).find({
|
||||
relations: ['images']
|
||||
})));
|
||||
},
|
||||
|
||||
async getIndex(request: Request, response: Response) {
|
||||
return response.json(OrphanageView.render(await getRepository(Orphanage).findOneOrFail(request.params.id, {
|
||||
relations: ['images']
|
||||
})));
|
||||
},
|
||||
|
||||
async create(request: Request, response: Response) {
|
||||
const {
|
||||
name,
|
||||
latitude,
|
||||
longitude,
|
||||
about,
|
||||
instructions,
|
||||
opening_hours,
|
||||
open_on_weekends,
|
||||
phone
|
||||
} = request.body;
|
||||
|
||||
const requestImages = request.files as Express.Multer.File[];
|
||||
const images = requestImages.map(image => {
|
||||
return { path: image.filename};
|
||||
})
|
||||
|
||||
const data = {
|
||||
name,
|
||||
latitude: Number(latitude),
|
||||
longitude: Number(longitude),
|
||||
about,
|
||||
instructions,
|
||||
opening_hours,
|
||||
open_on_weekends: open_on_weekends === 'true',
|
||||
phone,
|
||||
images,
|
||||
};
|
||||
|
||||
const schema = Yup.object().shape({
|
||||
name: Yup.string().required(),
|
||||
latitude: Yup.number().required(),
|
||||
longitude: Yup.number().required(),
|
||||
about: Yup.string().required().max(300),
|
||||
opening_hours: Yup.string().required(),
|
||||
open_on_weekends: Yup.boolean().required(),
|
||||
phone: Yup.string().required(),
|
||||
images: Yup.array(Yup.object().shape({
|
||||
path: Yup.string().required()
|
||||
}))
|
||||
});
|
||||
|
||||
await schema.validate(data, {
|
||||
abortEarly: false,
|
||||
});
|
||||
|
||||
const orphanage = getRepository(Orphanage).create(data);
|
||||
|
||||
await getRepository(Orphanage).save(orphanage);
|
||||
|
||||
return response.status(201).json(orphanage);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user