This commit is contained in:
Bozhidar Slaveykov 2024-04-03 12:42:32 +03:00
parent cda7ccb78c
commit f8728818f8
5 changed files with 103 additions and 0 deletions

View file

@ -3,8 +3,10 @@
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\Request\CustomerCreateRequest;
use App\Http\Controllers\ApiController;
use App\Models\Customer;
use Illuminate\Http\Request;
class CustomersController extends ApiController
{
@ -32,4 +34,41 @@ class CustomersController extends ApiController
]);
}
/**
* @OA\Post(
* path="/api/customers",
* @OA\Response(
* response=200,
* description="Successful operation",
* ),
* @OA\PathItem (
* ),
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* required={"name","email","phone"},
* @OA\Property(property="name", type="string", example="John Doe", description="Name of the customer"),
* @OA\Property(property="email", type="string", example="jhon@gmail.com", description="Email of the customer"),
* @OA\Property(property="phone", type="string", example="1234567890", description="Phone of the customer")
* )
* )
* )
*/
public function store(CustomerCreateRequest $request)
{
$customer = new Customer();
$customer->name = $request->name;
$customer->email = $request->email;
$customer->phone = $request->phone;
$customer->save();
return response()->json([
'status' => 'ok',
'message' => 'Customer created',
'data' => [
'customer' => $customer,
]
]);
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace App\Http\Controllers\Api\Request;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
class ApiRequest extends FormRequest
{
public function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json([
'error' => true,
'message' => $validator->errors()->first(),
'data' => $validator->errors(),
]));
}
}

View file

@ -0,0 +1,22 @@
<?php
namespace App\Http\Controllers\Api\Request;
class AuthorizedApiRequest extends ApiRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
$user = auth()->user();
if ($user) {
return true;
}
return false;
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace App\Http\Controllers\Api\Request;
class CustomerCreateRequest extends AuthorizedApiRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email|unique:customers,email',
'phone' => 'required',
];
}
}

View file

@ -17,6 +17,10 @@ use Illuminate\Support\Facades\Route;
Route::get('health', [\App\Http\Controllers\Api\HealthController::class, 'index']);
Route::get('customers', [\App\Http\Controllers\Api\CustomersController::class, 'index']);
Route::post('customers', [\App\Http\Controllers\Api\CustomersController::class, 'store']);
Route::get('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'show']);
Route::put('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'update']);
Route::delete('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'destroy']);
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();