<?php

use \Slim\Http\Request as Request;
use \Slim\Http\Response as Response;
use App\Model\Ticket;

$app->get('/datosticket', function (Request $request, Response $response, $args) {
    $objects = Ticket::all();
    $response->getBody()->write(Ticket::toJSON($objects));
    return $response;
});

$app->post('/datosticket', function (Request $request,  Response $response, $args) {
    $att = json_decode($request->getBody());
    $object = new Ticket();
    $object->cargar($att);
    $object->save();
    $objects = $object->all();
    $response->getBody()->write(Ticket::toJSON($objects));
    return $response;
});

$app->put('/datosticket', function (Request $request,  Response $response, $args) {
    $att = json_decode($request->getBody());
    $object = Ticket::find($att->id);
    $object->cargar($att);
    $object->save();
    $objects = $object->all();
    $response->getBody()->write(Ticket::toJSON($objects));
    return $response;
});

$app->delete('/datosticket', function (Request $request,  Response $response, $args) {
    Ticket::connection()->query("TRUNCATE TABLE mensatic");
    return $response;
});
