2022-08-02 04:44:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\CausasRaices;
|
2022-08-09 05:04:46 +00:00
|
|
|
use App\Models\plan;
|
2022-08-02 04:44:21 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class CausasRaicesController extends Controller
|
|
|
|
{
|
|
|
|
public function create(Request $request) {
|
|
|
|
$request->validate([
|
2022-08-09 05:04:46 +00:00
|
|
|
"id_plan"=> "required|integer",
|
2022-08-02 04:44:21 +00:00
|
|
|
"descripcion"=> "required",
|
|
|
|
]);
|
2022-08-09 05:04:46 +00:00
|
|
|
$id_user = auth()->user()->id;
|
|
|
|
if(plan::where(["id"=>$request->id_plan])->exists()){
|
|
|
|
$plan = plan::find($request->id_plan);
|
2022-08-17 03:20:04 +00:00
|
|
|
if($plan->id_user == $id_user){
|
2022-08-09 05:04:46 +00:00
|
|
|
$causa = new CausasRaices();
|
|
|
|
$causa->id_plan = $request->id_plan;
|
|
|
|
$causa->descripcion = $request->descripcion;
|
|
|
|
$causa->save();
|
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"message" => "Causa creada exitosamente",
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"message" => "No tienes permisos para crear esta causa",
|
|
|
|
],404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"message" => "No se encontro el plan",
|
|
|
|
],404);
|
|
|
|
}
|
2022-08-02 04:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function update(Request $request){
|
|
|
|
$request->validate([
|
|
|
|
"id"=> "required|integer",
|
|
|
|
"descripcion"=> "required"
|
|
|
|
]);
|
2022-08-09 05:04:46 +00:00
|
|
|
$id_user = auth()->user()->id;
|
|
|
|
if(CausasRaices::where(["id"=>$request->id])->exists()){
|
|
|
|
$causa = CausasRaices::find($request->id);
|
2022-08-17 03:20:04 +00:00
|
|
|
$plan = plan::find($causa->id_plan);
|
|
|
|
if($plan->id_user == $id_user){
|
2022-08-09 05:04:46 +00:00
|
|
|
$causa->descripcion = $request->descripcion;
|
|
|
|
$causa->save();
|
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"message" => "Causa actualizada exitosamente",
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"message" => "No tienes permisos para actualizar esta casua",
|
|
|
|
],404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"message" => "No se encontro la casua",
|
|
|
|
],404);
|
|
|
|
}
|
2022-08-02 04:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($id)
|
|
|
|
{
|
|
|
|
$id_user = auth()->user()->id;
|
2022-08-09 05:04:46 +00:00
|
|
|
if(CausasRaices::where(["id"=>$id])->exists()){
|
|
|
|
$causa = CausasRaices::find($id);
|
|
|
|
$plan = plan::find($causa->id_plan);
|
|
|
|
if($plan->id_user == $id_user){
|
|
|
|
$causa->delete();
|
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"message" => "Causa eliminada exitosamente",
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"message" => "No tienes permisos para eliminar esta causa",
|
|
|
|
],404);
|
|
|
|
}
|
2022-08-02 04:44:21 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
return response([
|
|
|
|
"status" => 0,
|
2022-08-09 05:04:46 +00:00
|
|
|
"message" => "No se encontro la casua",
|
2022-08-02 04:44:21 +00:00
|
|
|
],404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|