Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ludoguenet committed Mar 8, 2024
1 parent ab7a3f6 commit d28f134
Show file tree
Hide file tree
Showing 23 changed files with 657 additions and 43 deletions.
5 changes: 4 additions & 1 deletion app/Http/Controllers/Admin/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class IndexController extends Controller
{
public function __invoke(Request $request): View
{
$nudges = Nudge::query()->latest()->get();
$nudges = Nudge::query()
->with('user')
->latest()
->get();

return view('admin.index', [
'nudges' => $nudges,
Expand Down
19 changes: 19 additions & 0 deletions app/Http/Controllers/Admin/Nudge/EditController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Admin\Nudge;

use App\Http\Controllers\Controller;
use App\Models\Nudge;
use Illuminate\Contracts\View\View;

class EditController extends Controller
{
public function __invoke(Nudge $nudge): View
{
return view('nudges.edit', [
'nudge' => $nudge,
]);
}
}
29 changes: 29 additions & 0 deletions app/Http/Controllers/Admin/Nudge/UpdateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Admin\Nudge;

use App\Http\Controllers\Controller;
use App\Models\Nudge;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class UpdateController extends Controller
{
/**
* Handle the incoming request.
*/
public function __invoke(Nudge $nudge, Request $request): RedirectResponse
{
$validated = $request->validate([
'title' => ['required', 'unique:nudges,title,'.$nudge->id, 'max:100'],
'content' => 'required|string|max:500',
'code' => 'required|string',
]);

$nudge->update($validated);

return redirect()->route('admin.index');
}
}
25 changes: 25 additions & 0 deletions app/Http/Controllers/Nudge/ShowController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Nudge;

use App\Actions\Nudge\GenerateRandomSynonym;
use App\Http\Controllers\Controller;
use App\Models\Nudge;
use Illuminate\Contracts\View\View;

class ShowController extends Controller
{
public function __invoke(Nudge $nudge): View
{
abort_if(! $nudge->validated, 404);

$randomSynonym = (new GenerateRandomSynonym)->handle();

return view('nudges.show', [
'nudge' => $nudge->loadCount('likes'),
'randomSynonym' => $randomSynonym,
]);
}
}
4 changes: 3 additions & 1 deletion app/Http/Controllers/Nudge/StoreController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ class StoreController extends Controller
public function __invoke(Request $request): RedirectResponse
{
$validated = $request->validate([
'content' => 'required|string',
'title' => 'required|unique:nudges|max:100',
'content' => 'required|string|max:500',
'code' => 'required|string',
]);

/** @var User $user */
$user = auth()->user();

$user->nudges()->create([
'title' => $validated['title'],
'content' => $validated['content'],
'code' => $validated['code'],
]);
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/RSSFeedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class RSSFeedController extends Controller
{
public function __invoke(): Response
{
$nudges = Nudge::latest()->get();
$nudges = Nudge::query()
->validated()
->latest()
->get();

return response()->view('rss', [
'nudges' => $nudges,
Expand Down
20 changes: 20 additions & 0 deletions app/Models/Nudge.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;

class Nudge extends Model implements Likeable
{
use HasFactory;
use HasLikes;

protected $fillable = [
'title',
'slug',
'content',
'code',
'validated',
Expand All @@ -32,6 +35,23 @@ class Nudge extends Model implements Likeable
'validated' => 'boolean',
];

public static function boot(): void
{
parent::boot();

static::creating(function (Nudge $model) {
/** @var string title */
$title = $model->title;
$model->slug = Str::slug($title);
});

static::updating(function (Nudge $model) {
/** @var string title */
$title = $model->title;
$model->slug = Str::slug($title);
});
}

/**
* @return BelongsTo<User, Nudge>
*/
Expand Down
5 changes: 5 additions & 0 deletions database/factories/NudgeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ class NudgeFactory extends Factory
*/
public function definition(): array
{
$title = Str::random();
$slug = Str::slug($title);

return [
'title' => $title,
'slug' => $slug,
'content' => Str::random(),
'code' => Str::random(),
'validated' => false,
Expand Down
38 changes: 38 additions & 0 deletions database/migrations/2024_03_07_210642_add_title_to_nudges.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

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('nudges', function (Blueprint $table) {
$table->string('title', 100)->unique()->nullable();
});

DB::table('nudges')
->update([
'title' => DB::raw("('nudge-' || id)"),
]);
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('nudges', function (Blueprint $table) {
$table->dropUnique(['title']);
$table->dropColumn('title');

});
}
};
37 changes: 37 additions & 0 deletions database/migrations/2024_03_07_210649_add_slug_to_nudges.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

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('nudges', function (Blueprint $table) {
$table->string('slug')->unique()->nullable();
});

DB::table('nudges')
->update([
'slug' => DB::raw("('nudge-' || id)"),
]);
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('nudges', function (Blueprint $table) {
$table->dropUnique(['slug']);
$table->dropColumn('slug');
});
}
};
47 changes: 47 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"devDependencies": {
"@tailwindcss/forms": "^0.5.2",
"@tailwindcss/typography": "^0.5.10",
"alpinejs": "^3.4.2",
"autoprefixer": "^10.4.2",
"axios": "^1.6.4",
Expand Down
13 changes: 11 additions & 2 deletions resources/views/admin/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
<table class="min-w-full divide-y divide-gray-300">
<thead>
<tr>
<th scope="col" class="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-0">Content</th>
<th scope="col" class="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-0">Title</th>
<th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Author</th>
<th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Status</th>
<th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Date</th>
<th scope="col" class="relative py-3.5 pl-3 pr-4 sm:pr-0">
Expand Down Expand Up @@ -52,15 +53,23 @@
</div>
</x-modal>
<div class="ml-4">
<div class="text-gray-500" title="{{ $nudge->content }}">{{ Str::limit($nudge->content, 70) }}</div>
<div class="text-gray-500" title="{{ $nudge->content }}">{{ Str::limit($nudge->title, 70) }}</div>
</div>
</div>
</td>
<td class="whitespace-nowrap px-3 py-5 text-sm text-gray-500">
<span>{{ $nudge->user->name }}</span>
</td>
<td class="whitespace-nowrap px-3 py-5 text-sm text-gray-500">
<span class="inline-flex items-center rounded-md bg-{{ $nudge->validated === true ? 'green' : 'red' }}-50 px-2 py-1 text-xs font-medium text-{{ $nudge->validated === true ? 'green' : 'red' }}-700 ring-1 ring-inset ring-{{ $nudge->validated === true ? 'green' : 'red' }}-600/20">{{ $nudge->validated === true ? 'validated' : 'not validated' }}</span>
</td>
<td class="whitespace-nowrap px-3 py-5 text-sm text-gray-500">{{ $nudge->created_at->format('m/d/Y') }}</td>
<td class="flex items-center space-x-5 relative whitespace-nowrap py-5 pl-3 pr-4 text-left text-sm font-medium sm:pr-0">
<a href="{{ route('admin.nudges.edit', $nudge) }}">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L6.832 19.82a4.5 4.5 0 0 1-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 0 1 1.13-1.897L16.863 4.487Zm0 0L19.5 7.125" />
</svg>
</a>
@include('nudges.partials.delete-nudge-form', ['nudgeId' => $nudge->id])
<!-- Enabled: "bg-green-500", Not Enabled: "bg-gray-200" -->
<button x-data="toggles({{ $nudge->validated }})" @click="toggle({{ $nudge->id }})" type="button" :class="isToggled ? 'bg-green-500' : 'bg-gray-200'" class="relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-green-500 focus:ring-offset-2" role="switch" aria-checked="false">
Expand Down
4 changes: 2 additions & 2 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<table class="min-w-full divide-y divide-gray-300">
<thead>
<tr>
<th scope="col" class="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-0">Content</th>
<th scope="col" class="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-0">Title</th>
<th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Status</th>
<th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Date</th>
<th scope="col" class="relative py-3.5 pl-3 pr-4 sm:pr-0">
Expand Down Expand Up @@ -52,7 +52,7 @@
</div>
</x-modal>
<div class="ml-4">
<div class="text-gray-500" title="{{ $nudge->content }}">{{ Str::limit($nudge->content, 70) }}</div>
<div class="text-gray-500" title="{{ $nudge->content }}">{{ Str::limit($nudge->title, 70) }}</div>
</div>
</div>
</td>
Expand Down
4 changes: 3 additions & 1 deletion resources/views/homepage.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
<div class="mx-auto max-w-4xl">
@if ($nudge)
<div x-data="nudge({{ Js::from(auth()->user()?->isLiked($nudge)) }})">
<h1 class="text-4xl font-bold tracking-tight text-gray-900 mb-5">{{ $nudge->title }}</h1>

<article class="flex items-center justify-between text-md leading-8 text-gray-500 sm:text-lg mb-2.5">
<p>{{ $nudge->content }}</p>
<span class="prose">{!! Str::markdown($nudge->content) !!}</span>
<textarea type="text" x-ref="content" class="hidden">{!! $nudge->code !!}</textarea>
</article>

Expand Down
Loading

0 comments on commit d28f134

Please sign in to comment.