102 lines
2.8 KiB
PHP
102 lines
2.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Models\Unit;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use App\Support\ApiDataTransformer;
|
||
|
|
use Illuminate\Support\Facades\Validator;
|
||
|
|
|
||
|
|
class UnitController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Display a listing of the resource.
|
||
|
|
*/
|
||
|
|
public function index()
|
||
|
|
{
|
||
|
|
$units = Unit::all()->toArray();
|
||
|
|
return ApiDataTransformer::snakeToCamel($units);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Store a newly created resource in storage.
|
||
|
|
*/
|
||
|
|
public function store(Request $request)
|
||
|
|
{
|
||
|
|
$validator = Validator::make($request->all(), [
|
||
|
|
'name' => 'required|string|max:50|unique:units',
|
||
|
|
'symbol' => 'nullable|string|max:10',
|
||
|
|
]);
|
||
|
|
|
||
|
|
if ($validator->fails()) {
|
||
|
|
return response()->json([
|
||
|
|
'message' => 'Validation failed',
|
||
|
|
'errors' => $validator->errors()
|
||
|
|
], 422);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Wandeln Sie die Daten von camelCase in snake_case um
|
||
|
|
$data = ApiDataTransformer::camelToSnake($request->all());
|
||
|
|
$unit = Unit::create($data);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'message' => 'Unit created successfully',
|
||
|
|
'data' => ApiDataTransformer::snakeToCamel($unit->toArray())
|
||
|
|
], 201);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Display the specified resource.
|
||
|
|
*/
|
||
|
|
public function show(Unit $unit)
|
||
|
|
{
|
||
|
|
return ApiDataTransformer::snakeToCamel($unit->toArray());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the specified resource in storage.
|
||
|
|
*/
|
||
|
|
public function update(Request $request, Unit $unit)
|
||
|
|
{
|
||
|
|
$validator = Validator::make($request->all(), [
|
||
|
|
'name' => 'sometimes|required|string|max:50|unique:units,name,' . $unit->id,
|
||
|
|
'symbol' => 'nullable|string|max:10',
|
||
|
|
]);
|
||
|
|
|
||
|
|
if ($validator->fails()) {
|
||
|
|
return response()->json([
|
||
|
|
'message' => 'Validation failed',
|
||
|
|
'errors' => $validator->errors()
|
||
|
|
], 422);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Wandeln Sie die Daten von camelCase in snake_case um
|
||
|
|
$data = ApiDataTransformer::camelToSnake($request->all());
|
||
|
|
$unit->update($data);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'message' => 'Unit updated successfully',
|
||
|
|
'data' => ApiDataTransformer::snakeToCamel($unit->toArray())
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Remove the specified resource from storage.
|
||
|
|
*/
|
||
|
|
public function destroy(Unit $unit)
|
||
|
|
{
|
||
|
|
// Check if the unit is used by any products or line items
|
||
|
|
if ($unit->products()->exists() || $unit->lineItems()->exists()) {
|
||
|
|
return response()->json([
|
||
|
|
'message' => 'Cannot delete unit as it is used by products or line items'
|
||
|
|
], 409);
|
||
|
|
}
|
||
|
|
|
||
|
|
$unit->delete();
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'message' => 'Unit deleted successfully'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|