<?php

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

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

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

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

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