Skip to content

Commit

Permalink
feat: add note to name
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss committed Jan 17, 2024
1 parent 2863a60 commit f0b610f
Show file tree
Hide file tree
Showing 17 changed files with 302 additions and 7 deletions.
4 changes: 4 additions & 0 deletions app/Http/Controllers/NameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ public function show(Request $request): View
if (! auth()->check()) {
$favoritedNamesForLoggedUser = collect();
$lists = [];
$note = '';
} else {
$favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () {
return UserViewModel::favorites();
});

$lists = ListViewModel::lists($requestedName);

$note = $requestedName->getNoteForUser();
}

return view('names.show', [
Expand All @@ -93,6 +96,7 @@ public function show(Request $request): View
'numerology' => $numerology,
'favorites' => $favoritedNamesForLoggedUser,
'lists' => $lists,
'note' => $note,
]);
}

Expand Down
1 change: 0 additions & 1 deletion app/Http/Controllers/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\View\View;

class ShareController extends Controller
{
Expand Down
31 changes: 31 additions & 0 deletions app/Http/Controllers/UserNameController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Controllers;

use App\Helpers\StringHelper;
use App\Http\ViewModels\Names\NameViewModel;
use App\Services\AddNoteToName;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;

class UserNameController extends Controller
{
public function update(Request $request): RedirectResponse
{
$name = $request->attributes->get('name');

(new AddNoteToName(
nameId: $name->id,
userId: auth()->id(),
noteText: $request->input('note'),
))->execute();

return Redirect::route('name.show', [
'id' => $name->id,
'name' => StringHelper::sanitizeNameForURL($name->name),
]);
}
}
4 changes: 4 additions & 0 deletions app/Http/ViewModels/Names/NameViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public static function details(Name $name): array
'favorite' => route('favorite.name.update', [
'id' => $name->id,
]),
'note_edit' => route('user.name.update', [
'id' => $name->id,
'name' => StringHelper::sanitizeNameForURL($name->name),
]),
],
];
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/ViewModels/Search/SearchViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static function names(?string $term = null, int $limit = 20): array
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::formatNameFromDB($name->name),
'gender' => $name->gender == 'male' ? 'masculin' : 'feminin',
'url' => [
'show' => route('name.show', [
'id' => $name->id,
Expand Down
17 changes: 17 additions & 0 deletions app/Models/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,21 @@ public function toSitemapTag(): Url|string|array
->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY)
->setPriority(0.1);
}

public function getNoteForUser(): ?string
{
if (! auth()->check()) {
return null;
}

$note = Note::where('name_id', $this->id)
->where('user_id', auth()->id())
->first();

if (! $note) {
return null;
}

return $note->content;
}
}
30 changes: 30 additions & 0 deletions app/Models/Note.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Note extends Model
{
use HasFactory;

protected $table = 'notes';

protected $fillable = [
'user_id',
'name_id',
'content',
];

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

public function name(): BelongsTo
{
return $this->belongsTo(Name::class);
}
}
34 changes: 34 additions & 0 deletions app/Services/AddNoteToName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Services;

use App\Models\Note;

class AddNoteToName extends BaseService
{
private Note $note;

public function __construct(
public int $nameId,
public int $userId,
public string $noteText,
) {
}

public function execute(): Note
{
$this->create();

return $this->note;
}

private function create(): void
{
$this->note = Note::updateOrCreate([
'name_id' => $this->nameId,
'user_id' => $this->userId,
], [
'content' => $this->noteText,
]);
}
}
2 changes: 2 additions & 0 deletions app/Services/CreateAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\NameList;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

/**
* Create an account for the user.
Expand Down Expand Up @@ -55,6 +56,7 @@ private function createDefaultList(): void
'is_public' => false,
'can_be_modified' => true,
'is_list_of_favorites' => false,
'uuid' => Str::uuid(),
]);
}
}
29 changes: 29 additions & 0 deletions database/factories/NoteFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Database\Factories;

use App\Models\Name;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Note>
*/
class NoteFactory extends Factory
{
protected static ?string $password;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'user_id' => User::factory(),
'name_id' => Name::factory(),
'content' => fake()->sentence(),
];
}
}
24 changes: 24 additions & 0 deletions database/migrations/2024_01_16_223956_create_name_user_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('notes', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('name_id');
$table->string('content');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('name_id')->references('id')->on('names')->onDelete('cascade');
});
}
};
54 changes: 54 additions & 0 deletions resources/views/names/partials/note.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
@if ($note != '')

<div x-show="! edit" class="flex justify-between">
<span class="text-sm mb-2 text-gray-500 flex items-center"><x-heroicon-o-eye-slash class="w-4 h-4 mr-2" />Note privée</span>

