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.
 
 
 
 

54 lines
2.1 KiB

<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\MaterialProject;
class MaterialProjectController extends Controller
{
public function __invoke(Request $request)
{
$projectId = $request->project_id;
$subproyekId = $request->subproyek_id;
$listMaterialId = $request->material_id;
if (!$projectId && !$subproyekId) {
return response()->json(['status' => 'failed', 'message' => 'Required proyek_id or subproyek id', 'code' => 400]);
}
if (is_array($listMaterialId) && count($listMaterialId) > 0) {
$result = 0;
MaterialProject::where('proyek_id', $projectId)->delete();
MaterialProject::where('proyek_id', $subproyekId)->delete();
foreach ($listMaterialId as $material_id) {
$data = [
'material_resource_id' => (int)$material_id
];
if ($projectId && $projectId != "" && (int)$projectId > 0) {
$data['proyek_id'] = $projectId;
} else if ($subproyekId && $subproyekId != "" && (int)$subproyekId > 0) {
$data['subproyek_id'] = $subproyekId;
}
$insert = MaterialProject::create($data);
if ($insert) {
$result++;
} else {
$result--;
}
}
if ($result > 0) {
return response()->json(['status' => 'success', 'message' => 'Material Project successfull updated', 'code' => 200]);
} else {
return response()->json(['status' => 'failed', 'message' => 'Material Project failed updated', 'code' => 400]);
}
} else {
MaterialProject::where('proyek_id', $projectId)->delete();
MaterialProject::where('proyek_id', $subproyekId)->delete();
return response()->json(['status' => 'success', 'message' => 'Material Project Successfull updated', 'code' => 200]);
}
}
}