import Tag from '#models/tag';
export default class TagsController {
    async index({ view }) {
        const tags = await Tag.all();
        return view.render('admin/tags/index', { tags });
    }
    async create({ view }) {
        const defaultColor = '#' + Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0');
        return view.render('admin/tags/form', { tag: null, defaultColor });
    }
    async store({ request, response }) {
        const { name, color } = request.only(['name', 'color']);
        await Tag.create({ name, color: color || null });
        return response.redirect('/admin/tags');
    }
    async edit({ view, params, response }) {
        const tag = await Tag.find(params.id);
        if (!tag) {
            return response.notFound();
        }
        return view.render('admin/tags/form', { tag });
    }
    async update({ request, params, response }) {
        const tag = await Tag.find(params.id);
        if (!tag) {
            return response.notFound();
        }
        const { name, color } = request.only(['name', 'color']);
        await tag.merge({ name, color: color || null }).save();
        return response.redirect('/admin/tags');
    }
    async destroy({ params, response }) {
        const tag = await Tag.find(params.id);
        if (!tag) {
            return response.notFound();
        }
        await tag.delete();
        return response.redirect('/admin/tags');
    }
}
//# sourceMappingURL=tags_controller.js.map