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

105 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2022-08-01 01:21:58 +00:00
<?php
2022-08-05 06:25:59 +00:00
namespace App\Http\Controllers\Api;
2022-08-01 01:21:58 +00:00
2022-08-05 06:25:59 +00:00
use App\Http\Controllers\Controller;
2022-08-01 01:21:58 +00:00
use App\Models\Metas;
2022-08-05 06:25:59 +00:00
use App\Models\plan;
2022-08-01 01:21:58 +00:00
use Illuminate\Http\Request;
class MetasController extends Controller {
public function create(Request $request) {
$request->validate([
2022-08-05 06:25:59 +00:00
"id_plan"=> "required|integer",
2022-08-01 01:21:58 +00:00
"descripcion"=> "required",
]);
2022-08-05 06:25:59 +00:00
$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){
$meta = new Metas();
$meta->id_plan = $request->id_plan;
$meta->descripcion = $request->descripcion;
$meta->save();
return response([
"status" => 1,
"message" => "Meta creada exitosamente",
]);
}
else{
return response([
"status" => 0,
"message" => "No tienes permisos para crear esta meta",
],404);
}
}
else{
return response([
"status" => 0,
"message" => "No se encontro el plan",
],404);
}
2022-08-01 01:21:58 +00:00
}
public function update(Request $request){
$request->validate([
"id"=> "required|integer",
"descripcion"=> "required"
]);
2022-08-05 06:25:59 +00:00
//
$id_user = auth()->user()->id;
if(Metas::where(["id"=>$request->id])->exists()){
$meta = Metas::find($request->id);
$plan = plan::find($meta->id_plan);
if($plan->id_user == $id_user){
$meta->descripcion = $request->descripcion;
$meta->save();
return response([
"status" => 1,
"message" => "Meta actualizada exitosamente",
]);
}
else{
return response([
"status" => 0,
"message" => "No tienes permisos para actualizar esta meta",
],404);
}
}
else{
return response([
"status" => 0,
"message" => "No se encontro la meta",
],404);
}
2022-08-01 01:21:58 +00:00
}
2022-08-05 06:25:59 +00:00
public function delete($id){
2022-08-01 01:21:58 +00:00
$id_user = auth()->user()->id;
2022-08-05 06:25:59 +00:00
if(Metas::where(["id"=>$id])->exists()){
$meta = Metas::find($id);
$plan = plan::find($meta->id_plan);
if($plan->id_user == $id_user){
$meta->delete();
return response([
"status" => 1,
"message" => "Meta eliminada exitosamente",
]);
}
else{
return response([
"status" => 0,
"message" => "No tienes permisos para eliminar esta meta",
],404);
}
2022-08-01 01:21:58 +00:00
}
else{
return response([
"status" => 0,
2022-08-05 06:25:59 +00:00
"message" => "No se encontro la meta",
2022-08-01 01:21:58 +00:00
],404);
}
}
}