瀏覽代碼

Modulo Empleado creado

Jogibeda 8 月之前
父節點
當前提交
f91f600e10

+ 51 - 0
migrations/m240809_163926_Empleado.php

@@ -0,0 +1,51 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m240809_163926_Empleado
+ */
+class m240809_163926_Empleado extends Migration
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $this->createTable('Empleado', [
+            "id" => $this->string(36),
+            "nombre" => $this->string(100)->notNull(),
+            "fechaAlta" => $this->timestamp()->append(" with time zone"),
+            "estatus" => $this->boolean()->notNull(),
+            "descripcion" => $this->string(100)->notNull(),
+            "creado" => $this->timestamp()->append(" with time zone"),
+            "modificado" => $this->timestamp()->append(" with time zone"),
+            "eliminado" => $this->timestamp()->append(" with time zone"),
+        ]);
+
+        $this->addPrimaryKey('EmpleadoPK', 'Empleado', 'id');
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        $this->dropTable('Empleado');
+    }
+
+    /*
+    // Use up()/down() to run migration code without a transaction.
+    public function up()
+    {
+
+    }
+
+    public function down()
+    {
+        echo "m240809_163926_Empleado cannot be reverted.\n";
+
+        return false;
+    }
+    */
+}

+ 60 - 0
models/Empleado.php

@@ -0,0 +1,60 @@
+<?php
+
+namespace app\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "Vehiculo".
+ *
+ * @property string $id
+ * @property string|null $nombre
+ * @property string|null $fechaAlta
+ * @property boolean|null $estatus
+ * @property string|null $descripcion
+ * @property string|null $creado
+ * @property string|null $modificado
+ * @property string|null $eliminado
+ */
+class Empleado extends ModeloBase
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'Empleado';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id','nombre'], 'required'],
+            [['fechaAlta','creado', 'modificado', 'eliminado'], 'safe'],
+            [['estatus'],'boolean'],
+            [['id'], 'string', 'max' => 36],
+            [['nombre','descripcion'], 'string', 'max' => 100],
+            [['id'], 'unique'],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'nombre' => 'Nombre',
+            'fechaAlta' => 'Fecha Alta',
+            'estatus' => 'Estatus',
+            'descripcion' => 'Descripcion',
+            'creado'=>'Creado',
+            'modificado' => 'Modificado',
+            'eliminado' => 'Eliminado',
+        ];
+    }
+}

+ 84 - 0
modules/v1/controllers/EmpleadoController.php

@@ -0,0 +1,84 @@
+<?php
+
+namespace v1\controllers;
+
+use common\data\Respuesta;
+use common\rest\AuthController;
+use yii\db\Expression;
+
+class EmpleadoController extends AuthController {
+
+  public $modelClass = "v1\models\Empleado";
+
+  public function actionIndex() {
+    $id = trim($this->req->get("id", ""));
+    $buscar = trim($this->req->get("q", ""));
+
+    $query = $this->queryInicial;
+
+    if($id > 0) {
+      $query->andWhere(["id" => $id]);
+    }
+
+  
+
+    if($buscar) {
+      
+      $query->andWhere([
+        "OR",
+        ["ilike", "nombre", $buscar]
+      ]);
+
+    }
+
+    return new Respuesta($query, $this->limite, $this->pagina, $this->ordenar);
+  }
+
+  public function actionGuardar() {
+    $id = trim($this->req->getBodyParam("id", ""));
+    $modelo = null;
+
+    if($id !== "") {
+      $modelo = $this->modelClass::findOne($id);
+    }
+    if($modelo === null) {
+      $modelo = new $this->modelClass();
+      $modelo->uuid();
+      $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 el empleado");
+    }
+
+    $modelo->refresh();
+    return (new Respuesta($modelo))
+      ->mensaje("Empleado guardado");
+  }
+
+  public function actionEliminar() {
+    $id = trim($this->req->getBodyParam("id", ""));
+    $modelo = null;
+
+    if($id !== "") {
+      $modelo = $this->modelClass::findOne(["id" => $id]);
+    }
+    if($modelo === null) {
+      return (new Respuesta())
+        ->esError()
+        ->mensaje("Empleado no encontrado");
+    }
+    $modelo->eliminado = new Expression('now()');
+    if(!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("No se pudo eliminar el empleado");
+    }
+
+    return (new Respuesta())
+      ->mensaje("Empleado eliminado");
+  }
+}

+ 20 - 0
modules/v1/models/Empleado.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace v1\models;
+
+use app\models\Empleado as ModeloEmpleado;
+
+class Empleado extends ModeloEmpleado{
+
+  public function fields() {
+    return [
+      'id',
+      'nombre',
+      'fechaAlta',
+      'estatus',
+      'descripcion',
+      'creado',
+      'modificado',
+    ];
+  }
+}