Skip to content

Commit

Permalink
feat: add a public note on a name on a public list (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Feb 20, 2024
1 parent 4181d58 commit 5663e6c
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 6 deletions.
80 changes: 80 additions & 0 deletions app/Http/Controllers/StoreNoteForNameInListController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Http\Controllers;

use App\Models\Name;
use App\Services\ToggleNameToNameList;
use App\Services\UpdateNoteToNameInList;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;

class StoreNoteForNameInListController extends Controller
{
public function edit(Request $request, int $listId, int $nameId): View
{
$requestedList = $request->attributes->get('list');

$name = Name::findOrFail($nameId);

$record = DB::table('list_name')
->where('name_id', $nameId)
->where('list_id', $listId)
->first();

return view('user.lists.edit-note', [
'list' => [
'name' => $requestedList->name,
'url' => [
'show' => route('list.show', ['liste' => $listId]),
'update' => route('name.list.update', [
'liste' => $listId,
'id' => $nameId,
]),
],
],
'name' => $name->name,
'note' => is_null($record->public_note) ? '' : $record->public_note,
]);
}

/**
* Add a note for the given name in the given list.
*/
public function update(Request $request, int $listId, int $nameId): RedirectResponse
{
$requestedList = $request->attributes->get('list');

(new UpdateNoteToNameInList(
nameListId: $listId,
nameId: $nameId,
note: $request->input('note'),
))->execute();

Cache::forget('route-list-' . $requestedList->id);
Cache::forget('user-lists-' . auth()->id());
Cache::forget('list-details-' . $requestedList->id);
Cache::forget('admin-lists');

return Redirect::route('list.show', [
'liste' => $listId,
]);
}

public function destroy(Request $request, int $listId, int $nameId): void
{
$requestedList = $request->attributes->get('list');

(new ToggleNameToNameList(
nameId: $nameId,
listId: $requestedList->id,
))->execute();

Cache::forget('route-list-' . $requestedList->id);
Cache::forget('user-lists-' . auth()->id());
Cache::forget('list-details-' . $requestedList->id);
}
}
6 changes: 6 additions & 0 deletions app/Http/ViewModels/User/ListViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public static function index(): array
public static function show(NameList $list): array
{
$names = $list->names()
->withPivot('public_note')
->orderBy('name')->get()
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::formatNameFromDB($name->name),
'origins' => Str::words($name->origins, 50, '...'),
'total' => Number::format($name->total),
'public_note' => $name->pivot->public_note,
'url' => [
'show' => route('name.show', [
'id' => $name->id,
Expand All @@ -57,6 +59,10 @@ public static function show(NameList $list): array
'liste' => $list->id,
'id' => $name->id,
]),
'note' => route('name.list.edit', [
'liste' => $list->id,
'id' => $name->id,
]),
],
]);

Expand Down
2 changes: 1 addition & 1 deletion app/Models/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function mainCharacteristics(): BelongsToMany

public function lists(): BelongsToMany
{
return $this->belongsToMany(NameList::class, 'list_name', 'name_id', 'list_id');
return $this->belongsToMany(NameList::class, 'list_name', 'name_id', 'list_id')->withPivot('public_note');
}

public function toSitemapTag(): Url|string|array
Expand Down
2 changes: 1 addition & 1 deletion app/Models/NameList.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function user(): BelongsTo

public function names(): BelongsToMany
{
return $this->belongsToMany(Name::class, 'list_name', 'list_id', 'name_id');
return $this->belongsToMany(Name::class, 'list_name', 'list_id', 'name_id')->withPivot('public_note');
}

public function scopeFavorite(Builder $query): void
Expand Down
28 changes: 28 additions & 0 deletions app/Services/UpdateNoteToNameInList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Services;

use Illuminate\Support\Facades\DB;

class UpdateNoteToNameInList extends BaseService
{
public function __construct(
public int $nameListId,
public int $nameId,
public string $note,
) {
}

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

private function create(): void
{
DB::table('list_name')
->where('list_id', $this->nameListId)
->where('name_id', $this->nameId)
->update(['public_note' => $this->note]);
}
}
15 changes: 15 additions & 0 deletions database/migrations/2024_02_19_182155_update_list_name_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

return new class extends Migration
{
public function up(): void
{
Schema::table('list_name', function (Blueprint $table) {
$table->text('public_note')->nullable();
});
}
};
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ parameters:
- app/Http/Controllers/Auth/VerifyEmailController.php
- app/Http/ViewModels/Names/NameViewModel.php
- app/Http/ViewModels/Search/SearchViewModel.php
- app/Http/ViewModels/User/ListViewModel.php
#
# checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
Expand Down
62 changes: 62 additions & 0 deletions resources/views/user/lists/edit-note.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<x-guest-layout class="bg-gray-50">
<div class="bg-violet-100 mb-10">
<div class="border-b border-violet-200">
@include('layouts.unlogged-navigation')
</div>

