Skip to content

Commit

Permalink
Add the Items
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbrandB committed May 27, 2024
1 parent 2e39ab7 commit 67c27fa
Show file tree
Hide file tree
Showing 35 changed files with 1,018 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ yarn-error.log
/.fleet
/.idea
/.vscode
deploy.sh
89 changes: 89 additions & 0 deletions app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreItemRequest;
use App\Http\Requests\UpdateItemRequest;
use App\Models\Item;
use Inertia\Inertia;
use Vinkla\Hashids\Facades\Hashids;

class ItemController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return Inertia::render('Items/Index', [
'items' => Item::all()->each(function ($item) {
$item->hashid = $item->public_id;
}),
]);
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
return Inertia::render('Items/Edit', []);
}

/**
* Store a newly created resource in storage.
*/
public function store(StoreItemRequest $request)
{
Item::create([
'name' => $request->name,
'description' => $request->description,
]);

return redirect(route('items.index'));
}

/**
* Display the specified resource.
*/
public function show(String $hashid)
{
$id = Hashids::decode($hashid);
$item = Item::query()->where('id', $id)->firstOrFail();
$item->hashid = $item->public_id;
return Inertia::render('Items/Show', [
'item' => $item,
]);
}

/**
* Show the form for editing the specified resource.
*/
public function edit(Int $id)
{
return Inertia::render('Items/Edit', [
'item' => Item::findorFail($id),
]);
}

/**
* Update the specified resource in storage.
*/
public function update(StoreItemRequest $request, int $id)
{
$item = Item::findOrFail($id);
$item->update([
'name' => $request->name,
'description' => $request->description,
]);
return redirect(route('items.index'));
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Item $item)
{
//
}
}
31 changes: 31 additions & 0 deletions app/Http/Requests/StoreItemRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

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

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

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

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

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
//
];
}
}
29 changes: 29 additions & 0 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Models;

use Vinkla\Hashids\Facades\Hashids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
use HasFactory;

protected $fillable = [
'name',
'description',
];

public function resolveRouteBinding($value, $field = null)
{
return $this::findOrFail(Hashids::decode($value));
}

public function getPublicIdAttribute(): string
{
return Hashids::encode($this->id);
}


}
66 changes: 66 additions & 0 deletions app/Policies/ItemPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Policies;

use App\Models\Item;
use App\Models\User;
use Illuminate\Auth\Access\Response;

class ItemPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
//
}

/**
* Determine whether the user can view the model.
*/
public function view(User $user, Item $item): bool
{
//
}

/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
//
}

/**
* Determine whether the user can update the model.
*/
public function update(User $user, Item $item): bool
{
//
}

/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Item $item): bool
{
//
}

/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Item $item): bool
{
//
}

/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Item $item): bool
{
//
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"laravel/framework": "^11.0",
"laravel/sanctum": "^4.0",
"laravel/tinker": "^2.9",
"tightenco/ziggy": "^2.0"
"tightenco/ziggy": "^2.0",
"vinkla/hashids": "^12.0"
},
"require-dev": {
"fakerphp/faker": "^1.23",
Expand Down
Loading

0 comments on commit 67c27fa

Please sign in to comment.