diff --git a/M4/practica/api/app/controllers/CiudadController.php b/M4/practica/api/app/controllers/CiudadController.php new file mode 100644 index 0000000..9f747fa --- /dev/null +++ b/M4/practica/api/app/controllers/CiudadController.php @@ -0,0 +1,15 @@ +=', Input::get('desde', 1)) - ->whereRaw('id % 2 = ' . (Input::get('filtro') == 'par' ? 0 : 1)) - ->limit(Input::get('cantidad', 5)) - ->get(); + $cantidad = $cantidad > 5 ? 5 : $cantidad; + + if ($id === false) { + return Response::json( + Cliente::all() + ); - return Response::json($clientes); + } else { + $clientes = Cliente + ::where('id', '>=', $id) + ->whereRaw('id % 2 = ?', array($id & 1)) + ->limit($cantidad) + ->get(); + sleep($id & 1 ? 3 : 0); + return Response::json($cantidad == 1 ? array($clientes) : $clientes); + } } public function delete($id) { /** @var $cliente Cliente */ - $cliente = Cliente::findOrFail($id); - $cliente->delete(); - - return Response::json(array('id' => $cliente->id)); + if (!is_null($cliente = Cliente::find($id))) { + $cliente->delete(); + } + sleep(2); + return Response::json(array('id' => $id), is_null($cliente) ? 404 : 200); } public function post() { - return Response::json(Cliente::create(Input::all())); + $datos = Input::all(); + if (!$this->validar_post($datos)) { + return Response::json(array(), 400); + } + $datos['fecha_creacion'] = (new DateTime('now'))->format('l, F d, Y H:i A'); + $cliente = Cliente::create($datos); + return Response::json($cliente); + } + + private function validar_post($datos) + { + if (!count($datos)) { + return false; + } + foreach ($datos as $key => $value) { + if (empty($value)) { + return false; + } + } + return true; } } diff --git a/M4/practica/api/app/controllers/PresidenteController.php b/M4/practica/api/app/controllers/PresidenteController.php index c0f0662..e761ccf 100644 --- a/M4/practica/api/app/controllers/PresidenteController.php +++ b/M4/practica/api/app/controllers/PresidenteController.php @@ -8,11 +8,8 @@ class PresidenteController extends Controller { - public function get() - { - return Response::json( - Presidente::all() - ); - } - + public function get() + { + return Response::json(Presidente::all()); + } } diff --git a/M4/practica/api/app/models/Ciudad.php b/M4/practica/api/app/models/Ciudad.php new file mode 100644 index 0000000..b6c0251 --- /dev/null +++ b/M4/practica/api/app/models/Ciudad.php @@ -0,0 +1,25 @@ +belongsTo('App\\Models\\Pais'); + } +} diff --git a/M4/practica/api/app/models/Cliente.php b/M4/practica/api/app/models/Cliente.php index a3dbf9d..77d165a 100644 --- a/M4/practica/api/app/models/Cliente.php +++ b/M4/practica/api/app/models/Cliente.php @@ -7,7 +7,6 @@ class Cliente extends Eloquent { protected $table = 'clientes'; - public $timestamps = false; protected $fillable = array( @@ -15,22 +14,29 @@ class Cliente extends Eloquent 'logo', 'direccion_calle', 'direccion_altura', - 'direccion_ciudad', - 'pais_id', + 'ciudad_id', 'activo', 'pasivo', 'presidente_id' ); protected $hidden = array( + 'ciudad_id', 'presidente_id' ); protected $with = array( + 'ciudad', 'presidente' ); - public function presidente() { + public function ciudad() + { + return $this->belongsTo('App\\Models\\Ciudad'); + } + + public function presidente() + { return $this->belongsTo('App\\Models\\Presidente'); } } diff --git a/M4/practica/api/app/models/Pais.php b/M4/practica/api/app/models/Pais.php index a061f94..99d11ff 100644 --- a/M4/practica/api/app/models/Pais.php +++ b/M4/practica/api/app/models/Pais.php @@ -7,6 +7,9 @@ class Pais extends Eloquent { protected $table = 'paises'; - public $timestamps = false; + + protected $hidden = array( + 'id' + ); } diff --git a/M4/practica/api/app/models/Presidente.php b/M4/practica/api/app/models/Presidente.php index 78018cf..a26b529 100644 --- a/M4/practica/api/app/models/Presidente.php +++ b/M4/practica/api/app/models/Presidente.php @@ -7,6 +7,9 @@ class Presidente extends Eloquent { protected $table = 'presidentes'; - public $timestamps = false; + + protected $hidden = array( +// 'id' + ); } diff --git a/M4/practica/api/app/routes.php b/M4/practica/api/app/routes.php index 86db520..7847ff7 100644 --- a/M4/practica/api/app/routes.php +++ b/M4/practica/api/app/routes.php @@ -1,10 +1,16 @@ = 0; i--) { + if (impar_encontrado == 1 && parseInt($scope.clientes[i].id) % 2 == 1) { + impar_encontrado = parseInt($scope.clientes[i].id) + 2; + } + if (par_encontrado == 2 && parseInt($scope.clientes[i].id) % 2 == 0) { + par_encontrado = parseInt($scope.clientes[i].id) + 2; + } + if (impar_encontrado != 1 && par_encontrado != 2) { + break; + } + } + + function pedir_posta (desde) { + $http.get('api/cliente/' + desde + '/5', {cache: false} + ).error(function () { + $scope.alert_shown = 'error'; + $scope.boton_buscar_disabled = false; + }).success(function (data) { + angular.forEach(data, function (cliente) { + var exists = false; + for (var i = $scope.clientes.length - 1; i >= 0; i--) { + if ($scope.clientes[i].id == cliente.id) { + exists = true; + break; + } + } + if (!exists) { + $scope.clientes.push(cliente); + } + }); + if (data.length < 5) { + $scope.verificar_clientes_agotados(); + } + $scope.habilitar_boton_buscar(); + }); + } + + pedir_posta(impar_encontrado); + pedir_posta(par_encontrado); + }; + + $scope.tablas_cargadas = 0; + + $scope.habilitar_boton_buscar = function () { + + if (++$scope.tablas_cargadas >= 2) { + if ($scope.clientes_agotados >= 1) { + $scope.clientes_agotados = 0; + } + $scope.tablas_cargadas = 0; + $scope.boton_buscar_disabled = false; + } + }; + + $scope.clientes_agotados = 0; + + $scope.verificar_clientes_agotados = function () { + + if (++$scope.clientes_agotados >= 2) { + $scope.clientes_agotados = 0; + $scope.alert_shown = 'agotado'; + } + }; + + $scope.remove_cliente_warning_shown = false; + + $scope.borrar_cliente = function (id) { + if (!$scope.remove_cliente_warning_shown) { + $scope.remove_cliente_warning_shown = true; + } else { + $http.delete('api/cliente/' + id + ).error(function () { + $scope.alert_shown = 'error'; + }).success(function () { + for (var i = $scope.clientes.length - 1; i >= 0; i--) { + if ($scope.clientes[i].id == id) { + $scope.clientes.splice(i, 1); + break; + } + } + }); + } + }; + } +]); + +clientesProviderApp.filter('filtrarParidad', function () { + return function (input, paridad) { + var r = []; + for (var i in input) { + if (input[i].id % 2 == (paridad == 'impar' ? 1 : 0)) { + r.push(input[i]); + } + } + return r; + } +}); + +clientesProviderApp.controller('SubtitleCtrl', ['$scope', '$http', + function ($scope, $http) { + + $scope.subtitulo = 'Listado de clientes oficiales'; + + /* + si el usuario clickea afuera del texto (pero dentro del , no se llama a esta funcion + http://stackoverflow.com/questions/14943644/angularjs-how-to-watch-tab-selection + */ + $scope.modificar_subtitulo = function (id) { + if (id == 'listado') { + $scope.subtitulo = 'Listado de clientes oficiales'; + } else if (id == 'nuevo') { + $scope.subtitulo = 'Formulario de alta para nuevo cliente'; + if (!$scope.presidentes.length && !$scope.presidentes_cargando) { + $scope.presidentes_cargando = true; + $scope.cargar_presidentes(); + } + if (!$scope.ciudades.length && !$scope.ciudades_cargando) { + $scope.ciudades_cargando = true; + $scope.cargar_ciudades(); + } + } + }; + + $scope.presidentes = []; + $scope.presidentes_cargando = false; + + $scope.cargar_presidentes = function() { + $http.get('api/presidente', {cache: false} + ).error(function (data) { + $scope.presidentes_cargando = false; + }).success(function (data) { + $scope.presidentes = data; + $scope.presidentes_cargando = false; + }); + }; + + $scope.ciudades = []; + $scope.ciudades_cargando = false; + + $scope.cargar_ciudades = function() { + $http.get('api/ciudad', {cache: false} + ).error(function (data) { + $scope.ciudades_cargando = false; + }).success(function (data) { + $scope.ciudades = data; + $scope.ciudades_cargando = false; + }); + }; + } +]); + +clientesProviderApp.controller('NuevoCtrl', ['$scope', '$http', + function ($scope, $http) { + + $scope.alert_shown = ''; + $scope.campos = []; + + // puto el que lee + // 2.0 + // 3.0 + + $scope.alta_cliente = function () { + $http.post('api/cliente', $scope.campos + ).error(function () { + $scope.alert_shown = 'cargado-error'; + }).success(function () { + $scope.campos = []; + $scope.alert_shown = 'cargado-exito'; + }); + }; + } +]); diff --git a/M4/practica/estilos.css b/M4/practica/estilos.css index 2edab70..55f022d 100644 --- a/M4/practica/estilos.css +++ b/M4/practica/estilos.css @@ -1,10 +1,21 @@ -#boton-buscar { - padding: 13px 15px; +/* si se deja solo vcenter, entonces no funciona porque lo sobreescribe bootstrap */ +.vcenter { + vertical-align: middle !important; } + +table > thead > tr > td { + font-weight: bold !important; +} + +.nopadding { + padding: 0 !important; +} + .alert { - display: none; - margin-bottom: 0 !important; + display: none; + margin-bottom: 0 !important; +} + +.jumbotron { + margin-bottom: 10px !important; } -.bold { - font-weight: bold; -} \ No newline at end of file diff --git a/M4/practica/index.html b/M4/practica/index.html index af763d5..b848bdb 100644 --- a/M4/practica/index.html +++ b/M4/practica/index.html @@ -1,263 +1,219 @@ - + La web del curso - + + + + -
-
-
-
-

Telapong S.A.

-

Listado de clientes oficiales

-
+
+
+
+
+

Telapong S.A.

+

{{subtitulo}}

-
-
-
-
- -
-
-
El - servidor se encuentra ocupado. Por favor aguarde e intente nuevamente -
-
El - servidor está disponible para atender el pedido -
-
-
-
-
-
-
-

Impares

-
-
-
-
- - - - - - - - - - - - - - - - -
IDRazón socialPresidentePatrimonio neto
Sin clientes. Debe realizar una búsqueda. -
-
-
-
-
-
-
-

Pares

-
-
-
-
- - - - - - - - - - - - - - - - -
IDRazón socialPresidentePatrimonio neto
Sin clientes. Debe realizar una búsqueda. -
-
-
-
-
-
-