Skip to content

Commit

Permalink
Merge branch 'main' of github.com:chaostreff-flensburg/simple-borrow
Browse files Browse the repository at this point in the history
  • Loading branch information
scammo committed Mar 22, 2024
2 parents 4ed3cd4 + f2219eb commit 9c15faa
Show file tree
Hide file tree
Showing 15 changed files with 209 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

AUTO_LOGIN_CREDENTIALS="user%40example.com:password"

APPROVING_USER_EMAIL=
15 changes: 13 additions & 2 deletions app/Filament/Resources/ItemResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Filament\Resources\ItemResource\Pages;
use App\Models\Item;
use App\Models\StorageLocation;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
Expand All @@ -14,7 +15,10 @@
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\Action;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Contracts\Database\Eloquent\Builder;

class ItemResource extends Resource
{
Expand All @@ -38,6 +42,7 @@ public static function form(Form $form): Form
->columns(2)
->gridDirection('row')
->relationship('tags', titleAttribute: 'title'),
Checkbox::make('approved')->name('Approved')->default(true),
Select::make('storage_location_id')
->label('Storage Location')
->options(StorageLocation::all()->pluck('name', 'id'))
Expand All @@ -58,13 +63,19 @@ public static function table(Table $table): Table
->columns([
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('description')->limit(30),
Tables\Columns\TextColumn::make('borrow_state'),
Tables\Columns\TextColumn::make('approved'),
])
->filters([
//
Filter::make('approved')->toggle()
->label('Not approved')
->query(fn (Builder $query): Builder => $query->where('approved', false)),
])
->actions([
Tables\Actions\EditAction::make(),
Action::make('approve')
->label('Approve')
->icon('heroicon-o-check-circle')
->action(fn (Item $item) => $item->update(['approved' => true])),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
Expand Down
35 changes: 35 additions & 0 deletions app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace App\Http\Controllers;

use App\Mail\ItemSuggested;
use App\Models\Item;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\View\View;

class ItemController extends Controller
Expand Down Expand Up @@ -36,4 +40,35 @@ public function extend(Item $item): View
{
return view('templates.item.extend', compact('item'));
}

public function suggest(): View
{
return view('templates.item.suggest');
}

public function storeSuggestion(Request $request): RedirectResponse
{
$request->validate([
'name' => 'required|string',
'description' => 'required|string',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg',
]);

if ($request->image) {
$path = $request->file('image')->store('images', 'public');
}

$item = Item::create([
'name' => request()->input('name'),
'description' => request()->input('description'),
'image' => $path ?? null,
'approved' => false,
]);

if (config('app.approvingUserEmail')) {
Mail::to(config('app.approvingUserEmail'))->queue(new ItemSuggested($item));
}

return redirect()->route('home');
}
}
7 changes: 3 additions & 4 deletions app/Livewire/IndexItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ class IndexItems extends Component

public function mount()
{
$this->items = Item::all();
$this->items = Item::approved()->get();
}

public function render()
{

if ($this->term) {
$this->items = Item::where('name', 'like', '%'.$this->term.'%')
$this->items = Item::approved()
->where('name', 'like', '%'.$this->term.'%')
->with('tags')
->get();
} else {
$this->items = Item::with('tags')->get();
}

return view('livewire.index-items');
Expand Down
10 changes: 6 additions & 4 deletions app/Livewire/ItemsGuest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace App\Livewire;

use Livewire\Component;
use App\Models\Item;
use App\Models\Tag;
use Livewire\Component;

class ItemsGuest extends Component
{
public $items;

public $tags;

public function mount()
{
$this->items = Item::all();
$this->items = Item::approved()->get();
$this->tags = Tag::all();
}

Expand All @@ -25,10 +26,11 @@ public function render()
public function filter($tagId = null)
{
if ($tagId == null) {
$this->items = Item::all();
$this->items = Item::approved()->get();

return;
}

$this->items = Tag::find($tagId)->items;
$this->items = Tag::find($tagId)->items()->approved()->get();
}
}
53 changes: 53 additions & 0 deletions app/Mail/ItemSuggested.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Mail;

use App\Models\Item;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class ItemSuggested extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*/
public function __construct(
public Item $item
) {
}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Item Suggested',
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.item-suggested',
);
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
7 changes: 7 additions & 0 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ class Item extends Model
'manual_link',
'require_training',
'storage_location_id',
'approved',
];

protected $casts = [
'require_training' => 'boolean',
'approved' => 'boolean',
];

public function scopeApproved($query)
{
return $query->where('approved', true);
}

public function transactions(): HasMany
{
return $this->hasMany(Transaction::class);
Expand Down
2 changes: 2 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

return [

'approvingUserEmail' => env('APPROVING_USER_EMAIL', null),

'autoLoginCredentials' => env('AUTO_LOGIN_CREDENTIALS', null),

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('items', function (Blueprint $table) {
$table->boolean('approved')->default(false);
});

DB::table('items')->update(['approved' => true]);
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('items', function (Blueprint $table) {
$table->dropColumn('approved');
});
}
};
Binary file added public/images/1711142650-jpg
Binary file not shown.
6 changes: 6 additions & 0 deletions resources/views/emails/item-suggested.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
New item suggested!

Titel: {{ $item->name }}
Beschreibung: {{ $item->description }}

<a href="{{ config('app.url') . '/admin/items/' . $item->id . '/edit' }}">{{ __('Direct link') }}</a>
2 changes: 1 addition & 1 deletion resources/views/livewire/items-guest.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<button class="badge badge-outline" wire:click="filter({{ $tag->id }})" >{{ $tag->title }}</button>
@endforeach
</section>
<section class="grid md:grid-cols-2 lg:grid-cols-3">
<section class="grid gap-2 md:grid-cols-2 lg:grid-cols-3">
@foreach ($items as $item)
<x-card :name="$item->name" :image="$item->image" :description="$item->description" :tags="$item->tags"/>
@endforeach
Expand Down
1 change: 1 addition & 0 deletions resources/views/partials/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</div>
<div class="flex-none">
<ul class="menu menu-horizontal px-1">
<li><a href="{{ route('item.suggest') }}">{{ __('Suggest item') }}</a></li>
<li><a href="{{ route('item.index') }}">{{ __('Borrow items') }}</a></li>
<li><a href="{{ route('storageLocation.index') }}">{{ __('Storage Locations') }}</a></li>
</ul>
Expand Down
46 changes: 46 additions & 0 deletions resources/views/templates/item/suggest.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<x-layout>
<article class="prose min-w-full">
<h1>{{ __('Suggest a new item') }}</h1>
<form method="post" action="{{ route('item.storeSuggestion') }}" enctype="multipart/form-data">
@csrf
@method('POST')
<div class="form-control mb-8">
<label class="label" for="name">
<span class="label-text">{{ __('Name') }}*</span>
</label>
<input type="text" name="name" id="name" class="input input-bordered" placeholder="Säge" required>
@error('name')
<p class="text-red-500">{{ $message }}</p>
@enderror
</div>
<div class="form-control mb-8">
<label class="label" for="description">
<span class="label-text">{{ __('Description') }}*</span>
</label>
<textarea name="description" id="description" class="textarea textarea-bordered h-24" placeholder="Eine Säge für Holz…" required></textarea>
@error('description')
<p class="text-red-500">{{ $message }}</p>
@enderror
</div>
<div class="form-control mb-8">
<label class="label" for="image">
<span class="label-text">{{ __('Image') }}*</span>
</label>
<input type="file" name="image" id="image" class="file-input file-input-bordered" ept="image/png, image/jpeg" required>
@error('image')
<p class="text-red-500">{{ $message }}</p>
@enderror
</div>
<div class="form-control mb-8">
<label class="label" for="manual">
<span class="label-text">{{ __('Manual Link') }}</span>
</label>
<input type="text" name="manual" id="manual" class="input input-bordered" placeholder="https://example.com/manual.pdf">
@error('manual')
<p class="text-red-500">{{ $message }}</p>
@enderror
</div>
<input class="btn btn-success" type="submit">
</form>
</article>
</x-layout>
4 changes: 3 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use App\Http\Controllers\ItemController;
use App\Http\Controllers\StorageLocationController;
use App\Http\Controllers\TransactionController;
use App\Models\Item;
use Illuminate\Support\Facades\Route;

/*
Expand All @@ -19,6 +18,9 @@

Route::get('/', [ItemController::class, 'guest'])->name('home');

Route::get('/items/suggest', [ItemController::class, 'suggest'])->name('item.suggest');
Route::post('/items/suggest', [ItemController::class, 'storeSuggestion'])->name('item.storeSuggestion');

Route::group(['middleware' => 'auth.basic'], function () {
Route::group(['middleware' => 'auth.basic'], function () {
Route::group(['prefix' => 'items'], function () {
Expand Down

0 comments on commit 9c15faa

Please sign in to comment.