unsa-pis-be/app/Http/Controllers/Api/NarrativasController.php

136 lines
3.9 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Estandar;
use App\Models\Narrativa;
2022-11-16 20:59:38 +00:00
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Validator;
2022-10-20 07:34:46 +00:00
2022-10-17 15:56:03 +00:00
class NarrativasController extends Controller
{
2022-10-17 15:56:03 +00:00
public function create(Request $request)
{
2022-11-16 20:59:38 +00:00
$id_user = auth()->user();
if ($id_user->isAdmin()) {
$validator = Validator::make($request->all(), [
"id_estandar" => "required|integer|exists:estandars,id",
"contenido" => "required",
"semestre" => [
'required',
Rule::unique('narrativas', 'semestre')->where(function ($query) use ($request) {
return $query->where('id_estandar', $request->id_estandar);
}),
],
]);
if ($validator->fails()) {
return response([
"status" => "error",
"message" => $validator->errors()
], 400);
}
2022-10-17 15:56:03 +00:00
$narrativa = new Narrativa();
$narrativa->id_estandar = $request->id_estandar;
$narrativa->semestre = $request->semestre;
$narrativa->contenido = $request->contenido;
$narrativa->save();
return response([
"status" => 1,
"msg" => "!Narrativa creada exitosamente",
"data" => $narrativa,
2022-11-16 20:59:38 +00:00
], 200);
2022-10-17 15:56:03 +00:00
} else {
return response([
"status" => 0,
2022-11-16 20:59:38 +00:00
"msg" => "No tiene permisos para crear una narrativa",
"data" => null,
], 200);
}
2022-10-17 15:56:03 +00:00
}
2022-11-16 20:59:38 +00:00
2022-10-17 15:56:03 +00:00
public function update(Request $request)
{
$request->validate([
2022-11-16 20:59:38 +00:00
"id" => "required|exists:narrativas,id",
2022-10-17 15:56:03 +00:00
"contenido" => "required",
]);
2022-10-17 15:56:03 +00:00
if (Narrativa::where("id", $request->id)->exists()) {
$narrativa = Narrativa::find($request->id);
$narrativa->update([
"contenido" => $request->contenido,
]);
return response()->json($narrativa, 200);
} else {
return response([
"status" => 0,
"message" => "!No se encontro la narrativa",
2022-10-17 15:56:03 +00:00
], 404);
}
}
2022-11-16 20:59:38 +00:00
2022-10-17 15:56:03 +00:00
public function delete($id)
{
if (Narrativa::where("id", $id)->exists()) {
$narrativa = Narrativa::find($id);
$narrativa->delete();
return response([
"status" => 1,
"message" => "!Narrativa eliminada",
]);
} else {
return response([
"status" => 0,
"message" => "!No se encontro la narrativa",
2022-10-17 15:56:03 +00:00
], 404);
}
}
2022-11-16 20:59:38 +00:00
2022-10-17 15:56:03 +00:00
public function show($id)
{
if (Narrativa::where("id", $id)->exists()) {
$narrativa = Narrativa::find($id);
return response([
"status" => 1,
"message" => "!Narrativa encontrada",
"data" => $narrativa,
]);
2022-10-17 15:56:03 +00:00
} else {
return response([
"status" => 0,
"message" => "!No se encontro la narrativa",
], 404);
}
2022-10-17 15:56:03 +00:00
}
2022-11-16 20:59:38 +00:00
2022-10-17 15:56:03 +00:00
public function listNarrativas()
{
$narrativas = Narrativa::all();
2022-10-17 15:11:50 +00:00
return response([
"status" => 1,
2022-10-17 15:56:03 +00:00
"message" => "!Lista de narrativas",
2022-10-17 15:11:50 +00:00
"data" => $narrativas,
]);
}
2022-11-16 20:59:38 +00:00
public function ultimaNarrativa(Request $request)
{
$request->validate([
2022-11-15 00:10:16 +00:00
"id_estandar" => 'required|exists:App\Models\Estandar,id',
]);
2022-11-16 20:59:38 +00:00
$narrativa = Narrativa::where("id_estandar", $request->id_estandar)->latest()->first();
return response([
2022-11-15 00:10:16 +00:00
"status" => 1,
2022-11-16 20:59:38 +00:00
"message" => "!Ultima Narrativa del estandar " . $request->id_estandar,
2022-11-15 00:10:16 +00:00
"data" => $narrativa,
]);
2022-11-16 20:59:38 +00:00
}
}