Skip to content

Commit

Permalink
bill of materials end point added
Browse files Browse the repository at this point in the history
  • Loading branch information
maqndon committed Sep 14, 2024
1 parent abd22a8 commit 195f3b0
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
33 changes: 28 additions & 5 deletions app/Http/Controllers/Api/BomController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Http\Resources\BomResource;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bom\StoreBomRequest;
use App\Http\Requests\Bom\UpdateBomRequest;

class BomController extends Controller
{
Expand All @@ -29,6 +30,18 @@ public function show(Product $product)
public function store(StoreBomRequest $request, Product $product)
{
try {
$existingBom = Bom::where([
'bomable_type' => Product::class,
'bomable_id' => $product->id,
'item' => $request->input('item'),
])->first();

if ($existingBom) {
return response()->json([
'message' => 'Material ' . $request->item . ' already exists',
], 409);
}

$bom = new Bom();
$bom->bomable_type = Product::class;
$bom->bomable_id = $product->id;
Expand All @@ -48,16 +61,26 @@ public function store(StoreBomRequest $request, Product $product)
}
}

public function update(Product $product, Bom $bom)
public function update(UpdateBomRequest $request, Product $product, Bom $bom)
{
try {
$product->bill_of_materials()->update([$bom->id]);
// Filter the data that has changed
$data = array_filter($request->only([
'qty',
'item',
]), function ($value) {
return !is_null($value);
});

// Update the category with the filtered data
$bom->update($data);

return response()->json([
'message' => 'Bom ' . $bom->name . ' added to ' . $product->title . ' successfully',
'message' => 'Material ' . $bom->item . ' added to ' . $product->title . ' successfully',
], 201);
} catch (\Throwable $th) {
// Log error and return a JSON response
\Log::error('Error adding Bill of Material: ' . $th->getMessage());
\Log::error('Error adding material: ' . $th->getMessage());
return response()->json([
'message' => 'Material could not be added successfully',
], 500);
Expand All @@ -73,7 +96,7 @@ public function destroy(Product $product, Bom $bom)
], 201);
} catch (\Throwable $th) {
// Log error and return a JSON response
\Log::error('Error removing Material: ' . $th->getMessage());
\Log::error('Error removing material: ' . $th->getMessage());
return response()->json([
'message' => 'Material ' . $bom->item . ' could not be removed successfully',
], 500);
Expand Down
29 changes: 29 additions & 0 deletions app/Http/Requests/Bom/StoreBomRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Requests\Bom;

use Illuminate\Foundation\Http\FormRequest;

class StoreBomRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'qty' => 'required|numeric',
'item' => 'required|string',
];
}
}
49 changes: 49 additions & 0 deletions app/Http/Requests/Bom/UpdateBomRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Http\Requests\Bom;

use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;

class UpdateBomRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
# update HTTP method used
$method = $this->method();

if ($method == 'PUT') {
return [
'qty' => 'required|numeric',
'item' => [
'required',
'string',
Rule::unique('boms', 'item')->ignore($this->input('item'), 'item')
],
];
} else {
return [
'qty' => 'sometimes|required|numeric',
'item' => [
'sometimes',
'required',
'string',
Rule::unique('boms', 'item')->ignore($this->input('item'), 'item')
],
];
}
}
}

0 comments on commit 195f3b0

Please sign in to comment.