Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added and fixed book apis. #41

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/app/Http/Controllers/GrantController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
namespace App\Http\Controllers;

use App\Enums\ModifiedEnum;
use App\Enums\Permissions;
use App\Enums\ResponseStatus;
use App\Http\Requests\GrantItemRequest;
use App\Http\Requests\GrantRequest;
use App\Models\Grant;
use App\Models\Item;
use App\Models\LibraryPass;
use App\Models\User;
use Illuminate\Http\Request;
use App\Traits\CommonTrait;
use Illuminate\Support\Env;

class GrantController extends Controller
{
Expand Down Expand Up @@ -97,4 +103,60 @@ public function destroy(Grant $grant)
'Grant deleted successfully',
null);
}

public function GrantItem(GrantItemRequest $request) {
$validated = $request->validated();

$item = Item::where('identifier', $validated['identifier'])->first();
$library_pass = LibraryPass::where('barcode', $request->bearerToken())->first();
$user = User::where('id', $library_pass->user_id)->first();

if (!isset($user)) {
return $this->CommonResponse(
ResponseStatus::notFound, 'User not found', null
);
}

if (!$user->can(Permissions::CREATE_GRANT->value) && Env::get('APP_ENV') != 'local') {
return $this->CommonResponse(
ResponseStatus::unauthorized, 'Unauthorized', null
);
}

$grant = Grant::where('user_id', $user->id)
->where('item_id', $item->id)
->where('modified_kind', '!=', ModifiedEnum::deleted)
->first();

if (isset($grant)) {

if ($grant->return_date != null) {
return $this->CommonResponse(
ResponseStatus::forbidden, 'Item already returned', null
);
}

$grant->return_date = now();
$grant->modified_kind = ModifiedEnum::modified;
$grant->modified_user = $user->id;
$grant->save();

$message = 'Item returned successfully';
} else {
$grant = Grant::create([
'user_id' => $user->id,
'item_id' => $item->id,
'borrowed_date' => now(),
'return_date' => null,
'modified_kind' => ModifiedEnum::inserted,
'modified_user' => $user->id,
]);

$message = 'Item granted successfully';
}

return $this->CommonResponse(
ResponseStatus::success, $message, $grant
);
}
}
89 changes: 87 additions & 2 deletions src/app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

namespace App\Http\Controllers;

use App\Enums\ItemTypes;
use App\Enums\ModifiedEnum;
use App\Enums\Permissions;
use App\Enums\ResponseStatus;
use App\Http\Requests\ApiStoreItemRequest;
use App\Http\Requests\ItemRequest;
use App\Http\Requests\SearchItemRequest;
use App\Models\Author;
use App\Models\Item;
use App\Models\ItemImage;
use App\Models\LibraryPass;
use App\Models\User;
use App\Traits\CommonTrait;
use App\Traits\CommonLibraryTrait;
use Illuminate\Support\Env;
use Illuminate\Support\Facades\Storage;

