This commit is contained in:
Marley Rae 2022-04-28 12:01:17 -07:00
parent 55215be5d9
commit 7b77a131ac
9 changed files with 99 additions and 75 deletions

View file

@ -45,6 +45,7 @@ public function update(UpdateJoinedRequest $request, Joined $joined)
public function destroy(Joined $joined)
{
$joined->remove();
return redirect()->route('admin.joined.index')->with('success', 'Fanlisting removed.');
}
}

View file

@ -35,6 +35,9 @@ public function create()
*/
public function store(StoreOwnedRequest $request)
{
Owned::store($request->validated());
return redirect()->route('admin.owned.index')->with('success', 'Fanlisting added!');
}
/**

View file

@ -32,7 +32,7 @@ public function rules()
'slug' => ['required', 'alpha_dash'],
'title' => ['nullable', 'string'],
'image' => ['nullable', 'image'],
'date_opened' => ['nullable', 'date'],
'opened' => ['nullable', 'date'],
'hold_member_updates' => ['nullable', 'boolean'],
];
}

View file

@ -5,7 +5,6 @@
use App\Traits\Categorizable;
use App\Traits\Imageable;
use App\Traits\Ownable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@ -35,15 +34,6 @@ class Joined extends Model
// injected by trait: categories (morph many-to-many)
/* -------------------------------------------------------------------------- attributes ---- */
protected function approved() : Attribute
{
return Attribute::make(
set: fn ($value) => isset($value) ? $value = $value : $value = false,
);
}
/* ------------------------------------------------------------------------------ joined ---- */
public static function store(array $validated) : Joined

View file

@ -17,5 +17,35 @@ class Owned extends Model
protected $table = 'owned';
protected $casts = [
'opened' => 'datetime',
'hold_member_updates' => 'boolean',
];
/* ----------------------------------------------------------------------- relationships ---- */
// injected by trait: collective (belongsTo)
// injected by trait: categories (many-to-many polymorphic)
/* ------------------------------------------------------------------------------- store ---- */
public static function store(array $validated) : static
{
$validated['image'] = $validated['image'] ?? null;
$owned = new static();
$owned->subject = $validated['subject'];
$owned->status = $validated['status'];
$owned->slug = $validated['slug'];
$owned->title = $validated['title'] ?? null;
$owned->image = self::imagePath($validated['image']);
$owned->opened = $validated['opened'] ?? null;
$owned->hold_member_updates = $validated['hold_member_updates'] ?? false;
auth_collective()->owned()->save($owned);
$owned->categories()->sync($validated['categories']);
return $owned;
}
}

View file