<div class="border-b border-violet-200">
<div class="mx-auto max-w-7xl sm:px-6 lg:px-8 py-2">
<ul class="text-xs">
<li class="inline after:content-['>'] after:text-gray-500 after:text-xs">
<a hx-boost="true" href="{{ route('home.index') }}" class="text-violet-900 underline">Accueil</a>
</li>
<li class="inline after:content-['>'] after:text-gray-500 after:text-xs">
<a hx-boost="true" href="{{ route('list.index') }}" class="text-violet-900 underline">Toutes vos listes de prénoms</a>
</li>
<li class="inline after:content-['>'] after:text-gray-500 after:text-xs">
<a hx-boost="true" href="{{ $list['url']['show'] }}" class="text-violet-900 underline">{{ $list['name'] }}</a>
</li>
<li class="inline">Ajout d'une note</li>
</ul>
</div>
</div>
</div>

<div>
<div class="mx-auto max-w-2xl sm:px-6 lg:px-8 py-2">
<form method="POST" action="{{ $list['url']['update'] }}" class="mb-8 shadow sm:rounded-lg">
@csrf
@method('PUT')

<div class="relative border-b dark:border-gray-600 px-6 py-4 bg-yellow-50">
<h1 class="text-center font-bold">Ajout d'une note au prénom {{ $name }}</h1>
</div>

<!-- description -->
<div class="relative px-6 pt-4 pb-6">
<x-input-label for="note"
:value="'Note'" />

<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('description')" />
</div>

<!-- actions -->
<div class="flex items-center justify-between border-t dark:border-gray-600 bg-white dark:bg-gray-800 px-6 py-4">
<x-link href="{{ $list['url']['show'] }}">Retour</x-link>

<div>
<x-primary-button class="w-full text-center" dusk="submit-form-button">
Sauvegarder
</x-primary-button>
</div>
</div>
</div>
</form>
</div>
</div>
</x-guest-layout>
21 changes: 17 additions & 4 deletions resources/views/user/lists/partials/names.blade.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
<div id="names-index">
@foreach ($list['names'] as $name)
<div
class="flex items-center justify-between border border-transparent hover:bg-gray-50 hover:border-gray-200 px-2 py-1 rounded-sm"
class="flex items-center justify-between border hover:bg-gray-50 hover:border-gray-200 px-2 py-1 mb-2 rounded-md"
hx-target="this"
hx-swap="delete"
>
<div class="flex items-center">
<div class="rounded-full w-6 mr-4 ring-4 ring-violet-100">{!! \App\Helpers\NameHelper::getAvatar($name['name']) !!}</div>
<div class="flex items-center relative">
<div class="top-0 relative rounded-full w-4 mr-4 ring-4 ring-violet-100">{!! \App\Helpers\NameHelper::getAvatar($name['name']) !!}</div>

<div class="flex-col">
<a href="{{ $name['url']['show'] }}" class="text-lg hover:underline">{{ $name['name'] }} <span x-text="last_name"></span></a>
<p class="text-xs text-gray-700">{{ $name['total'] }} utilisations depuis 1900</p>

@if ($name['public_note'] != '')
<p>{{ $name['public_note'] }}</p>
@endif

@if (auth()->user()->is_administrator)
<p class="mb-1 italic flex items-center">
<x-heroicon-c-plus class="w-3 h-3 mr-1 text-gray-500" />
<a href="{{ $name['url']['note'] }}" class="text-xs hover:underline">ajouter une note</a>
</p>
@endif
<p class="text-xs text-gray-700 flex">
<x-heroicon-c-arrow-trending-up class="w-4 h-4 mr-1 text-gray-500" />
{{ $name['total'] }} utilisations depuis 1900</p>
</div>
</div>

Expand Down
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Http\Controllers\PublicListController;
use App\Http\Controllers\SearchController;
use App\Http\Controllers\ShareController;
use App\Http\Controllers\StoreNoteForNameInListController;
use App\Http\Controllers\TermsController;
use App\Http\Controllers\UserNameController;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -78,6 +79,9 @@
Route::delete('listes/{liste}/prenoms/{id}', [ListNameController::class, 'destroy'])->name('list.name.destroy');

Route::put('listes/{liste}/prenoms/{id}/set', [NameController::class, 'storeNameInList'])->name('name.list.store');

Route::get('listes/{liste}/prenoms/{id}/note/update', [StoreNoteForNameInListController::class, 'edit'])->name('name.list.edit');
Route::put('listes/{liste}/prenoms/{id}/note', [StoreNoteForNameInListController::class, 'update'])->name('name.list.update');
});

Route::get('profil', [ProfileController::class, 'show'])->name('profile.show');
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/ViewModels/User/ListViewModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ public function it_gets_the_list_of_names_in_the_list(): void
'total' => '1 000',
'origins' => 'test',
'name' => 'Test',
'public_note' => null,
'url' => [
'show' => env('APP_URL') . '/prenoms/' . $name->id . '/test',
'favorite' => env('APP_URL') . '/prenoms/' . $name->id . '/favorite',
'destroy' => env('APP_URL') . '/listes/' . $nameList->id . '/prenoms/' . $name->id,
'note' => env('APP_URL') . '/listes/' . $nameList->id . '/prenoms/' . $name->id . '/note/update',
],
],
],
Expand Down

0 comments on commit 5663e6c

Please sign in to comment.