ElementController.js
Home
/
app /
Controllers /
Http /
ElementController.js
'use strict'
/** @typedef {import('@adonisjs/framework/src/Request')} Request */
/** @typedef {import('@adonisjs/framework/src/Response')} Response */
/** @typedef {import('@adonisjs/framework/src/View')} View */
const Element = use('App/Models/Element')
const Theme = use('App/Models/Theme')
const { validate } = use('Validator')
/**
* Resourceful controller for interacting with elements
*/
class ElementController {
/**
* Show a list of all elements.
* GET elements
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
* @param {View} ctx.view
*/
async index ({ request, response, view }) {
const elements = await Element.query().with('theme').orderBy('name', 'asc').fetch()
return view.render('elements.index', {
elements: elements.toJSON()
})
}
/**
* Render a form to be used for creating a new element.
* GET elements/create
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
* @param {View} ctx.view
*/
async create ({ request, response, view }) {
const themes = await Theme.all()
return view.render('elements.create', {
themes: themes.toJSON()
})
}
/**
* Create/save a new element.
* POST elements
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
*/
async store ({ request, response }) {
const data = request.only(['name', 'theme_id', 'priority', 'refresh_rate'])
const validation = await validate(request.all(), {
name: 'required|max:255',
theme_id: 'required',
priority: 'required',
refresh_rate: 'required'
}, {
'name.required': `Theme name is required`,
'theme_id.required': `Theme is required`,
'priority.required': `Priority is required`,
'refresh_rate.required': `Refresh rate is required`,
})
if(validation.fails()) {
session.withErrors(validation.messages()).flashAll()
return response.redirect('back')
}
await Element.create(data)
return response.redirect('/elements')
}
/**
* Display a single element.
* GET elements/:id
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
* @param {View} ctx.view
*/
async show ({ params, request, response, view }) {
}
/**
* Render a form to update an existing element.
* GET elements/:id/edit
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
* @param {View} ctx.view
*/
async edit ({ params, request, response, view }) {
}
/**
* Update element details.
* PUT or PATCH elements/:id
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
*/
async update ({ params, request, response }) {
}
/**
* Delete a element with id.
* DELETE elements/:id
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
*/
async destroy ({ params, request, response }) {
const element = await Element.findOrFail(params.id)
await element.delete()
return response.redirect('/elements')
}
}
module.exports = ElementController