Skip to content

Commit

Permalink
Controllers (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanericNetwork authored Dec 20, 2023
2 parents bac0930 + ad39880 commit ce746f1
Show file tree
Hide file tree
Showing 18 changed files with 456 additions and 54 deletions.
10 changes: 10 additions & 0 deletions src/app/Enums/ModifiedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Enums;

enum ModifiedEnum: string
{
case inserted = 'I';
case modified = 'M';
case deleted = 'D';
}
3 changes: 3 additions & 0 deletions src/app/Enums/ResponseStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
enum ResponseStatus: string
{
case success = 'success';
case created = 'created';
case error = 'error';
case unauthorized = 'unauthorized';
case forbidden = 'forbidden';
Expand All @@ -16,6 +17,7 @@ public function getStatusCode(): int
{
return match ($this) {
static::success => 200,
static::created => 201,
static::error => 400,
static::unauthorized => 401,
static::forbidden => 403,
Expand All @@ -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',
Expand Down
44 changes: 40 additions & 4 deletions src/app/Http/Controllers/FineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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],
);
}

/**
Expand All @@ -52,14 +69,33 @@ 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],
);
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Fine $fine)
{
//
$fine->delete();

return $this->CommonResponse(
ResponseStatus::success,
'Fine deleted successfully',
null,
);
}
}
45 changes: 40 additions & 5 deletions src/app/Http/Controllers/GrantController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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);
}

/**
Expand All @@ -50,16 +67,34 @@ 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]);
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Grant $grant)
{
//
$grant->delete();
return $this->CommonResponse(
ResponseStatus::success,
'Grant deleted successfully',
null);
}
}
67 changes: 50 additions & 17 deletions src/app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,97 @@

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
}

/**
* Show the form for creating a new resource.
*/
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,
]);
}

/**
* Display the specified resource.
*/
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
);
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Item $item)
{
//
$item->delete();
return $this->CommonResponse(
ResponseStatus::success,
'Item deleted',
null,
);
}
}
47 changes: 41 additions & 6 deletions src/app/Http/Controllers/LibraryPassController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand 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,
);
}

/**
Expand All @@ -50,16 +68,33 @@ 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,
);
}

/**
* Remove the specified resource from storage.
*/
public function destroy(LibraryPass $libraryPass)
{
//
$libraryPass->delete();
return $this->CommonResponse(ResponseStatus::success,
'Library pass deleted successfully',
null,
);
}
}
Loading

0 comments on commit ce746f1

Please sign in to comment.