Crud de evidencias (download), correccion de EstandarController

This commit is contained in:
cgonzalesmo 2022-08-16 20:11:22 -05:00
parent a055178f93
commit 6a2285690c
3 changed files with 173 additions and 5 deletions

View File

@ -67,7 +67,7 @@ public function updateEstandar(Request $request, $id){
$id_user = auth()->user()->id; $id_user = auth()->user()->id;
if(Estandar::where(["id_user"=>$id_user,"id"=>$id])->exists()){ if(Estandar::where(["id_user"=>$id_user,"id"=>$id])->exists()){
$estandar = Estandar::find($id); $estandar = Estandar::find($id);
$estandar->name = isset($request->name) ? $request->name : $estandar->title; $estandar->name = isset($request->name) ? $request->name : $estandar->name;
$estandar->save(); $estandar->save();
return response([ return response([
"status" => 1, "status" => 1,

View File

@ -0,0 +1,161 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Evidencias;
use App\Models\plan;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Response as FacadeResponse;
use Illuminate\Http\Request;
class EvidenciasController extends Controller
{
public function create(Request $request){
$request->validate([
"id_plan"=> "required|integer",
"codigo"=>"required",
"denominacion"=>"required",
"adjunto"=>"required",
]);
$id_user = auth()->user()->id;
if(plan::where(["id"=>$request->id_plan])->exists()){
$plan = plan::find($request->id_plan);
if($plan->id_user == $id_user){
$evidencia = new Evidencias();
$evidencia->id_plan = $request->id_plan;
$evidencia->codigo = $request->codigo;
$evidencia->denominacion = $request->denominacion;
$path = $request->adjunto->store('evidencias');
$evidencia->adjunto = $path;
$evidencia->id_user = $id_user;
$evidencia->save();
return response([
"status" => 1,
"message" => "Evidencia creada exitosamente",
]);
}
else{
return response([
"status" => 0,
"message" => "No tienes permisos para crear esta Evidencia",
],404);
}
}
else{
return response([
"status" => 0,
"message" => "No se encontro el plan",
],404);
}
}
public function show($id){
if(Evidencias::where("id",$id)->exists()){
$evidencia = Evidencias::find($id);
//Para retornar nombre de user
/*$user = User::find($evidencia->id_user);
$evidencia->id_user = $user->name;*/
return response([
"status" => 1,
"msg" => "!Evidencia",
"data" => $evidencia,
]);
}
else{
return response([
"status" => 0,
"msg" => "!No se encontro el evidencia",
],404);
}
}
public function update(Request $request){
$request->validate([
"id"=> "required|integer",
"codigo"=> "required",
"denominacion"=> "required",
"adjunto"=> "required"
]);
$id_user = auth()->user()->id;
if(Evidencias::where(["id"=>$request->id])->exists()){
$evidencia = Evidencias::find($request->id);
$plan = plan::find($evidencia->id_plan);
if($plan->id_user == $id_user){
$evidencia->codigo = $request->codigo;
$evidencia->denominacion = $request->denominacion;
$path = $request->adjunto->store('evidencias');
$evidencia->adjunto = $path;
$evidencia->save();
return response([
"status" => 1,
"message" => "Evidencia actualizada exitosamente",
]);
}
else{
return response([
"status" => 0,
"message" => "No tienes permisos para actualizar esta evidencia",
],404);
}
}
else{
return response([
"status" => 0,
"message" => "No se encontro la evidencia",
],404);
}
}
public function delete($id)
{
$id_user = auth()->user()->id;
if(Evidencias::where(["id"=>$id])->exists()){
$evidencia = Evidencias::find($id);
$plan = plan::find($evidencia->id_plan);
if($plan->id_user == $id_user){
$evidencia->delete();
return response([
"status" => 1,
"message" => "Evidencia eliminada exitosamente",
]);
}
else{
return response([
"status" => 0,
"message" => "No tienes permisos para eliminar esta evidencia",
],404);
}
}
else{
return response([
"status" => 0,
"message" => "No se encontro la evidencia",
],404);
}
}
public function download($id){
if(Evidencias::where("id",$id)->exists()){
$evidencia = Evidencias::find($id);
$path = storage_path('app/'.$evidencia->adjunto);
//$evidencia->adjunto = download($path);
return response()->download($path);
}
else{
return response([
"status" => 0,
"msg" => "!No se encontro la evidencia",
],404);
}
}
}

View File

@ -16,6 +16,7 @@
use App\Http\Controllers\Api\ResponsablesValoresController; use App\Http\Controllers\Api\ResponsablesValoresController;
use App\Http\Controllers\Api\ResponsablesController; use App\Http\Controllers\Api\ResponsablesController;
use App\Http\Controllers\Api\EstadosValoresController; use App\Http\Controllers\Api\EstadosValoresController;
use App\Http\Controllers\Api\EvidenciasController;
Route::post('register', [UserController::class, 'register']); Route::post('register', [UserController::class, 'register']);
@ -89,6 +90,12 @@
Route::put('responsable',[ResponsablesController::class,'update']); Route::put('responsable',[ResponsablesController::class,'update']);
Route::delete('responsable/{id}',[ResponsablesController::class,'delete']); Route::delete('responsable/{id}',[ResponsablesController::class,'delete']);
//ruta evidencias
Route::post('evidencia',[EvidenciasController::class,'create']);
Route::get('evidencia/download/{id}', [EvidenciasController::class,'download']);
Route::get('evidencia/{id}', [EvidenciasController::class,'show']);
Route::put('evidencia',[EvidenciasController::class,'update']);
Route::delete('evidencia/{id}',[EvidenciasController::class,'delete']);
}); });
Route::middleware('auth:sanctum')->get('/user', function (Request $request) { Route::middleware('auth:sanctum')->get('/user', function (Request $request) {