Custom Backend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.9 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\UserToVersionGantt;
class UserToVersionGanttController extends Controller
{
public function add(Request $request){
$this->validate($request, [
'user_id' => 'required',
'version_gantt_id' => 'required'
]);
$data = $request->all();
$data['created_by'] = $this->currentName;
$result = UserToVersionGantt::create($data);
if($result){
return response()->json(['status'=>'success','message'=>'user to version gantt Project successfull created','code'=>200]);
}else{
return response()->json(['status'=>'failed','message'=>'user to version gantt Project failed created','code'=>400]);
}
}
public function addMultiple(Request $request){
$users_id = $request->user_id;
$data = UserToVersionGantt::where("version_gantt_id", $request->version_gantt_id);
if($data){
$data->delete();
}
if(is_array($users_id) && count($users_id) > 0){
$countRes = 0;
foreach($users_id as $item){
$dataInsert = array(
"user_id" => $item,
"version_gantt_id" => $request->version_gantt_id,
"created_by" => $this->currentName
);
$result = UserToVersionGantt::create($dataInsert);
if($result){
$countRes++;
}else{
$countRes--;
}
}
if($countRes > 0){
return response()->json(['status'=>'success' ,'message'=>'user to version gantt Project successfull created','code'=>200]);
}else{
return response()->json(['status'=>'success' ,'message'=>'user to version gantt Project failed created','code'=>400]);
}
}else{
return response()->json(['status'=>'success' ,'message'=>'user to version gantt Project successfull created','code'=>200]);
}
}
public function update(Request $request, $id){
if(!$id || (int) $id < 0 || $id==""){
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
}
$data = UserToVersionGantt::find($id);
if($data){
$result = $data->update($request->all());
}else{
return response()->json(['status'=>'failed','message'=>'data user to version gantt Project not found!','code'=>400], 400);
die();
}
if($result){
return response()->json(['status'=>'success','message'=>'user to version gantt Project successfully updated!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'user to version gantt Project failed updated!','code'=>400], 400);
}
}
public function delete($id)
{
$data = UserToVersionGantt::find($id);
if($data){
$delete = $data->delete();
}else{
return response()->json(['status'=>'failed','message'=>'data user to version gantt Project not found!','code'=>400], 400);
die();
}
if($delete){
return response()->json(['status'=>'success','message'=>'user to version gantt Project successfully deleted!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'user to version gantt Project failed deleted!','code'=>400], 400);
}
}
public function edit($id){
if(!$id || (int) $id < 0 || $id==""){
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
die();
}
$result = UserToVersionGantt::find($id);
if($result){
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get data tools resource, please try again later!','code'=>400], 400);
}
}
public function search(Request $request)
{
$payload = $request->all();
$dataBuilder = $this->setUpPayload($payload, 'user_to_version_gantt');
$builder = $dataBuilder['builder'];
$countBuilder = $dataBuilder['count'];
$dataGet = $builder->get();
$totalRecord = $countBuilder->count();
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200);
}
public function list()
{
$data = UserToVersionGantt::all();
$countData = $data->count();
if($data){
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get list user to version gantt, please try again later!','code'=>400], 400);
}
}
public function getByUserGantt($gantt_id)
{
$user_id = $this->currentId;
if($user_id > 0){
$cekData = UserToVersionGantt::where("version_gantt_id", $gantt_id)->where("user_id", $user_id)->count();
if($cekData <=0){
$data = array(
"status"=>"not have"
);
}else{
$data = UserToVersionGantt::where("version_gantt_id", $gantt_id)->where("user_id", $user_id)->first();
}
return response()->json(['status'=>'success','code'=>200,'data'=>$data], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get list user to version gantt, please try again later!','code'=>400], 400);
}
}
}