Skip to content

Commit

Permalink
Campaigns: Systems as a multi dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
spitfire305 committed Jan 16, 2024
1 parent 886ad4f commit ba1e261
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/Http/Resources/Public/CampaignResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function toArray($request)
'followers' => number_format($campaign->follower),
'entities' => number_format($campaign->visible_entity_count),
'locale' => $campaign->locale,
'system' => $campaign->system,
'system' => $campaign->getSystems(),
'is_open' => $campaign->isOpen(),
];
}
Expand Down
12 changes: 12 additions & 0 deletions app/Models/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,16 @@ public function hasVanity(): bool
{
return $this->slug != $this->id;
}

public function getSystems(): string
{
$systems = '';
foreach ($this->systems as $system) {
if ($systems) {
$systems .= ', ';
}
$systems .= $system->name;
}
return $systems;
}
}
11 changes: 0 additions & 11 deletions app/Models/GameSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,10 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Concerns\Acl;
use App\Models\Concerns\SortableTrait;
use App\Traits\ExportableTrait;
use App\Models\CampaignSystem;

class GameSystem extends Model
{
use Acl;
use ExportableTrait;
use SortableTrait;

public $fillable = [
'name',
];

public function campaignSystem()
{
return $this->hasMany(CampaignSystem::class, 'system_id', 'id');
Expand Down
11 changes: 5 additions & 6 deletions app/Models/Scopes/CampaignScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,11 @@ public function scopeFilterPublic(Builder $query, array $options): Builder
}

if (!empty($system)) {
$valid = \App\Facades\CampaignCache::systems();
if ($system == 'other') {
$query->whereNotIn('system', $valid);
} elseif (in_array($system, $valid)) {
$query->where('system', $system);
}
$query
->select('campaigns.*')
->leftJoin('campaign_system as cs', function ($join) {
$join->on('cs.campaign_id', '=', 'campaigns.id');
})->whereIn('cs.system_id', $system)->distinct();
}
$boosted = Arr::get($options, 'is_boosted');
if ($boosted === "1") {
Expand Down
2 changes: 1 addition & 1 deletion app/Services/Api/CampaignService.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function filters(): self
'sk' => 'Slovak',
]
],
'system' => [
'system[]' => [
'title' => 'System',
'options' => CampaignCache::systems()
],
Expand Down
8 changes: 6 additions & 2 deletions app/Services/Caches/CampaignCacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ public function entityCount(): int
*/
public function systems(): array
{
$key = 'campaign_public_systems';
$key = 'campaign_systems';
if ($this->has($key)) {
return $this->get($key);
}
$data = [];
$systems = GameSystem::withCount('campaignSystem')->orderBy('campaign_system_count', 'desc')->get(20);

$data = GameSystem::withCount('campaignSystem')->orderBy('campaign_system_count', 'desc')->get()->toArray();
foreach ($systems as $system) {
$data[$system->id] = $system->name;
}

$this->put($key, $data, 24 * 3600);
return $data;
Expand Down

0 comments on commit ba1e261

Please sign in to comment.