|
@@ -6,48 +6,59 @@ use common\data\Respuesta;
|
|
|
use common\rest\AuthController;
|
|
|
use yii\db\Expression;
|
|
|
|
|
|
-class TipoHerramientaController extends AuthController {
|
|
|
+class TipoHerramientaController extends AuthController
|
|
|
+{
|
|
|
|
|
|
public $modelClass = "v1\models\TipoHerramienta";
|
|
|
|
|
|
- public function actionIndex() {
|
|
|
+ public function actionIndex()
|
|
|
+ {
|
|
|
$id = trim($this->req->get("id", ""));
|
|
|
$buscar = trim($this->req->get("q", ""));
|
|
|
-
|
|
|
+
|
|
|
$query = $this->queryInicial;
|
|
|
-
|
|
|
|
|
|
- if($id !== "") {
|
|
|
+
|
|
|
+ if ($id !== "") {
|
|
|
$query->andWhere(["id" => $id]);
|
|
|
}
|
|
|
|
|
|
- if($buscar) {
|
|
|
-
|
|
|
+ if ($buscar) {
|
|
|
+
|
|
|
$query->andWhere([
|
|
|
"OR",
|
|
|
["ilike", "tipo", $buscar]
|
|
|
]);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
return new Respuesta($query, $this->limite, $this->pagina, $this->ordenar);
|
|
|
}
|
|
|
|
|
|
- public function actionGuardar() {
|
|
|
+ public function actionGuardar()
|
|
|
+ {
|
|
|
$id = trim($this->req->getBodyParam("id", ""));
|
|
|
$modelo = null;
|
|
|
|
|
|
- if($id !== "") {
|
|
|
+ if ($id !== "") {
|
|
|
$modelo = $this->modelClass::findOne($id);
|
|
|
}
|
|
|
- if($modelo === null) {
|
|
|
+ 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(), '');
|
|
|
|
|
|
+ $clave = $modelo->clave;
|
|
|
+ $existeClave = $this->modelClass::find()->where(['clave' => $clave])->andWhere(['<>', 'id', $id])->exists();
|
|
|
+
|
|
|
+ if ($existeClave) {
|
|
|
+ return (new Respuesta())
|
|
|
+ ->esError()
|
|
|
+ ->mensaje("La clave '$clave' ya existe. Por favor, ingresa una clave diferente.");
|
|
|
+ }
|
|
|
$modelo->load($this->req->getBodyParams(), '');
|
|
|
if (!$modelo->save()) {
|
|
|
return (new Respuesta($modelo))
|
|
@@ -59,20 +70,21 @@ class TipoHerramientaController extends AuthController {
|
|
|
->mensaje("Tipo de herramienta guardado");
|
|
|
}
|
|
|
|
|
|
- public function actionEliminar() {
|
|
|
+ public function actionEliminar()
|
|
|
+ {
|
|
|
$id = trim($this->req->getBodyParam("id", ""));
|
|
|
$modelo = null;
|
|
|
|
|
|
- if($id !== "") {
|
|
|
+ if ($id !== "") {
|
|
|
$modelo = $this->modelClass::findOne(["id" => $id]);
|
|
|
}
|
|
|
- if($modelo === null) {
|
|
|
+ if ($modelo === null) {
|
|
|
return (new Respuesta())
|
|
|
->esError()
|
|
|
->mensaje("Tipo de herramienta no encontrado");
|
|
|
}
|
|
|
$modelo->eliminado = new Expression('now()');
|
|
|
- if(!$modelo->save()) {
|
|
|
+ if (!$modelo->save()) {
|
|
|
return (new Respuesta($modelo))
|
|
|
->mensaje("No se pudo eliminar el tipo de herramienta");
|
|
|
}
|
|
@@ -80,4 +92,4 @@ class TipoHerramientaController extends AuthController {
|
|
|
return (new Respuesta())
|
|
|
->mensaje("Tipo de herramienta eliminado");
|
|
|
}
|
|
|
-}
|
|
|
+}
|