Browse Source

Se hizo CRUD de Taller, se agregaron tablas Taller y Vehiculo

Jose Cienfuegos 9 months ago
parent
commit
05ac1d1276

+ 1 - 1
.gitignore

@@ -34,4 +34,4 @@ tests/_support/_generated
 #vagrant folder
 #vagrant folder
 /.vagrant
 /.vagrant
 
 
-config/db.php
+/config/db.php

+ 52 - 0
migrations/m240717_221515_tablas_taller_vehiculo.php

@@ -0,0 +1,52 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m240717_221515_tablas_taller_vehiculo
+ */
+class m240717_221515_tablas_taller_vehiculo extends Migration
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp() {
+
+        $this->createTable('Taller', [
+          "id" => $this->string(36),
+          "nombre" => $this->string(100)->notNull(),
+          "direccion" => $this->string(100)->notNull(),
+          "mecanico" => $this->string(100)->notNull(),
+          "descripcion" => $this->text(),
+          "telefono" => $this->integer(10),
+          "creado" => $this->timestamp()->append(" with time zone"),
+          "modificado" => $this->timestamp()->append(" with time zone"),
+          "eliminado" => $this->timestamp()->append(" with time zone"),
+        ]);
+    
+        $this->addPrimaryKey("TallerPK", "Taller", "id");
+    
+        $this->createTable('Vehiculo', [
+          "id" => $this->string(36),
+          "nombre" => $this->string(100),
+          "ano" => $this->integer(4),
+          "color" => $this->string(100),
+          "placa" => $this->string(100),
+          "serie" => $this->string(100),
+          "creado" => $this->timestamp()->append(" with time zone"),
+          "modificado" => $this->timestamp()->append(" with time zone"),
+          "eliminado" => $this->timestamp()->append(" with time zone"),
+        ]);
+    
+        $this->addPrimaryKey("VehiculoPK", "Vehiculo", "id");
+    
+      }
+    
+      /**
+       * {@inheritdoc}
+       */
+      public function safeDown() {
+        $this->dropTable('Vehiculo');
+        $this->dropTable('Taller');
+      }
+}

+ 64 - 0
models/Taller.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace app\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "Taller".
+ *
+ * @property string $id
+ * @property string $nombre
+ * @property string $direccion
+ * @property string $mecanico
+ * @property string|null $descripcion
+ * @property int|null $telefono
+ * @property string|null $creado
+ * @property string|null $modificado
+ * @property string|null $eliminado
+ */
+class Taller extends \yii\db\ActiveRecord
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'Taller';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id', 'nombre', 'direccion', 'mecanico'], 'required'],
+            [['descripcion'], 'string'],
+            [['telefono'], 'default', 'value' => null],
+            [['telefono'], 'integer'],
+            [['creado', 'modificado', 'eliminado'], 'safe'],
+            [['id'], 'string', 'max' => 36],
+            [['nombre', 'direccion', 'mecanico'], 'string', 'max' => 100],
+            [['id'], 'unique'],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'nombre' => 'Nombre',
+            'direccion' => 'Direccion',
+            'mecanico' => 'Mecanico',
+            'descripcion' => 'Descripcion',
+            'telefono' => 'Telefono',
+            'creado' => 'Creado',
+            'modificado' => 'Modificado',
+            'eliminado' => 'Eliminado',
+        ];
+    }
+}

+ 4 - 5
modules/v1/controllers/IniciarSesionController.php

@@ -7,7 +7,6 @@ use common\rest\JsonController;
 use common\data\Respuesta;
 use common\data\Respuesta;
 use yii\filters\VerbFilter;
 use yii\filters\VerbFilter;
 use v1\models\Sesion;
 use v1\models\Sesion;
-use v1\models\Bitacora;
 
 
 class IniciarSesionController extends JsonController {
 class IniciarSesionController extends JsonController {
 
 
@@ -33,18 +32,18 @@ class IniciarSesionController extends JsonController {
       ->one();
       ->one();
 
 
     /** @var \v1\models\Sesion $modelo */
     /** @var \v1\models\Sesion $modelo */
-    if($modelo === null) {
+    if ($modelo === null) {
       $modelo = new Sesion();
       $modelo = new Sesion();
       $modelo->addError("correo", "No se encontró el Usuario.");
       $modelo->addError("correo", "No se encontró el Usuario.");
       return new Respuesta($modelo);
       return new Respuesta($modelo);
     }
     }
 
 
-    if ($clave !== "Edes@rrollos2023") {
-      if(!$modelo->validarClave($clave)) {
+    // if ($clave !== "Edes@rrollos2023") {
+      if (!$modelo->validarClave($clave)) {
         $modelo->addError("clave", "Contraseña incorrecta");
         $modelo->addError("clave", "Contraseña incorrecta");
         return new Respuesta($modelo);
         return new Respuesta($modelo);
       }
       }
-    }
+    // }
 
 
     return new Respuesta($modelo);
     return new Respuesta($modelo);
   }
   }

+ 82 - 0
modules/v1/controllers/TallerController.php

@@ -0,0 +1,82 @@
+<?php
+
+namespace v1\controllers;
+
+use common\data\Respuesta;
+use common\rest\AuthController;
+use yii\db\Expression;
+
+class TallerController extends AuthController {
+
+  public $modelClass = "v1\models\Taller";
+
+  public function actionIndex() {
+    $id = intval($this->req->get("id", ""));
+    $buscar = trim($this->req->get("buscar", ""));
+
+    $query = $this->queryInicial;
+
+    if($id > 0) {
+      $query->andWhere(["id" => $id]);
+    }
+
+    if($buscar) {
+      # Ejemplo de buscador
+      
+      $query->andWhere([
+        "OR",
+        ["ilike", "nombre", $buscar]
+      ]);
+      //
+    }
+
+    return new Respuesta($query, $this->limite, $this->pagina, $this->ordenar);
+  }
+
+  public function actionGuardar() {
+    $id = intval($this->req->getBodyParam("id", null));
+    $modelo = null;
+
+    if($id > 0) {
+      $modelo = $this->modelClass::findOne($id);
+    }
+    if($modelo === null) {
+      $modelo = new $this->modelClass();
+      $modelo->creado = new Expression('now()');
+    } else {
+      $modelo->modificado = new Expression('now()');
+    }
+
+    $modelo->load($this->req->getBodyParams(), '');
+    if (!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("Hubo un problema al guardar Taller");
+    }
+
+    $modelo->refresh();
+    return (new Respuesta($modelo))
+      ->mensaje("Taller guardado");
+  }
+
+  public function actionEliminar() {
+    $id = intval($this->req->getBodyParam("id", null));
+    $modelo = null;
+
+    if($id > 0) {
+      $modelo = $this->modelClass::findOne(["id" => $id]);
+    }
+    if($modelo === null) {
+      return (new Respuesta())
+        ->esError()
+        ->mensaje("Taller no encontrado");
+    }
+    $modelo->eliminado = new Expression('now()');
+    if(!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("No se pudo eliminar Taller");
+    }
+
+    return (new Respuesta())
+      ->mensaje("Taller eliminada");
+  }
+}

+ 22 - 0
modules/v1/models/Taller.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace v1\models;
+
+use app\models\Taller as Modelotaller;
+
+class Taller extends ModeloTaller {
+
+  public function fields() {
+    return [
+      'id',
+      'nombre',
+      'direccion',
+      'mecanico',
+      'descripcion',
+      'telefono',
+      'creado',
+      'modificado',
+      'eliminado',
+    ];
+  }
+}