<?php

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

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

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

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

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