<?php

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

$app->get('/zonas[/{id}]', function (Request $request, Response $response, $args) {
    if (isset($args['id'])){
        $result = Zona::find($args['id']);
        $response->getBody()->write(Zona::toJSON($result));
    }else{
        $objects = Zona::all();
        $response->getBody()->write(Zona::toJSON($objects));
    }
    return $response;
});

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

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

$app->delete('/zonas/{id}', function (Request $request,  Response $response, $args) {
    Zona::find($args['id'])->delete();
    $objects = Zona::all();
    $response->getBody()->write(Zona::toJSON($objects));
    return $response;
});