@ -8,10 +8,10 @@ trait Imageable
{
public static function imagePath($image) : ?string
{
$path = strtolower(static::class);
$path = strtolower(substr(strrchr(__CLASS__, '\\'), 1));
if (isset($image)) {
return Storage::putFile($path, $image);
return $image->storePublicly($path, 'public');
} else {
return null;
}

View file

@ -8,7 +8,8 @@
@section('content')
<form action="{{ route('admin.owned.store') }}" method="POST" autocomplete="off">
<form action="{{ route('admin.owned.store') }}" method="POST" autocomplete="off"
enctype="multipart/form-data">
@csrf
<fieldset class="form__fieldset">
@ -18,7 +19,7 @@
<x-form.text name="slug" :label="'Slug:'" required />
<x-form.text name="title" :label="'Title:'" />
<x-form.file name="image" />
<x-form.date name="date_opened" :label="'Date opened:'" />
<x-form.date name="opened" :label="'Date opened:'" />
<x-form.checkbox name="hold_member_updates" :label="'Hold member updates?'" />
<x-form.buttons />

View file

@ -1,51 +1,50 @@
<div>
<input type="text" name="search" placeholder="Search fanlistings..." class="form__input--search"
wire:model="searchTerm" />
wire:model="searchTerm" />
<table class="table">
<thead class="table__thead">
<tr>
<th>{{-- approved --}}</th>
<th>Subject</th> {{-- link --}}
<th>Categories</th>
<th>Image</th>
<th colspan="3">Actions</th>
</tr>
</thead>
<thead class="table__thead">
<tr>
<th>{{-- approved --}}</th>
<th>Subject</th> {{-- link --}}
<th>Categories</th>
<th>Image</th>
<th colspan="3">Actions</th>
</tr>
</thead>
<tbody class="table__tbody">
@foreach ($fanlistings as $fl)
<tr>
<td>
@if (!$fl->approved)
<span class="text--error">&times;</span>
@endif
</td>
<td><a href="{{ $fl->url }}" target="_blank">{{ $fl->subject }}</a></td>
<td>{{ $fl->listCategories() }}</td>
<td><img src="{{ $fl->image }}"></td>
<tbody class="table__tbody">
@foreach ($fanlistings as $fl)
<tr>
<td>
@if (!$fl->approved)
<span class="text--error">&times;</span>
@endif
</td>
<td><a href="{{ $fl->url }}" target="_blank">{{ $fl->subject }}</a></td>
<td>{{ $fl->listCategories() }}</td>
<td><img src="/storage/{{ $fl->image }}"></td>
<td>
@if ($class == 'joined' && $fl->approved == false)
<button wire:click="approve({{ $fl }})" class="btn--table">
Approve
</button>
@elseif ($class == 'owned')
<a href="{{ route('admin.owned.show', $fl) }}" class="btn--table">View</a>
@endif
</td>
<td>
@if ($class == 'joined' && $fl->approved == false)
<button wire:click="approve({{ $fl }})" class="btn--table">
Approve
</button>
@elseif ($class == 'owned')
<a href="{{ route('admin.owned.show', $fl) }}" class="btn--table">View</a>
@endif
</td>
<td>
<a href="{{ route("admin.$class.edit", $fl) }}" class="btn--table">Edit</a>
</td>
<td>
<a href="{{ route("admin.$class.edit", $fl) }}" class="btn--table">Edit</a>
</td>
<td>
<x-admin.form.destroy :object="$fl" :route="'admin.'.$class.'.destroy'"
:btnClass="'btn--table'"/>
</td>
</tr>
@endforeach
</tbody>
</table>
<td>
<x-admin.form.destroy :object="$fl" :route="'admin.'.$class.'.destroy'" :btnClass="'btn--table'" />
</td>
</tr>
@endforeach
</tbody>
</table>
{{ $fanlistings->links() }}
</div>
{{ $fanlistings->links() }}
</div>

View file

@ -8,14 +8,14 @@
uses()->group('owned', 'admin');
beforeEach(function () {
$this->user = Collective::first();
$this->user = Collective::first();
$this->request = [
'categories' => [rand(1, 57), rand(1, 57), rand(1, 57)],
'subject' => faker()->word,
'status' => 'current',
'slug' => faker()->slug(2),
'title' => faker()->sentence(),
'date_opened' => faker()->dateTimeThisMonth(),
'opened' => faker()->dateTimeThisMonth(),
'hold_member_updates' => faker()->boolean(),
'notify_pending' => faker()->boolean(),
];
@ -48,29 +48,29 @@
it('fails non-array categories', function () {
$this->request['categories'] = 'This is not an array.';
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response->assertInvalid();
});
it('fails empty categories array', function () {
$this->request['categories'] = [];
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response->assertInvalid();
});
it('fails non-numeric category item', function () {
$this->request['categories'][] = 'a';
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response->assertInvalid();
});
it('fails non-existant category', function () {
$invalidCat = (Category::all()->count()) + 10;
$invalidCat = (Category::all()->count()) + 10;
$this->request['categories'][] = $invalidCat;
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response->assertInvalid();
});
@ -84,7 +84,7 @@
it('fails non-string subject', function () {
$this->request['subject'] = 39502;
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response->assertInvalid();
});
@ -98,7 +98,7 @@
it('fails non-valid status', function () {
$this->request['status'] = 'This is not a correct status.';
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response->assertInvalid();
});
@ -112,7 +112,7 @@
it('fails non-valid slug format', function () {
$this->request['slug'] = 'This is not a valid slug.';
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response->assertInvalid();
});
@ -126,21 +126,21 @@
it('fails non-string title', function () {
$this->request['title'] = 494920;
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response->assertInvalid();
});
it('allows null date_opened', function () {
unset($this->request['date_opened']);
it('allows null opened', function () {
unset($this->request['opened']);
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response->assertValid();
});
it('fails non-date date_opened', function () {
$this->request['date_opened'] = 'This is not a date.';
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
it('fails non-date opened', function () {
$this->request['opened'] = 'This is not a date.';
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response->assertInvalid();
});
@ -154,7 +154,7 @@
it('fails non-bool hold_member_updates', function () {
$this->request['holds_member_updates'] = 'This is not a boolean.';
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response = $this->actingAs($this->user)->post('/fanatic/owned', $this->request);
$response->assertInvalid();
});