Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: vote a list #18

Merged
merged 6 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/Helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ public static function formatNameFromDB(string $name): string

return $formattedName;
}

public static function shareLink(string $uuid): string
{
return route('share.show', [
'uuid' => $uuid,
]);
}
}
12 changes: 12 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,15 @@ public function show(Request $request): View
'numerology' => $numerology,
'favorites' => $favoritedNamesForLoggedUser,
'lists' => $lists,
'note' => $note,
'url' => [
'edit' => route('user.name.edit', [
'id' => $requestedName->id,
]),
'delete' => route('user.name.destroy', [
'id' => $requestedName->id,
]),
],
]);
}

Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/NameFavoriteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function update(Request $request): View
))->execute();

Cache::forget('user-favorites-' . auth()->id());
Cache::forget('user-favorites-details-' . auth()->id());

return view('components.favorite', [
'name' => NameViewModel::details($name),
Expand Down
12 changes: 12 additions & 0 deletions app/Http/Controllers/ShareController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ShareController extends Controller
{
public function show(Request $request): void
{
}
}
93 changes: 93 additions & 0 deletions app/Http/Controllers/UserNameController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace App\Http\Controllers;

use App\Helpers\StringHelper;
use App\Services\AddNoteToName;
use App\Services\DestroyNote;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\View\View;

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

return view('components.note-show', [
'note' => $name->getNoteForUser(),
'url' => route('user.name.edit', [
'id' => $name->id,
]),
'deleteUrl' => route('user.name.destroy', [
'id' => $name->id,
]),
]);
}

public function edit(Request $request): View
{
$name = $request->attributes->get('name');

return view('names.partials.edit-note', [
'note' => $name->getNoteForUser(),
'url' => [
'show' => route('user.name.show', [
'id' => $name->id,
]),
'update' => route('user.name.update', [
'id' => $name->id,
'name' => StringHelper::sanitizeNameForURL($name->name),
]),
],
]);
}

public function update(Request $request): View
{
$name = $request->attributes->get('name');

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

Cache::forget('user-favorites-' . auth()->id());
Cache::forget('user-favorites-details-' . auth()->id());

return view('components.note-show', [
'note' => $name->getNoteForUser(),
'url' => route('user.name.edit', [
'id' => $name->id,
]),
'deleteUrl' => route('user.name.destroy', [
'id' => $name->id,
]),
]);
}

public function destroy(Request $request): View
{
$name = $request->attributes->get('name');

(new DestroyNote(
userId: auth()->id(),
nameId: $name->id,
))->execute();

Cache::forget('user-favorites-' . auth()->id());
Cache::forget('user-favorites-details-' . auth()->id());

return view('components.note-show', [
'note' => $name->getNoteForUser(),
'url' => route('user.name.edit', [
'id' => $name->id,
]),
'deleteUrl' => route('user.name.destroy', [
'id' => $name->id,
]),
]);
}
}
3 changes: 3 additions & 0 deletions app/Http/ViewModels/Names/NameViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public static function details(Name $name): array
'favorite' => route('favorite.name.update', [
'id' => $name->id,
]),
'note_edit' => route('user.name.update', [
'id' => $name->id,
]),
],
];
}
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
1 change: 1 addition & 0 deletions app/Http/ViewModels/User/ListViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static function show(NameList $list): array
'name' => $list->name,
'description' => $list->description,
'names' => $names,
'uuid' => StringHelper::shareLink($list->uuid),
'url' => [
'show' => route('list.show', [
'liste' => $list->id,
Expand Down
1 change: 1 addition & 0 deletions app/Http/ViewModels/User/UserViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static function index(): array
'id' => $name->id,
'name' => StringHelper::formatNameFromDB($name->name),
'total' => Number::format($name->total, locale: 'fr'),
'note' => $name->getNoteForUser(),
'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;
}
}
1 change: 1 addition & 0 deletions app/Models/NameList.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class NameList extends Model

protected $fillable = [
'user_id',
'uuid',
'name',
'description',
'is_public',
Expand Down
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(),
]);
}
}
2 changes: 2 additions & 0 deletions app/Services/CreateList.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Services;

use App\Models\NameList;
use Illuminate\Support\Str;

class CreateList extends BaseService
{
Expand All @@ -27,6 +28,7 @@ private function createList(): void
{
$this->nameList = NameList::create([
'user_id' => auth()->id(),
'uuid' => Str::uuid(),
'name' => $this->name,
'description' => $this->description,
'is_public' => $this->isPublic,
Expand Down
31 changes: 31 additions & 0 deletions app/Services/DestroyNote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Services;

use App\Models\Note;

class DestroyNote extends BaseService
{
private Note $note;

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

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

private function check(): void
{
$this->note = Note::where([
'name_id' => $this->nameId,
'user_id' => $this->userId,
])->firstOrFail();

$this->note->delete();
}
}
Binary file modified bun.lockb
Binary file not shown.
Loading