浏览代码

Agregado Modulos y Controlador de Permisos, como su migracion

Jogibeda 7 月之前
父节点
当前提交
62e0d47d1c

+ 2 - 2
migrations/m240925_164317_Creacion_de_Tabla_Modulo.php

@@ -14,8 +14,8 @@ class m240925_164317_Creacion_de_Tabla_Modulo extends Migration
     {
         $this->createTable('Modulo', [
             "id" => $this->string(36),
-            "clave" => $this->string(100)->notNull(),
-            "nombre" => $this->string(100)->notNull(),
+            "clave" => $this->string(100),
+            "nombre" => $this->string(100),
             "creado" => $this->timestamp()->append(" with time zone"),
             "modificado" => $this->timestamp()->append(" with time zone"),
             "eliminado" => $this->timestamp()->append(" with time zone"),

+ 2 - 2
migrations/m240925_184940_Creacion_tablas_Partida_Unidad_Medida.php

@@ -25,8 +25,8 @@ class m240925_184940_Creacion_tablas_Partida_Unidad_Medida extends Migration
 
         $this->createTable('UnidadMedida', [
             "id" => $this->string(36),
-            "nombre" => $this->string(100)->notNull(),
-            "descripcion" => $this->string(100)->notNull(),
+            "nombre" => $this->string(100),
+            "descripcion" => $this->string(100),
             "creado" => $this->timestamp()->append(" with time zone"),
             "modificado" => $this->timestamp()->append(" with time zone"),
             "eliminado" => $this->timestamp()->append(" with time zone"),

+ 52 - 0
migrations/m240926_165247_Tabla_Permiso_Creacion.php

@@ -0,0 +1,52 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m240926_165247_Tabla_Permiso_Creacion
+ */
+class m240926_165247_Tabla_Permiso_Creacion extends Migration
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $this->createTable('Permiso', [
+            "id" => $this->string(36),
+            "nombre" => $this->string(100),
+            "idModulo" => $this->string(36),
+            "clave" => $this->string(100),
+            "descripcion"=> $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->addForeignKey("PermisosModuloFK", "Permiso", "idModulo", "Modulo", "id");
+        $this->addPrimaryKey('PermisoPK', 'Permiso', 'id');
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        $this->dropForeignKey("PermisosModuloFK", "Permiso");
+        $this->dropTable('Permiso');
+    }
+
+    /*
+    // Use up()/down() to run migration code without a transaction.
+    public function up()
+    {
+
+    }
+
+    public function down()
+    {
+        echo "m240926_165247_Tabla_Permiso_Creacion cannot be reverted.\n";
+
+        return false;
+    }
+    */
+}

+ 72 - 0
models/ModuloPermiso.php

@@ -0,0 +1,72 @@
+<?php
+
+namespace app\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "Permiso".
+ *
+ * @property string $id
+ * @property string $nombre
+ * @property string|null $idModulo
+ * @property string|null $clave
+ * @property string|null $descripcion
+ * @property string|null $creado
+ * @property string|null $modificado
+ * @property string|null $eliminado
+ *
+ * @property Modulo $idModulo
+ */
+class ModuloPermiso extends ModeloBase
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'Permiso';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id','nombre','clave'], 'required'],
+            [['creado', 'modificado', 'eliminado'], 'safe'],
+            [['id', 'idModulo'], 'string', 'max' => 36],
+            [['nombre','descripcion','clave'], 'string', 'max' => 100],
+            [['id'], 'unique'],
+            [['idModulo'], 'exist', 'skipOnError' => true, 'targetClass' => Modulo::class, 'targetAttribute' => ['idModulo' => 'id']],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'nombre' => 'Nombre',
+            'idModulo' => 'Id Modulo',
+            'clave' => 'Clave',
+            'descripcion' => 'Descripcion',
+            'creado' => 'Creado',
+            'modificado' => 'Modificado',
+            'eliminado' => 'Eliminado',
+        ];
+    }
+
+    /**
+     * Gets query for [[IdModulo]].
+     *
+     * @return \yii\db\ActiveQuery
+     */
+    public function getModulo()
+    {
+        return $this->hasOne(Modulo::class, ['id' => 'idModulo']);
+    }
+}

+ 83 - 0
modules/v1/controllers/ModuloPermisoController.php

@@ -0,0 +1,83 @@
+<?php
+
+namespace v1\controllers;
+
+use common\data\Respuesta;
+use common\rest\AuthController;
+use yii\db\Expression;
+
+class ModuloPermisoController extends AuthController {
+
+  public $modelClass = "v1\models\ModuloPermiso";
+
+  public function actionIndex() {
+    $id = trim($this->req->get("id", ""));
+    $buscar = trim($this->req->get("buscar", ""));
+
+    $query = $this->queryInicial;
+
+    if($id !== '') {
+      $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 = 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 Modulo");
+    }
+
+    $modelo->refresh();
+    return (new Respuesta($modelo))
+      ->mensaje("Modulo guardada");
+  }
+
+  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("Modulo no encontrada");
+    }
+    $modelo->eliminado = new Expression('now()');
+    if(!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("No se pudo eliminar Modulo");
+    }
+
+    return (new Respuesta())
+      ->mensaje("Modulo eliminada");
+  }
+}

+ 32 - 0
modules/v1/models/ModuloPermiso.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace v1\models;
+
+use app\models\ModuloPermiso as ModeloModuloPermiso;
+
+class ModuloPermiso extends ModeloModuloPermiso
+{
+
+    public function fields()
+    {
+        return [
+            'id',
+            'nombre',
+            'idModulo',
+            'clave',
+            'descripcion',
+            'creado',
+            'modificado',
+
+        ];
+    }
+    public function extraFields() {
+        return [
+            'modulo',
+        ];
+    }
+    public function getModulo()
+    {
+        return $this->hasOne(Modulo::class, ['id' => 'idModulo']);
+    }
+}