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: lists of names for admins #20

Merged
merged 8 commits into from
Feb 5, 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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ It contains:

MIT license.

### how to
### How to import the INSEE database

- first, * replace all semi colons with commas in the CSV provided by the INSEE
* `sed 's/;/,/g' yourfile.csv > newfile.csv`
- next, run the command `php artisan openname:import`
- finally, run the command `php artisan openname:count-total-after-import`

### Cache

There is a lot of cache instances in this project. Everything is cached. If you encounter any problem while debugging, remember to clear the cache with `php artisan cache:clear`.
5 changes: 5 additions & 0 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ public function index(): View
});
}

$lists = Cache::remember('admin-lists', 604800, function () {
return HomeViewModel::adminLists();
});

return view('home.index', [
'twentyMostPopularNames' => $popularNames,
'stats' => $stats,
'nameSpotlight' => HomeViewModel::nameSpotlight(),
'favorites' => $favoritedNamesForLoggedUser,
'lists' => $lists,
]);
}
}
1 change: 1 addition & 0 deletions app/Http/Controllers/ListNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function store(Request $request, int $listId, int $nameId): Response
Cache::forget('route-list-' . $requestedList->id);
Cache::forget('user-lists-' . auth()->id());
Cache::forget('list-details-' . $requestedList->id);
Cache::forget('admin-lists');

return new HtmxResponseClientRedirect(route('list.show', [
'liste' => $requestedList->id,
Expand Down
35 changes: 35 additions & 0 deletions app/Http/Controllers/ListSystemController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Http\Controllers;

use App\Http\ViewModels\User\ListViewModel;
use App\Services\CreateList;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;

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

if (! auth()->user()->is_administrator) {
abort(403);
}

$list->is_public = ! $list->is_public;
$list->save();

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

return Redirect::route('list.show', [
'liste' => $list->id,
]);
}
}
37 changes: 37 additions & 0 deletions app/Http/Controllers/PublicListController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Controllers;

use App\Http\ViewModels\User\ListViewModel;
use App\Http\ViewModels\User\UserViewModel;
use App\Services\CreateList;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;

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

$details = Cache::remember('list-details-' . $requestedList->id, 604800, function () use ($requestedList) {
return ListViewModel::show($requestedList);
});

if (! auth()->check()) {
$favoritedNamesForLoggedUser = collect();
} else {
$favoritedNamesForLoggedUser = Cache::remember('user-favorites-' . auth()->id(), 604800, function () {
return UserViewModel::favorites();
});
}

return view('user.lists.public.show', [
'list' => $details,
'favorites' => $favoritedNamesForLoggedUser,
]);
}
}
40 changes: 40 additions & 0 deletions app/Http/ViewModels/Home/HomeViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Helpers\StringHelper;
use App\Http\ViewModels\Names\NameViewModel;
use App\Models\Name;
use App\Models\NameList;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Number;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -76,4 +78,42 @@ public static function serverStats(): array
'total_names' => Number::format($totalNames, locale: 'fr'),
];
}

/**
* Get the list of all the public lists that administrators have set public.
*
* @return Collection
*/
public static function adminLists(): Collection
{
return NameList::where('is_public', true)
->withCount('names')
->with('names')
->inRandomOrder()
->get()
->map(fn (NameList $list) => [
'id' => $list->id,
'name' => $list->name,
'total' => Number::format($list->names_count, locale: 'fr'),
'names' => $list->names()
->inRandomOrder()
->take(4)
->get()
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::formatNameFromDB($name->name),
'url' => [
'show' => route('name.show', [
'id' => $name->id,
'name' => StringHelper::sanitizeNameForURL($name->name),
]),
],
]),
'url' => [
'show' => route('list.public.show', [
'liste' => $list->id,
]),
],
]);
}
}
10 changes: 10 additions & 0 deletions app/Http/ViewModels/User/ListViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Name;
use App\Models\NameList;
use Illuminate\Support\Number;
use Illuminate\Support\Str;

class ListViewModel
{
Expand Down Expand Up @@ -42,12 +43,16 @@ public static function show(NameList $list): array
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::formatNameFromDB($name->name),
'origins' => Str::words($name->origins, 50, '...'),
'total' => Number::format($name->total, locale: 'fr'),
'url' => [
'show' => route('name.show', [
'id' => $name->id,
'name' => StringHelper::sanitizeNameForURL($name->name),
]),
'favorite' => route('favorite.update', [
'id' => $name->id,
]),
'destroy' => route('list.name.destroy', [
'liste' => $list->id,
'id' => $name->id,
Expand All @@ -61,6 +66,8 @@ public static function show(NameList $list): array
'description' => $list->description,
'names' => $names,
'uuid' => StringHelper::shareLink($list->uuid),
'visibility' => $list->is_public,
'created_at' => $list->created_at->isoFormat('LL'),
'url' => [
'show' => route('list.show', [
'liste' => $list->id,
Expand All @@ -74,6 +81,9 @@ public static function show(NameList $list): array
'search' => route('list.search.index', [
'liste' => $list->id,
]),
'visibility' => route('list.system.update', [
'liste' => $list->id,
]),
],
];
}
Expand Down
1 change: 0 additions & 1 deletion app/Models/NameList.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class NameList extends Model
'is_public',
'can_be_modified',
'is_list_of_favorites',
'system_list',
];

protected $casts = [
Expand Down
2 changes: 2 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class User extends Authenticatable implements MustVerifyEmail
'email_verified_at',
'password',
'last_name',
'is_administrator',
];

/**
Expand All @@ -43,6 +44,7 @@ class User extends Authenticatable implements MustVerifyEmail
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'is_administrator' => 'boolean',
];

public function lists(): HasMany
Expand Down
Binary file removed bun.lockb
Binary file not shown.
Loading
Loading