From ad398808a58f26d5723ae807dd457291520117a6 Mon Sep 17 00:00:00 2001 From: Tim Slager Date: Thu, 14 Dec 2023 15:36:57 +0100 Subject: [PATCH] Controllers fuck de item controller. Hier is alle controllers. Pages moeten we idd nog ff instellen. --- src/app/Enums/ModifiedEnum.php | 10 +++ src/app/Enums/ResponseStatus.php | 3 + src/app/Http/Controllers/FineController.php | 44 ++++++++++-- src/app/Http/Controllers/GrantController.php | 45 +++++++++++-- src/app/Http/Controllers/ItemController.php | 67 ++++++++++++++----- .../Controllers/LibraryPassController.php | 47 +++++++++++-- .../PaymentTransactionController.php | 45 +++++++++++-- .../Controllers/ReservationController.php | 46 +++++++++++-- .../Http/Controllers/SessionController.php | 8 +-- ...adRequest.php => AuthorizePassRequest.php} | 2 +- src/app/Http/Requests/FineRequest.php | 29 ++++++++ src/app/Http/Requests/GrantRequest.php | 31 +++++++++ src/app/Http/Requests/ItemRequest.php | 35 ++++++++++ src/app/Http/Requests/LibraryPassRequest.php | 31 +++++++++ .../Requests/PaymentTransactionRequest.php | 28 ++++++++ src/app/Http/Requests/ReserveRequest.php | 31 +++++++++ src/app/Traits/CommonTrait.php | 6 +- .../2023_12_04_125056_create_fines_table.php | 2 +- 18 files changed, 456 insertions(+), 54 deletions(-) create mode 100644 src/app/Enums/ModifiedEnum.php rename src/app/Http/Requests/{LibraryPassReadRequest.php => AuthorizePassRequest.php} (92%) create mode 100644 src/app/Http/Requests/FineRequest.php create mode 100644 src/app/Http/Requests/GrantRequest.php create mode 100644 src/app/Http/Requests/ItemRequest.php create mode 100644 src/app/Http/Requests/LibraryPassRequest.php create mode 100644 src/app/Http/Requests/PaymentTransactionRequest.php create mode 100644 src/app/Http/Requests/ReserveRequest.php diff --git a/src/app/Enums/ModifiedEnum.php b/src/app/Enums/ModifiedEnum.php new file mode 100644 index 0000000..a886364 --- /dev/null +++ b/src/app/Enums/ModifiedEnum.php @@ -0,0 +1,10 @@ + 200, + static::created => 201, static::error => 400, static::unauthorized => 401, static::forbidden => 403, @@ -30,6 +32,7 @@ public function label(): string return match ($this) { static::success => 'Success', static::error => 'Error', + static::created => 'Created', static::unauthorized => 'Unauthorized', static::forbidden => 'Forbidden', static::notFound => 'Not Found', diff --git a/src/app/Http/Controllers/FineController.php b/src/app/Http/Controllers/FineController.php index cbfbf79..70204d7 100644 --- a/src/app/Http/Controllers/FineController.php +++ b/src/app/Http/Controllers/FineController.php @@ -2,11 +2,16 @@ namespace App\Http\Controllers; +use App\Enums\ModifiedEnum; +use App\Enums\ResponseStatus; +use App\Http\Requests\FineRequest; use App\Models\Fine; use Illuminate\Http\Request; +use App\Traits\CommonTrait; class FineController extends Controller { + use CommonTrait; /** * Display a listing of the resource. */ @@ -26,9 +31,21 @@ public function create() /** * Store a newly created resource in storage. */ - public function store(Request $request) + public function store(FineRequest $request) { - // + $validated = $request->validated(); + $fine = Fine::create([ + 'grant_id' => $validated['grant_id'], + 'amount' => $validated['amount'], + 'modified_kind' => ModifiedEnum::inserted, + 'modified_user' => Auth()->id(), + ]); + + return $this->CommonResponse( + ResponseStatus::created, + 'Fine created successfully', + ['fine' => $fine], + ); } /** @@ -52,7 +69,20 @@ public function edit(Fine $fine) */ public function update(Request $request, Fine $fine) { - // + $validated = $request->validated(); + + //TODO: Check: do we want to update the grant_id? + $fine->update([ + 'amount' => $validated['amount'], + 'modified_kind' => ModifiedEnum::modified, + 'modified_user' => Auth()->id(), + ]); + + return $this->CommonResponse( + ResponseStatus::success, + 'Fine updated successfully', + ['fine' => $fine], + ); } /** @@ -60,6 +90,12 @@ public function update(Request $request, Fine $fine) */ public function destroy(Fine $fine) { - // + $fine->delete(); + + return $this->CommonResponse( + ResponseStatus::success, + 'Fine deleted successfully', + null, + ); } } diff --git a/src/app/Http/Controllers/GrantController.php b/src/app/Http/Controllers/GrantController.php index 58d0395..a7ce175 100644 --- a/src/app/Http/Controllers/GrantController.php +++ b/src/app/Http/Controllers/GrantController.php @@ -2,11 +2,16 @@ namespace App\Http\Controllers; +use App\Enums\ModifiedEnum; +use App\Enums\ResponseStatus; +use App\Http\Requests\GrantRequest; use App\Models\Grant; use Illuminate\Http\Request; +use App\Traits\CommonTrait; class GrantController extends Controller { + use CommonTrait; /** * Display a listing of the resource. */ @@ -26,9 +31,21 @@ public function create() /** * Store a newly created resource in storage. */ - public function store(Request $request) + public function store(GrantRequest $request) { - // + $validated = $request->validated(); + $grant = Grant::create([ + 'user_id' => $validated['user_id'], + 'item_id' => $validated['item_id'], + 'borrowed_date' => $validated['borrowed_date'], + 'return_date' => $validated['return_date'], + 'modified_kind' => ModifiedEnum::inserted, + 'modified_user' => Auth()->id(), + ]); + return $this->CommonResponse( + ResponseStatus::created, + 'Grant created successfully', + $grant); } /** @@ -50,9 +67,23 @@ public function edit(Grant $grant) /** * Update the specified resource in storage. */ - public function update(Request $request, Grant $grant) + public function update(GrantRequest $request, Grant $grant) { - // + $validated = $request->validated(); + + $grant->update([ + 'user_id' => $validated['user_id'], + 'item_id' => $validated['item_id'], + 'borrowed_date' => $validated['borrowed_date'], + 'return_date' => $validated['return_date'], + 'modified_kind' => ModifiedEnum::modified, + 'modified_user' => Auth()->id(), + ]); + + return $this->CommonResponse( + ResponseStatus::success, + 'Grant updated successfully', + ['grant' => $grant]); } /** @@ -60,6 +91,10 @@ public function update(Request $request, Grant $grant) */ public function destroy(Grant $grant) { - // + $grant->delete(); + return $this->CommonResponse( + ResponseStatus::success, + 'Grant deleted successfully', + null); } } diff --git a/src/app/Http/Controllers/ItemController.php b/src/app/Http/Controllers/ItemController.php index 6b049c9..5994b9d 100644 --- a/src/app/Http/Controllers/ItemController.php +++ b/src/app/Http/Controllers/ItemController.php @@ -2,17 +2,21 @@ namespace App\Http\Controllers; +use App\Enums\ModifiedEnum; +use App\Enums\ResponseStatus; +use App\Http\Requests\ItemRequest; use App\Models\Item; -use Illuminate\Http\Request; +use App\Traits\CommonTrait; class ItemController extends Controller { + use CommonTrait; /** * Display a listing of the resource. */ public function index() { - // + // render page } /** @@ -20,15 +24,28 @@ public function index() */ public function create() { - // + // render page } /** * Store a newly created resource in storage. */ - public function store(Request $request) + public function store(ItemRequest $request) { - // + $validated = $request->validated(); + + $item = Item::create([ + 'Identifier' => '', //TODO: Use trait to generate valid identifier + 'type' => $validated['type'], + 'name' => $validated['name'], + 'description' => $validated['description'], + 'category' => $validated['category'], + 'ISBN' => $validated['ISBN'], + 'rating' => $validated['rating'], + 'borrowing_time' => $validated['borrowing_time'], + 'modified_kind' => 'I', + 'modified_user' => auth()->user()->id, + ]); } /** @@ -36,23 +53,34 @@ public function store(Request $request) */ public function show(Item $item) { - // - } - - /** - * Show the form for editing the specified resource. - */ - public function edit(Item $item) - { - // + // render page } /** * Update the specified resource in storage. */ - public function update(Request $request, Item $item) + public function update(ItemRequest $request, Item $item) { - // + $validated = $request->validated(); + + // update the items with the new data + $item->update([ + 'type' => $validated['type'], + 'name' => $validated['name'], + 'description' => $validated['description'], + 'category' => $validated['category'], + 'ISBN' => $validated['ISBN'], + 'rating' => $validated['rating'], + 'borrowing_time' => $validated['borrowing_time'], + 'modified_kind' => ModifiedEnum::modified, + 'modified_user' => auth()->id(), + ]); + + return $this->CommonResponse( + ResponseStatus::success, + 'Item updated', + $item + ); } /** @@ -60,6 +88,11 @@ public function update(Request $request, Item $item) */ public function destroy(Item $item) { - // + $item->delete(); + return $this->CommonResponse( + ResponseStatus::success, + 'Item deleted', + null, + ); } } diff --git a/src/app/Http/Controllers/LibraryPassController.php b/src/app/Http/Controllers/LibraryPassController.php index 8c0c576..4e34d25 100644 --- a/src/app/Http/Controllers/LibraryPassController.php +++ b/src/app/Http/Controllers/LibraryPassController.php @@ -2,17 +2,22 @@ namespace App\Http\Controllers; +use App\Enums\ModifiedEnum; +use App\Enums\ResponseStatus; +use App\Http\Requests\LibraryPassRequest; use App\Models\LibraryPass; use Illuminate\Http\Request; +use App\Traits\CommonTrait; class LibraryPassController extends Controller { + use CommonTrait; /** * Display a listing of the resource. */ public function index() { - // + $passes = LibraryPass::all(); } /** @@ -26,9 +31,22 @@ public function create() /** * Store a newly created resource in storage. */ - public function store(Request $request) + public function store(LibraryPassRequest $request) { - // + $validated = $request->validated(); + + $libraryPass = LibraryPass::create([ + 'user_id' => $validated['user_id'], + 'barcode' => $validated['barcode'], + 'is_active' => $validated['is_active'], + 'modified_kind' => ModifiedEnum::inserted, + 'modified_user' => auth()->id(), + ]); + + return $this->CommonResponse(ResponseStatus::created, + 'Library pass created successfully', + $libraryPass, + ); } /** @@ -50,9 +68,22 @@ public function edit(LibraryPass $libraryPass) /** * Update the specified resource in storage. */ - public function update(Request $request, LibraryPass $libraryPass) + public function update(LibraryPassRequest $request, LibraryPass $libraryPass) { - // + $validated = $request->validated(); + + $libraryPass->update([ + 'user_id' => $validated['user_id'], + 'barcode' => $validated['barcode'], + 'is_active' => $validated['is_active'], + 'modified_kind' => ModifiedEnum::modified, + 'modified_user' => auth()->user()->id, + ]); + + return $this->CommonResponse(ResponseStatus::success, + 'Library pass updated successfully', + null, + ); } /** @@ -60,6 +91,10 @@ public function update(Request $request, LibraryPass $libraryPass) */ public function destroy(LibraryPass $libraryPass) { - // + $libraryPass->delete(); + return $this->CommonResponse(ResponseStatus::success, + 'Library pass deleted successfully', + null, + ); } } diff --git a/src/app/Http/Controllers/PaymentTransactionController.php b/src/app/Http/Controllers/PaymentTransactionController.php index 30c7862..6c90dc1 100644 --- a/src/app/Http/Controllers/PaymentTransactionController.php +++ b/src/app/Http/Controllers/PaymentTransactionController.php @@ -2,11 +2,16 @@ namespace App\Http\Controllers; +use App\Enums\ModifiedEnum; +use App\Enums\ResponseStatus; +use App\Http\Requests\PaymentTransactionRequest; use App\Models\PaymentTransaction; use Illuminate\Http\Request; +use App\Traits\CommonTrait; class PaymentTransactionController extends Controller { + use CommonTrait; /** * Display a listing of the resource. */ @@ -26,9 +31,21 @@ public function create() /** * Store a newly created resource in storage. */ - public function store(Request $request) + public function store(PaymentTransactionRequest $request) { - // + $validated = $request->validated(); + $paymentTransaction = PaymentTransaction::create([ + 'fine_id' => $validated['fine_id'], + 'amount' => $validated['amount'], + 'modified_kind' => ModifiedEnum::inserted, + 'modified_user' => Auth()->id(), + ]); + + return $this->CommonResponse( + ResponseStatus::created, + 'Payment transaction created successfully', + ['paymentTransaction' => $paymentTransaction], + ); } /** @@ -36,7 +53,7 @@ public function store(Request $request) */ public function show(PaymentTransaction $paymentTransaction) { - // + } /** @@ -50,9 +67,20 @@ public function edit(PaymentTransaction $paymentTransaction) /** * Update the specified resource in storage. */ - public function update(Request $request, PaymentTransaction $paymentTransaction) + public function update(PaymentTransactionRequest $request, PaymentTransaction $paymentTransaction) { - // + $validated = $request->validated(); + $paymentTransaction->update([ + 'amount' => $validated['amount'], + 'modified_kind' => ModifiedEnum::modified, + 'modified_user' => Auth()->id(), + ]); + + return $this->CommonResponse( + ResponseStatus::success, + 'Payment transaction updated successfully', + ['paymentTransaction' => $paymentTransaction], + ); } /** @@ -60,6 +88,11 @@ public function update(Request $request, PaymentTransaction $paymentTransaction) */ public function destroy(PaymentTransaction $paymentTransaction) { - // + $paymentTransaction->delete(); + return $this->CommonResponse( + ResponseStatus::success, + 'Payment transaction deleted successfully', + null, + ); } } diff --git a/src/app/Http/Controllers/ReservationController.php b/src/app/Http/Controllers/ReservationController.php index 9c19d90..ea464ff 100644 --- a/src/app/Http/Controllers/ReservationController.php +++ b/src/app/Http/Controllers/ReservationController.php @@ -2,11 +2,16 @@ namespace App\Http\Controllers; +use App\Enums\ModifiedEnum; +use App\Enums\ResponseStatus; +use App\Http\Requests\ReserveRequest; use App\Models\Reservation; +use App\Traits\CommonTrait; use Illuminate\Http\Request; class ReservationController extends Controller { + use CommonTrait; /** * Display a listing of the resource. */ @@ -26,9 +31,22 @@ public function create() /** * Store a newly created resource in storage. */ - public function store(Request $request) + public function store(ReserveRequest $request) { - // + $validated = $request->validated(); + + $reservation = Reservation::create([ + 'user_id' => $validated['user_id'], + 'item_id' => $validated['item_id'], + 'status' => $validated['status'], + 'modified_kind' => ModifiedEnum::inserted, + 'modified_user' => auth()->id(), + ]); + + return $this->CommonResponse(ResponseStatus::created, + 'Reservation created successfully', + $reservation, + ); } /** @@ -50,9 +68,22 @@ public function edit(Reservation $reservation) /** * Update the specified resource in storage. */ - public function update(Request $request, Reservation $reservation) + public function update(ReserveRequest $request, Reservation $reservation) { - // + $validated = $request->validated(); + + $reservation->update([ + 'user_id' => $validated['user_id'], + 'item_id' => $validated['item_id'], + 'status' => $validated['status'], + 'modified_kind' => ModifiedEnum::modified, + 'modified_user' => auth()->id(), + ]); + + return $this->CommonResponse(ResponseStatus::success, + 'Reservation updated successfully', + ["reservation" => $reservation], + ); } /** @@ -60,6 +91,11 @@ public function update(Request $request, Reservation $reservation) */ public function destroy(Reservation $reservation) { - // + $reservation->delete(); + + return $this->CommonResponse(ResponseStatus::success, + 'Reservation deleted successfully', + null, + ); } } diff --git a/src/app/Http/Controllers/SessionController.php b/src/app/Http/Controllers/SessionController.php index 440b3ed..ab9532d 100644 --- a/src/app/Http/Controllers/SessionController.php +++ b/src/app/Http/Controllers/SessionController.php @@ -3,7 +3,7 @@ namespace App\Http\Controllers; use App\Enums\ResponseStatus; -use App\Http\Requests\LibraryPassReadRequest; +use App\Http\Requests\LibraryPassRequest; use App\Models\LibraryPass; use App\Models\User; use App\Traits\CommonTrait; @@ -11,7 +11,7 @@ class SessionController extends Controller { use CommonTrait; - public function auth(LibraryPassReadRequest $request) + public function auth(LibraryPassRequest $request) { $code = $request->validated()['code']; @@ -20,7 +20,6 @@ public function auth(LibraryPassReadRequest $request) ResponseStatus::validationError, 'Code is required', null, - ResponseStatus::validationError->getStatusCode() ); } @@ -31,7 +30,6 @@ public function auth(LibraryPassReadRequest $request) ResponseStatus::unauthorized, 'Incorrect code', null, - ResponseStatus::unauthorized->getStatusCode() ); } @@ -42,7 +40,6 @@ public function auth(LibraryPassReadRequest $request) ResponseStatus::unauthorized, 'Incorrect code', null, - ResponseStatus::unauthorized->getStatusCode() ); } @@ -52,7 +49,6 @@ public function auth(LibraryPassReadRequest $request) ResponseStatus::success, 'User logged in current session', ['user' => $user], - ResponseStatus::success->getStatusCode() ); } } diff --git a/src/app/Http/Requests/LibraryPassReadRequest.php b/src/app/Http/Requests/AuthorizePassRequest.php similarity index 92% rename from src/app/Http/Requests/LibraryPassReadRequest.php rename to src/app/Http/Requests/AuthorizePassRequest.php index 8551b41..521bb70 100644 --- a/src/app/Http/Requests/LibraryPassReadRequest.php +++ b/src/app/Http/Requests/AuthorizePassRequest.php @@ -5,7 +5,7 @@ use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Foundation\Http\FormRequest; -class LibraryPassReadRequest extends FormRequest +class AuthorizePassRequest extends FormRequest { /** * Determine if the user is authorized to make this request. diff --git a/src/app/Http/Requests/FineRequest.php b/src/app/Http/Requests/FineRequest.php new file mode 100644 index 0000000..6875b34 --- /dev/null +++ b/src/app/Http/Requests/FineRequest.php @@ -0,0 +1,29 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array + { + return [ + 'grant_id' => 'required|integer|exists:grants,id', + 'amount' => 'required|decimal:1,2', + ]; + } +} diff --git a/src/app/Http/Requests/GrantRequest.php b/src/app/Http/Requests/GrantRequest.php new file mode 100644 index 0000000..28115d3 --- /dev/null +++ b/src/app/Http/Requests/GrantRequest.php @@ -0,0 +1,31 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array + { + return [ + 'user_id' => 'required|integer|exists:users,id', + 'item_id' => 'required|integer|exists:items,id', + 'borrowed_date' => 'required|date', + 'return_date' => 'nullable|date', + ]; + } +} diff --git a/src/app/Http/Requests/ItemRequest.php b/src/app/Http/Requests/ItemRequest.php new file mode 100644 index 0000000..79ee120 --- /dev/null +++ b/src/app/Http/Requests/ItemRequest.php @@ -0,0 +1,35 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array + { + return [ + 'type' => 'required|string', + 'name' => 'required|string', + 'description' => 'required|string', + 'category' => 'required|integer|exists:item_categories,id', + 'rating' => 'required|integer|exists:item_age_ratings,id', + 'borrowing_time' => 'nullable|integer', + 'ISBN' => 'nullable|string|unique:items,ISBN', + ]; + } +} diff --git a/src/app/Http/Requests/LibraryPassRequest.php b/src/app/Http/Requests/LibraryPassRequest.php new file mode 100644 index 0000000..4b816da --- /dev/null +++ b/src/app/Http/Requests/LibraryPassRequest.php @@ -0,0 +1,31 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules(): array + { + return [ + 'user_id' => 'integer|exists:users,id', + 'barcode' => 'required|string|max:255', + 'is_active' => 'required|boolean', + ]; + } +} diff --git a/src/app/Http/Requests/PaymentTransactionRequest.php b/src/app/Http/Requests/PaymentTransactionRequest.php new file mode 100644 index 0000000..ab85bc4 --- /dev/null +++ b/src/app/Http/Requests/PaymentTransactionRequest.php @@ -0,0 +1,28 @@ +|string> + */ + public function rules(): array + { + return [ + // + ]; + } +} diff --git a/src/app/Http/Requests/ReserveRequest.php b/src/app/Http/Requests/ReserveRequest.php new file mode 100644 index 0000000..d8289cd --- /dev/null +++ b/src/app/Http/Requests/ReserveRequest.php @@ -0,0 +1,31 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array + { + return [ + 'user_id' => 'required|integer|exists:users,id', + 'item_id' => 'required|integer|exists:items,id', + 'expired_at' => 'required|date', + 'status' => 'required|integer|exists:reservation_statuses,id', + ]; + } +} diff --git a/src/app/Traits/CommonTrait.php b/src/app/Traits/CommonTrait.php index b788571..35f198d 100644 --- a/src/app/Traits/CommonTrait.php +++ b/src/app/Traits/CommonTrait.php @@ -17,12 +17,12 @@ trait CommonTrait { * @param int $code http status code * @return JsonResponse response formatted in json */ - public function CommonResponse(ResponseStatus $status, string $message, array|null $data, int $code): JsonResponse + public function CommonResponse(ResponseStatus $status, string $message, array|null $data): JsonResponse { return response()->json([ - 'status' => $status, + 'status' => $status->label(), 'message' => $message, 'data' => $data - ], $code); + ], $status->getStatusCode()); } } diff --git a/src/database/migrations/2023_12_04_125056_create_fines_table.php b/src/database/migrations/2023_12_04_125056_create_fines_table.php index 0527ad9..fd73bb4 100644 --- a/src/database/migrations/2023_12_04_125056_create_fines_table.php +++ b/src/database/migrations/2023_12_04_125056_create_fines_table.php @@ -13,10 +13,10 @@ public function up(): void { Schema::create('fines', function (Blueprint $table) { $table->id(); - $table->char('modified_kind' ,1)->default('I'); $table->unsignedBigInteger('grant_id'); $table->foreign('grant_id')->references('id')->on('grants')->onDelete("cascade"); $table->double('amount'); + $table->char('modified_kind' ,1)->default('I'); $table->unsignedBigInteger('modified_user'); $table->foreign('modified_user')->references('id')->on('users')->onDelete("cascade"); $table->timestamps();