Create routes and views for customers and their addresses.

This commit is contained in:
2024-12-10 21:09:12 +01:00
parent 9d2eddbcf1
commit 679a067506
16 changed files with 797 additions and 3 deletions

View File

@@ -0,0 +1,69 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Customer;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\Rule;
class CustomerController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(): JsonResponse
{
return response()->json(Customer::with(['address', 'delivery'])->orderBy('name')->get());
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): JsonResponse
{
$customerData = $request->validate([
'name' => 'required|string',
'email' => 'required|string|email|unique:customers'
]);
$customer = new Customer($customerData);
$customer->save();
return response()->json($customer);
}
/**
* Display the specified resource.
*/
public function show(Customer $customer): JsonResponse
{
return response()->json($customer);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Customer $customer): JsonResponse
{
$customerData = $request->validate([
'name' => 'required|string',
'email' => ['required', 'string', 'email', Rule::unique('customers')->ignore($customer->id)],
]);
$customer->update($customerData);
return response()->json($customer);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Customer $customer): JsonResponse
{
$customer->delete();
return response()->json(null, 204);
}
}