Browse Source

Se agrego modulo de campana modelos y controller

Jose Cienfuegos 5 months ago
parent
commit
e3b82850fe

+ 57 - 0
migrations/m241122_235440_tablas_campanas_banner.php

@@ -0,0 +1,57 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m241122_235440_tablas_campanas_banner
+ */
+class m241122_235440_tablas_campanas_banner extends Migration
+{    public function safeUp()
+    {
+        $this->createTable('Campana', [
+            "id" => $this->string(36),
+            "idMedia" => $this->string(36),
+            "nombre" => $this->string(),
+            "descripcion" => $this->string(550),
+            "activo" => $this->boolean()->defaultValue(false),
+            "fechaInicio" => $this->timestamp()->append(" with time zone"),
+            "fechaFin" => $this->timestamp()->append(" with time zone"),
+            "creado" => $this->timestamp()->append(" with time zone"),
+            "modificado" => $this->timestamp()->append(" with time zone"),
+            "eliminado" => $this->timestamp()->append(" with time zone"),
+          ]);
+
+          $this->addPrimaryKey('CampanaPK', 'Campana', 'id');
+
+          $this->addForeignKey('CampanaMediaFK', 'Campana', 'idMedia', 'Media', 'id');
+
+          $this->createTable('Banner', [
+            "id" => $this->string(36),
+            "idCampana" => $this->string(36),
+            "idMedia" => $this->string(36),
+            "nombre" => $this->string(),
+            "tamano" => $this->string(),
+            "descripcion" => $this->text(),
+            "activo" => $this->boolean()->defaultValue(false),
+            "fechaInicio" => $this->timestamp()->append("with time zone"),
+            "fechaFin" => $this->timestamp()->append("with time zone"),
+            "creado" => $this->timestamp()->append("with time zone"),
+            "modificado" => $this->timestamp()->append("with time zone"),
+            "eliminado" => $this->timestamp()->append("with time zone")
+          ]);
+
+          $this->addPrimaryKey('BannerPK', 'Banner', 'id');
+
+          $this->addForeignKey('BannerCampanaFK', 'Banner', 'idCampana', 'Campana', 'id');
+    }
+
+    public function safeDown()
+    {
+        $this->dropForeignKey('BannerCampanaFK', 'Banner');
+        $this->dropForeignKey('CampanaMediaFK', 'Campana');
+
+        $this->dropTable('Banner');
+        $this->dropTable('Campana');
+
+    }
+}

+ 79 - 0
models/Campana.php

@@ -0,0 +1,79 @@
+<?php
+
+namespace app\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "Campana".
+ *
+ * @property string $id
+ * @property string|null $idMedia
+ * @property string|null $nombre
+ * @property string|null $descripcion
+ * @property bool|null $activo
+ * @property string|null $fechaInicio
+ * @property string|null $fechaFin
+ * @property string|null $creado
+ * @property string|null $modificado
+ * @property string|null $eliminado
+ *
+ * @property Banner[] $banners
+ * @property Media $idMedia0
+ */
+class Campana extends ModeloBase
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'Campana';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id'], 'required'],
+            [['activo'], 'boolean'],
+            [['fechaInicio', 'fechaFin', 'creado', 'modificado', 'eliminado'], 'safe'],
+            [['id', 'idMedia'], 'string', 'max' => 36],
+            [['nombre'], 'string', 'max' => 255],
+            [['descripcion'], 'string', 'max' => 550],
+            [['id'], 'unique'],
+            [['idMedia'], 'exist', 'skipOnError' => true, 'targetClass' => Media::class, 'targetAttribute' => ['idMedia' => 'id']],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'idMedia' => 'Id Media',
+            'nombre' => 'Nombre',
+            'descripcion' => 'Descripcion',
+            'activo' => 'Activo',
+            'fechaInicio' => 'Fecha Inicio',
+            'fechaFin' => 'Fecha Fin',
+            'creado' => 'Creado',
+            'modificado' => 'Modificado',
+            'eliminado' => 'Eliminado',
+        ];
+    }
+
+    public function getBanners()
+    {
+        return $this->hasMany(Banner::class, ['idCampana' => 'id']);
+    }
+
+    public function getMedia()
+    {
+        return $this->hasOne(Media::class, ['id' => 'idMedia']);
+    }
+}

+ 91 - 0
modules/v1/controllers/CampanaController.php

@@ -0,0 +1,91 @@
+<?php
+
+namespace v1\controllers;
+
+use app\models\Media;
+use common\data\Respuesta;
+use common\rest\AuthController;
+use yii\db\Expression;
+
+class CampanaController extends AuthController {
+
+  public $modelClass = "v1\models\Campana";
+
+  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", ""));
+    $archivos = $this->req->getBodyParam("archivos", []);
+    $archivosEliminados = $this->req->getBodyParam("archivosEliminados", []);
+    $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 (empty($archivos)) {
+      $modelo->idMedia = null;
+    } else {
+      $modelo->idMedia = $archivos[0]['idMedia'];
+    }
+
+    if (!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("Hubo un problema al guardar la Campaña");
+    }
+
+    $modelo->refresh();
+    return (new Respuesta($modelo))
+      ->mensaje("Campaña guardada");
+  }
+
+  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("Campaña no encontrada");
+    }
+    $modelo->eliminado = new Expression('now()');
+    if (!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("No se pudo eliminar la Campaña");
+    }
+
+    return (new Respuesta())
+      ->mensaje("Campaña eliminada");
+  }
+}

+ 39 - 0
modules/v1/models/Campana.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace v1\models;
+
+use app\models\Campana as ModeloCampana;
+
+class Campana extends ModeloCampana {
+
+  public function fields() {
+    return [
+        'id',
+        'idMedia',
+        'nombre',
+        'descripcion',
+        'activo',
+        'fechaInicio',
+        'fechaFin',
+        'creado',
+        'modificado',
+    ];
+  }
+
+  public function extraFields() {
+    return [
+        'banners',
+        'media',
+    ];
+}
+
+  public function getBanners()
+  {
+      return $this->hasMany(Banner::class, ['idCampana' => 'id']);
+  }
+
+  public function getMedia()
+  {
+      return $this->hasOne(Media::class, ['id' => 'idMedia']);
+  }
+}