Create routes and views for customers and their addresses.
This commit is contained in:
66
app/Http/Controllers/Api/AddressController.php
Normal file
66
app/Http/Controllers/Api/AddressController.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Address;
|
||||
use App\Models\Customer;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class AddressController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Customer $customer): JsonResponse
|
||||
{
|
||||
return response()->json($customer->addresses);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request, Customer $customer): JsonResponse
|
||||
{
|
||||
$addressData = $request->validate([
|
||||
'name' => 'required|string',
|
||||
'email' => 'required|string|email',
|
||||
'phone' => 'string',
|
||||
'address' => 'string',
|
||||
'city' => 'string',
|
||||
'zip' => 'numeric',
|
||||
'is_address' => 'boolean',
|
||||
'is_delivery' => 'boolean',
|
||||
]);
|
||||
|
||||
if (isset($addressData['is_address']) && $addressData['is_address'] == 1) {
|
||||
$customer->addresses()->where('is_address', true)->update(['is_address' => false]);
|
||||
}
|
||||
|
||||
if (isset($addressData['is_delivery']) && $addressData['is_delivery'] == 1) {
|
||||
$customer->addresses()->where('is_delivery', true)->update(['is_delivery' => false]);
|
||||
}
|
||||
|
||||
$address = $customer->addresses()->create($addressData);
|
||||
return response()->json($address);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Address $address): JsonResponse
|
||||
{
|
||||
return response()->json($address);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(Address $address): JsonResponse
|
||||
{
|
||||
$address->delete();
|
||||
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
}
|
||||
69
app/Http/Controllers/Api/CustomerController.php
Normal file
69
app/Http/Controllers/Api/CustomerController.php
Normal 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);
|
||||
}
|
||||
}
|
||||
34
app/Http/Controllers/CustomerController.php
Normal file
34
app/Http/Controllers/CustomerController.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Customer;
|
||||
use Illuminate\Contracts\View\View;
|
||||
|
||||
class CustomerController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
return view('customer.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('customer.create');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Customer $customer): View
|
||||
{
|
||||
return view('customer.edit', ['customer' => $customer->load(['addresses'])]);
|
||||
}
|
||||
}
|
||||
@@ -8,4 +8,20 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
class Address extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'is_address',
|
||||
'is_delivery',
|
||||
'name',
|
||||
'email',
|
||||
'phone',
|
||||
'address',
|
||||
'city',
|
||||
'state',
|
||||
'country',
|
||||
'zip',
|
||||
|
||||
];
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,9 +3,77 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class Customer extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that are appended with attribute getters.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $appends = [
|
||||
'created',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the created_at attribute in local time format.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCreatedAttribute(): string
|
||||
{
|
||||
return $this->created_at->format('d.m.Y');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the invoice address.
|
||||
*
|
||||
* @return HasOne
|
||||
*/
|
||||
public function address(): HasOne
|
||||
{
|
||||
return $this->hasOne(Address::class)->ofMany([],
|
||||
function (Builder $query) {
|
||||
$query->where('is_address', '=', true);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the delivery address.
|
||||
*
|
||||
* @return HasOne
|
||||
*/
|
||||
public function delivery(): HasOne
|
||||
{
|
||||
return $this->hasOne(Address::class)->ofMany([],
|
||||
function (Builder $query) {
|
||||
$query->where('is_delivery', '=', true);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all customer's addresses.
|
||||
*
|
||||
* @return HasMany
|
||||
*/
|
||||
public function addresses(): HasMany
|
||||
{
|
||||
return $this->hasMany(Address::class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user