2022-07-09 03:09:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Models\Estandar;
|
2022-11-11 03:13:45 +00:00
|
|
|
use App\Models\User;
|
2022-07-09 03:09:04 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
class EstandarController extends Controller
|
|
|
|
{
|
2022-11-11 03:13:45 +00:00
|
|
|
public function createEstandar(Request $request)
|
|
|
|
{
|
2022-07-09 03:09:04 +00:00
|
|
|
$request->validate([
|
2022-11-11 03:13:45 +00:00
|
|
|
"name" => "required",
|
|
|
|
"cabecera" => "required",
|
2022-07-09 03:09:04 +00:00
|
|
|
]);
|
|
|
|
$id_user = auth()->user()->id;
|
|
|
|
$estandar = new Estandar();
|
|
|
|
$estandar->id_user = $id_user;
|
|
|
|
$estandar->name = $request->name;
|
2022-11-11 03:13:45 +00:00
|
|
|
$estandar->cabecera = $request->cabecera;
|
2022-07-09 03:09:04 +00:00
|
|
|
$estandar->save();
|
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"msg" => "!Estandar creado exitosamente",
|
2022-11-11 03:13:45 +00:00
|
|
|
"data" => $estandar,
|
2022-07-09 03:09:04 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-11-11 03:13:45 +00:00
|
|
|
public function listEstandar()
|
|
|
|
{
|
2022-07-09 03:09:04 +00:00
|
|
|
$estandares = Estandar::all();
|
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"msg" => "!Lista de Estandares",
|
|
|
|
"data" => $estandares,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-11-11 03:13:45 +00:00
|
|
|
public function listEstandarValores()
|
|
|
|
{
|
|
|
|
$estandaresNombreslist = Estandar::select('estandars.name', 'estandars.id')
|
|
|
|
->orderBy('estandars.id', 'asc')
|
|
|
|
->get();
|
2022-08-13 02:27:34 +00:00
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"msg" => "!Lista de nombres de Estandares",
|
|
|
|
"data" => $estandaresNombreslist,
|
|
|
|
]);
|
|
|
|
}
|
2022-08-17 01:11:22 +00:00
|
|
|
|
2022-08-13 02:27:34 +00:00
|
|
|
|
2022-11-11 03:13:45 +00:00
|
|
|
public function showEstandar($id)
|
|
|
|
{
|
|
|
|
if (Estandar::where("id", $id)->exists()) {
|
2022-07-09 03:09:04 +00:00
|
|
|
$estandar = Estandar::find($id);
|
2022-11-11 03:13:45 +00:00
|
|
|
$user = User::find($estandar->id_user);
|
|
|
|
$estandar->user = $user;
|
|
|
|
$estandar->esEncargado = ($user->id == auth()->user()->id);
|
2022-07-09 03:09:04 +00:00
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"msg" => "!Estandar",
|
|
|
|
"data" => $estandar,
|
|
|
|
]);
|
2022-11-11 03:13:45 +00:00
|
|
|
} else {
|
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"msg" => "!No se encontro el estandar",
|
|
|
|
], 404);
|
2022-07-09 03:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 03:13:45 +00:00
|
|
|
public function updateEstandar(Request $request, $id)
|
|
|
|
{
|
2022-11-14 21:40:44 +00:00
|
|
|
$request->validate([
|
|
|
|
"id_user" => 'exists:App\Models\User,id',
|
|
|
|
]);
|
|
|
|
|
2022-07-09 03:09:04 +00:00
|
|
|
$id_user = auth()->user()->id;
|
2022-11-11 03:13:45 +00:00
|
|
|
if (Estandar::where(["id_user" => $id_user, "id" => $id])->exists()) {
|
2022-07-09 03:09:04 +00:00
|
|
|
$estandar = Estandar::find($id);
|
2022-08-17 01:11:22 +00:00
|
|
|
$estandar->name = isset($request->name) ? $request->name : $estandar->name;
|
2022-11-11 03:13:45 +00:00
|
|
|
$estandar->cabecera = isset($request->cabecera) ? $request->cabecera : $estandar->cabecera;
|
2022-11-14 21:40:44 +00:00
|
|
|
$estandar->id_user = isset($request->id_user) ? $request->id_user : $estandar->id_user;
|
2022-07-09 03:09:04 +00:00
|
|
|
$estandar->save();
|
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"msg" => "!Estandar actualizado",
|
|
|
|
"data" => $estandar,
|
|
|
|
]);
|
2022-11-11 03:13:45 +00:00
|
|
|
} else {
|
2022-07-09 03:09:04 +00:00
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"msg" => "!No se encontro el estandar o no esta autorizado",
|
2022-11-11 03:13:45 +00:00
|
|
|
], 404);
|
2022-07-09 03:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 03:13:45 +00:00
|
|
|
public function deleteEstandar($id)
|
|
|
|
{
|
2022-07-09 03:09:04 +00:00
|
|
|
$id_user = auth()->user()->id;
|
2022-11-11 03:13:45 +00:00
|
|
|
if (Estandar::where(["id" => $id, "id_user" => $id_user])->exists()) {
|
|
|
|
$estandar = Estandar::where(["id" => $id, "id_user" => $id_user])->first();
|
|
|
|
$estandar->delete();
|
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"msg" => "!Estandar eliminado",
|
|
|
|
]);
|
|
|
|
} else {
|
2022-07-09 03:09:04 +00:00
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"msg" => "!No se encontro el estandar o no esta autorizado",
|
2022-11-11 03:13:45 +00:00
|
|
|
], 404);
|
2022-07-09 03:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|