<ul class="text-sm">
<li class="inline mr-2"><span @click="edit = true" class="underline cursor-pointer hover:underline">Editer</span></li>
<li class="inline"><span class="underline cursor-pointer hover:underline">Supprimer</span></li>
</ul>
</div>
<div x-show='! edit'>
{!! $note !!}
</div>

@else

<div x-show="! edit" class="flex items-center justify-between">
<p class="text-sm">Ajoutez une note privée à ce prénom.</p>
@auth
<span @click="edit = true" class="rounded-md bg-white px-2.5 py-1.5 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50 cursor-pointer flex items-center">
<x-heroicon-o-pencil-square class="w-4 h-4 mr-2 text-gray-500" />
<span>Ajouter</span>
</span>
@else
<a href="{{ route('login') }}" class="rounded-md bg-white px-2.5 py-1.5 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50 cursor-pointer flex items-center">
<x-heroicon-o-pencil-square class="w-4 h-4 mr-2 text-gray-500" />
<span>Ajouter</span>
</a>
@endauth
</div>

@endif

<form x-show="edit">
<div class="relative mb-3">
<x-textarea class="mt-1 block w-full"
id="note"
name="note"
type="text">{{ old('note', $note) }}</x-textarea>

<x-input-error class="mt-2" :messages="$errors->get('note')" />
</div>

<div class="flex items-center">
<button
hx-put="{{ $name['url']['note_edit'] }}"
hx-headers='{"X-CSRF-TOKEN": "{{ csrf_token() }}"}'
class="px-2 py-1 inline-flex items-center justify-center whitespace-nowrap rounded-md font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 border border-gray-300 bg-transparent shadow-sm hover:bg-accent hover:text-accent-foreground transition ease-in-out duration-150 hover:bg-gray-100 dark:bg-gray-700 dark:text-white dark:hover:text-gray-800 mr-4">
Sauvegarder
</button>

<span @click="edit = false" class="cursor-pointer underline hover:no-underline">Annuler</span>
</div>
</form>
17 changes: 11 additions & 6 deletions resources/views/names/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,22 @@
<div class="mb-6 p-2 border border-gray-200 rounded-lg">
<ul class="grid grid-cols-2 text-sm">
<li>
<a href="#origine" class="underline">Origine</a>
<a href="#origine" class="underline hover:no-underline">Origine</a>
</li>
<li>
<a href="#personnalite" class="underline">Personnalité</a>
<a href="#personnalite" class="underline hover:no-underline">Personnalité</a>
</li>
<li>
<a href="#celebrites" class="underline">Célébrités portant ce prénom</a>
<a href="#celebrites" class="underline hover:no-underline">Célébrités portant ce prénom</a>
</li>
<li>
<a href="#references" class="underline">Références artistiques</a>
<a href="#references" class="underline hover:no-underline">Références artistiques</a>
</li>
<li>
<a href="#similaires" class="underline">Prénoms similaires</a>
<a href="#similaires" class="underline hover:no-underline">Prénoms similaires</a>
</li>
<li>
<a href="#elfiques" class="underline">Traits elfiques</a>
<a href="#elfiques" class="underline hover:no-underline">Traits elfiques</a>
</li>
</ul>
</div>
Expand Down Expand Up @@ -108,6 +108,11 @@
</div>
</div>

<!-- user note, if it exists -->
<div class="mb-10 p-4 border rounded-lg border-gray-200 bg-gray-100" x-data="{edit: false}">
@include('names.partials.note')
</div>

<!-- origin -->
<div class="mb-8">
<h2 class="mb-2 flex items-center" id="origine">
Expand Down
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\SearchController;
use App\Http\Controllers\ShareController;
use App\Http\Controllers\UserNameController;
use Illuminate\Support\Facades\Route;

Route::get('partage/{uuid}', [ShareController::class, 'show'])->name('share.show');
Expand Down Expand Up @@ -46,6 +47,9 @@

// used on the show page
Route::put('prenoms/{id}/show/favorite', [NameFavoriteController::class, 'update'])->name('favorite.name.update');

// set the note for the given name
Route::put('prenoms/{id}/{name}/note', [UserNameController::class, 'update'])->name('user.name.update');
});

Route::get('favoris', [FavoriteController::class, 'index'])->name('favorite.index');
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/Models/NameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Models\Name;
use App\Models\NameList;
use App\Models\NameStatistic;
use App\Models\Note;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

Expand Down Expand Up @@ -32,4 +34,22 @@ public function it_has_many_name_lists(): void

$this->assertTrue($name->lists()->exists());
}

/** @test */
public function it_gets_the_content_of_the_note_for_the_user_if_it_is_set(): void
{
$name = Name::factory()->create();
$user = User::factory()->create();
$this->be($user);
Note::factory()->create([
'name_id' => $name->id,
'user_id' => $user->id,
'content' => 'This is a note',
]);

$this->assertEquals(
'This is a note',
$name->getNoteForUser()
);
}
}
Loading

0 comments on commit f0b610f

Please sign in to comment.