import Training from '#models/training';
import { DateTime } from 'luxon';
export default class TrainingsController {
    async index({ view }) {
        const trainings = await Training.query().orderBy('start', 'desc');
        return view.render('admin/trainings/index', { trainings });
    }
    async create({ view }) {
        return view.render('admin/trainings/form', { training: null });
    }
    async store({ request, response }) {
        const { name, start, end, skill, place } = request.only([
            'name',
            'start',
            'end',
            'skill',
            'place',
        ]);
        await Training.create({
            name,
            start: start ? DateTime.fromISO(start) : null,
            end: end ? DateTime.fromISO(end) : null,
            skill: skill || null,
            place: place || null,
        });
        return response.redirect('/admin/trainings');
    }
    async edit({ view, params, response }) {
        const training = await Training.find(params.id);
        if (!training) {
            return response.notFound();
        }
        return view.render('admin/trainings/form', { training });
    }
    async update({ request, params, response }) {
        const training = await Training.find(params.id);
        if (!training) {
            return response.notFound();
        }
        const { name, start, end, skill, place } = request.only([
            'name',
            'start',
            'end',
            'skill',
            'place',
        ]);
        await training
            .merge({
            name,
            start: start ? DateTime.fromISO(start) : null,
            end: end ? DateTime.fromISO(end) : null,
            skill: skill || null,
            place: place || null,
        })
            .save();
        return response.redirect('/admin/trainings');
    }
    async destroy({ params, response }) {
        const training = await Training.find(params.id);
        if (!training) {
            return response.notFound();
        }
        await training.delete();
        return response.redirect('/admin/trainings');
    }
}
//# sourceMappingURL=trainings_controller.js.map