- Ajout de destroy() dans CommandsController (DELETE /commands/:id) - Routes DELETE /commands/:id dans les groupes /api/v1 et /internal - Bouton supprimer dans Commands.vue visible uniquement sur les commandes PENDING - Utilise fetchWithCsrf pour la protection CSRF
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import type { HttpContext } from '@adonisjs/core/http'
|
|
import CommandService from '#services/command_service'
|
|
import { createCommandValidator } from '#validators/command'
|
|
|
|
export default class CommandsController {
|
|
private commandService = new CommandService()
|
|
|
|
async index({ response }: HttpContext) {
|
|
const commands = await this.commandService.getCommands()
|
|
return response.json(commands)
|
|
}
|
|
|
|
async show({ params, response }: HttpContext) {
|
|
const commands = await this.commandService.getCommandsByDevice(params.id)
|
|
return response.json(commands)
|
|
}
|
|
|
|
async store({ request, response }: HttpContext) {
|
|
const payload = await request.validateUsing(createCommandValidator)
|
|
|
|
const command = await this.commandService.createCommand({
|
|
deviceIdentifier: payload.deviceIdentifier,
|
|
action: payload.action as any,
|
|
parameters: payload.parameters as Record<string, any>,
|
|
mode: payload.mode as any,
|
|
priority: payload.priority,
|
|
})
|
|
|
|
return response.status(201).json(command)
|
|
}
|
|
|
|
async destroy({ params, response }: HttpContext) {
|
|
const deleted = await this.commandService.deleteCommand(params.id)
|
|
if (!deleted) {
|
|
return response.status(404).json({ error: 'Command not found' })
|
|
}
|
|
return response.status(204).send('')
|
|
}
|
|
}
|