Skip to content

Commit

Permalink
fix: if clauses wich are dependant on transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
flemming-pr committed Aug 9, 2023
1 parent 455b876 commit daaca9c
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 21 deletions.
10 changes: 3 additions & 7 deletions app/Http/Controllers/TransactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ public function create(Request $request): RedirectResponse
]
);

$item = Item::find($request->item_id);
$item->borrow_state = $request->transaction_type;
$item->save();

if ((int) $request->transaction_type === Transaction::BORROWED) {
Mail::to($request->email)->queue(new ItemBorrowed($item));
if ((int) $request->transaction_type === Transaction::BORROW) {
Mail::to($request->email)->queue(new ItemBorrowed(Item::find($request->item_id)));
}

return redirect()->route('item.show', $request->item_id);
Expand All @@ -52,7 +48,7 @@ public function extend(Request $request): RedirectResponse
]);

$transaction = Transaction::where('item_id', $request->item_id)
->where('transaction_type', Transaction::BORROWED)
->where('transaction_type', Transaction::BORROW)
->orderBy('created_at', 'desc')
->first();

Expand Down
2 changes: 1 addition & 1 deletion app/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Transaction extends Model

public const RETURN = 0;

public const BORROWED = 1;
public const BORROW = 1;

protected $fillable = [
'return_date',
Expand Down
2 changes: 1 addition & 1 deletion database/factories/TransactionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function definition(): array
{
return [
'return_date' => $this->faker->dateTimeBetween('now', '+1 year'),
'transaction_type' => $this->faker->randomElement([Transaction::RETURN, Transaction::BORROWED]),
'transaction_type' => $this->faker->randomElement([Transaction::RETURN, Transaction::BORROW]),
'item_id' => Item::factory(),
'note' => $this->faker->text,
'email' => $this->faker->email,
Expand Down
2 changes: 1 addition & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function run(): void
{
$this->call([
ItemSeeder::class,
TransactionSeeder::class,
//TransactionSeeder::class,
UserSeeder::class,
]);
}
Expand Down
10 changes: 5 additions & 5 deletions resources/views/partials/item/availability-badge.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
@if ($item->transactions->last()->transaction_type === App\Models\Transaction::RETURN)
<small class="badge badge-success">
Status: verfügbar
</small>
@else
@if ($item->transactions->last()?->transaction_type === App\Models\Transaction::BORROW)
<small class="badge badge-error">
Status: ausgeliehen bis {{ $item->transactions->last()->return_date->format('d.m.Y') }}
</small>
@else
<small class="badge badge-success">
Status: verfügbar
</small>
@endif
4 changes: 2 additions & 2 deletions resources/views/templates/item/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<a class="btn btn-primary" href="{{ $item->manual_link }}" target="_blank" role="button">Anleitung / Wiki-Eintrag</a>
@endif
<hr>
<a class="btn btn-success" href="{{ route('item.transaction', $item->id) }}" role="button">{{ $item->transactions->last()->transaction_type === App\Models\Transaction::RETURN ? 'Ausleihen' : 'Zurückgeben' }}</a>
@if ($item->transactions->last()->transaction_type === App\Models\Transaction::BORROWED)
<a class="btn btn-success" href="{{ route('item.transaction', $item->id) }}" role="button">{{ $item->transactions->last()?->transaction_type === App\Models\Transaction::BORROW ? 'Zurückgeben' : 'Ausleihen' }}</a>
@if ($item->transactions->last()?->transaction_type === App\Models\Transaction::BORROW)
<a class="btn btn-error" href="{{ route('item.extend', $item->id) }}" role="button">Verlängern</a>
@endif
<div class="collapse collapse-arrow bg-base-200 mt-8">
Expand Down
8 changes: 4 additions & 4 deletions resources/views/templates/item/transaction.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<x-layout>
<article class="prose min-w-full">
<h1>{{ $item->transactions->last()->transaction_type === App\Models\Transaction::RETURN ? 'Ausleihen' : 'Zurückgeben' }}: {{ $item->name }}</h1>
<h1>{{ $item->transactions->last()?->transaction_type === App\Models\Transaction::BORROW ? 'Zurückgeben' : 'Ausleihen' }}: {{ $item->name }}</h1>
<form action="{{ route('transaction.create') }}" method="post">
@csrf
@method('POST')
<input type="hidden" name="item_id" value="{{ $item->id }}">
<input type="hidden" name="transaction_type" value="{{ $item->borrow_state === 0 ? 1 : 0 }}">
@if ($item->borrow_state === 0)
<input type="hidden" name="transaction_type" value="{{ $item->transactions->last()?->transaction_type === App\Models\Transaction::BORROW ? App\Models\Transaction::RETURN : App\Models\Transaction::BORROW }}">
@if ($item->transactions->last()?->transaction_type === App\Models\Transaction::RETURN || $item->transactions->last()?->transaction_type === null)
<section class="mb-4">
<label class="block" for="return_date">Rückgabedatum</label>
<input class="input input-bordered w-full max-w-xs" type="date" name="return_date" id="return_date" required>
Expand All @@ -27,7 +27,7 @@
</label>
<textarea name="note" id="note" class="textarea textarea-bordered h-24" placeholder="Wir brauchen neue Sägeblätter…"></textarea>
</div>
<button class="btn btn-success" type="submit">{{ $item->borrow_state === 0 ? 'Ausleihen' : 'Zurückgeben' }}</button>
<button class="btn btn-success" type="submit">{{ $item->transactions->last()?->transaction_type === App\Models\Transaction::BORROW ? 'Zurückgeben' : 'Ausleihen' }}</button>
</form>
</article>
</x-layout>

0 comments on commit daaca9c

Please sign in to comment.