class ItemController extends Controller
{
use CommonTrait;
use CommonTrait, CommonLibraryTrait;
/**
* Display a listing of the resource.
*/
Expand Down Expand Up @@ -101,15 +111,90 @@ public function search(SearchItemRequest $request) {
$validated = $request->validated();
$search = $validated['search'];

$library_pass = LibraryPass::where('barcode', $request->bearerToken())->first();
$user = User::where('id', $library_pass->user_id)->first();

if (!$user->can(Permissions::VIEW_ITEM->value) && Env::get('APP_ENV') != 'local') {
return $this->CommonResponse(
ResponseStatus::unauthorized,
'Permission denied',
null
);
}

$items = Item::where('name', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%")
->orWhere('ISBN', 'like', "%{$search}%")
->get();

$authors = Author::where('name', 'like', "%{$search}%")->get();

if (count($authors) >= 1) {
foreach ($authors as $author) {

$author_items = $author->items;
foreach ($author_items as $author_item) {

$item_exists = false;
foreach ($items as $item) {
if ($item->id == $author_item->id) {
$item_exists = true;
}
}

if (!$item_exists) {
$items[] = $author_item;
}

}
}
}

return $this->CommonResponse(
ResponseStatus::success,
'Search results',
$items
$this->ReturnMultipleFullItems($items)
);
}

public function ApiStore(ApiStoreItemRequest $request) {
$validated = $request->validated();
$library_pass = LibraryPass::where('barcode', $request->bearerToken())->first();
$user = User::where('id', $library_pass->user_id)->first();

$title = $validated['title'];
$description = $validated['description'];
$ISBN = $validated['ISBN'];
$images = $validated['images'];

$item = Item::create([
'name' => $title,
'description' => $description,
'ISBN' => $ISBN,
'author_id' => 1,
'identifier' => $this->generateItemIdentifier(),
'type' => ItemTypes::book,
'modified_user' => $user->id,
'modified_kind' => ModifiedEnum::inserted,
]);

foreach ($images as $image) {
// store image in storage
$what = $image->storeAs('public/images/', $image->getClientOriginalName());

ItemImage::create([
'item_id' => $item->id,
'filename' => $image->getClientOriginalName(),
'path' => $what,
'modified_user' => $user->id,
'modified_kind' => ModifiedEnum::inserted,
]);
}

return $this->CommonResponse(
ResponseStatus::success,
'Item created',
$this->ReturnFullItem($item)
);
}
}
23 changes: 12 additions & 11 deletions src/app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@ class Kernel extends HttpKernel
* @var array<string, class-string|string>
*/
protected $middlewareAliases = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'precognitive' => \Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests::class,
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'precognitive' => \Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests::class,
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'api.auth' => \App\Http\Middleware\ApiAuthorize::class,
];
}
46 changes: 46 additions & 0 deletions src/app/Http/Middleware/ApiAuthorize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Http\Middleware;

use App\Enums\ModifiedEnum;
use App\Enums\ResponseStatus;
use App\Models\LibraryPass;
use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Traits\CommonTrait;

class ApiAuthorize
{
use CommonTrait;
/**
* Handle an incoming request.
*
* @param \Closure(Request): (Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
// get bearer token
$pass = $request->bearerToken();

if (!isset($pass)) {
return $this->CommonResponse(ResponseStatus::unauthorized, 'Unauthorized. Missing library pass.', null);
}

$lib_pass = LibraryPass::where('barcode', $pass)
->where('modified_kind', '!=', ModifiedEnum::deleted)->first();

if (!isset($lib_pass)) {
return $this->CommonResponse(ResponseStatus::unauthorized, 'Unauthorized. Invalid library pass.', null);
}

$user = $lib_pass->user;

if (!isset($user)) {
return $this->CommonResponse(ResponseStatus::unauthorized, 'Unauthorized. Invalid library pass.', null);
}

return $next($request);
}
}
32 changes: 32 additions & 0 deletions src/app/Http/Requests/ApiStoreItemRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Requests;

use App\Enums\Permissions;
use Illuminate\Foundation\Http\FormRequest;

class ApiStoreItemRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'title' => 'required|string',
'description' => 'required|string',
'ISBN' => 'required|string',
'images' => 'required|array',
];
}
}
28 changes: 28 additions & 0 deletions src/app/Http/Requests/GrantItemRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class GrantItemRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'identifier' => 'required|string|max:255',
];
}
}
4 changes: 2 additions & 2 deletions src/app/Http/Requests/SearchItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests;

use App\Enums\Permissions;
use App\Models\Item;
use Illuminate\Foundation\Http\FormRequest;

Expand All @@ -12,8 +13,7 @@ class SearchItemRequest extends FormRequest
*/
public function authorize(): bool
{
$user = auth()->user();
return $user->can('search', Item::class);
return true;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/app/Models/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

class Author extends Model
{
use HasFactory;
protected $guarded = [];

public function items()
{
return $this->hasMany(Item::class);
}
}
7 changes: 6 additions & 1 deletion src/app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Item extends Model
Expand All @@ -16,10 +17,14 @@ public function author(): BelongsTo
}

public function category(): BelongsTo {
return $this->belongsTo(ItemCategory::class);
return $this->BelongsTo(ItemCategory::class, 'category_id');
}

public function ageRating(): BelongsTo {
return $this->belongsTo(ItemAgeRating::class, 'rating_id');
}

public function images(): HasMany {
return $this->hasMany(ItemImage::class);
}
}
Loading
Loading