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

mark elephpants as prototypes #174

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions app/Console/Commands/ReadElephpants.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function handle(): void
'sponsor' => $elephpant->sponsor,
'year' => (int)$elephpant->year,
'image' => $this->processImage($elephpant),
'prototype' => $elephpant->prototype
]
);
}
Expand Down
12 changes: 11 additions & 1 deletion app/Elephpant.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class Elephpant extends Model
Expand All @@ -16,8 +16,18 @@ class Elephpant extends Model
'sponsor',
'year',
'image',
'prototype',
];

protected static function boot()
{
parent::boot();

static::addGlobalScope('nonPrototype', function (Builder $builder) {
$builder->where('prototype', 0);
});
}

public function users(): BelongsToMany
{
return $this->belongsToMany(User::class)
Expand Down
8 changes: 7 additions & 1 deletion app/Http/Controllers/ElephpantController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ class ElephpantController extends Controller
{
public function index(Request $request)
{
$elephpants = Elephpant::query()->filter($request)->orderBy('year', 'desc')->orderBy('id', 'desc')->get();
$elephpants = Elephpant::query()
->filter($request)
->withCount('users')
->withoutGlobalScope('nonPrototype')
->orderBy('year', 'desc')
->orderBy('id', 'desc')
->get();

return view('elephpant.index', compact('elephpants'));
}
Expand Down
8 changes: 7 additions & 1 deletion app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ class HomeController extends Controller
{
public function index(Request $request)
{
$elephpants = Elephpant::query()->filter($request)->orderBy('year', 'desc')->orderBy('id', 'desc')->get();
$elephpants = Elephpant::query()
->filter($request)
->withCount('users')
->withoutGlobalScope('nonPrototype')
->orderBy('year', 'desc')
->orderBy('id', 'desc')
->get();

return view('home', compact('elephpants'));
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/StatisticsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function index()
$elephpants = DB::table('elephpants')
->select(DB::raw('COUNT(elephpant_user.elephpant_id) as nbElephpant, id, name, description, SUM(elephpant_user.quantity) as totalElephpant, image'))
->leftJoin('elephpant_user', 'elephpants.id', '=', 'elephpant_user.elephpant_id')
->where('prototype', 0)
->orderBy('nbElephpant', 'desc')
->orderBy('elephpants.id', 'desc')
->orderBy('totalElephpant', 'desc')
Expand Down
2 changes: 2 additions & 0 deletions app/Queries/RankedUsersQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public function fetchAll(?string $country): Collection

$users = $userQuery
->join('elephpant_user', 'users.id', '=', 'elephpant_user.user_id')
->join('elephpants', 'elephpants.id', '=', 'elephpant_user.elephpant_id')
->select($visibleFields)
->selectRaw('SUM(elephpant_user.quantity) AS elephpants_total')
->selectRaw('COUNT(DISTINCT elephpant_user.elephpant_id) AS elephpants_unique')
->where('prototype', 0)
->groupBy($visibleFields)
->orderBy('elephpants_unique', 'desc')
->orderBy('elephpants_total', 'desc')
Expand Down
1 change: 1 addition & 0 deletions database/factories/ElephpantFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
'year' => (int)$faker->dateTimeBetween('-12 years', 'now')->format('Y'),
'sponsor' => $faker->company,
'image' => $faker->imageUrl(),
'prototype' => $faker->boolean(1)
];
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

class AddPrototypeFlagToElephpantsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('elephpants', function (Blueprint $table) {
$table->boolean('prototype')->default(false)->after('image');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('elephpants', function (Blueprint $table) {
$table->dropColumn('prototype');
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

class RemovePrototypeElephpantsFromCollections extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::statement('DELETE FROM elephpant_user WHERE elephpant_id IN (52,53,55,62)');
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
Loading