Skip to content

Commit

Permalink
Merge pull request #370 from pacoorozco/issue-369
Browse files Browse the repository at this point in the history
Increase performance by reducing # of queries
  • Loading branch information
pacoorozco authored Oct 13, 2022
2 parents 0ff27d7 + 49de360 commit 6a00501
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 29 deletions.
25 changes: 16 additions & 9 deletions app/Models/Level.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
use Laracodes\Presenter\Traits\Presentable;
use QCod\ImageUp\HasImageUploads;

Expand Down Expand Up @@ -92,21 +93,27 @@ class Level extends Model

public static function findNextByExperience(int $experience): Level
{
return self::query()
return Cache::rememberForever('levels', function () {
return self::query()
->active()
->where('required_points', '>', $experience)
->orderBy('required_points', 'asc')
->first()
->orderBy('required_points', 'ASC')
->get();
})
->where('required_points', '>', $experience)
->first()
?? self::findByExperience($experience);
}

public static function findByExperience(int $experience): Level
{
return self::query()
return Cache::rememberForever('levels', function () {
return self::query()
->active()
->where('required_points', '<=', $experience)
->orderBy('required_points', 'desc')
->first()
->orderBy('required_points', 'ASC')
->get();
})
->where('required_points', '<=', $experience)
->last()
?? self::defaultLevel();
}

Expand All @@ -133,7 +140,7 @@ public function scopeActive(Builder $query): Builder
protected function image(): Attribute
{
return Attribute::make(
get: fn ($value) => $this->imageUrl()
get: fn($value) => $this->imageUrl()
);
}
}
12 changes: 0 additions & 12 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,6 @@ public function answeredQuestions(): BelongsToMany
->using(UserResponse::class);
}

public function hasQuestionsToAnswer(): bool
{
$answeredQuestions = $this->answeredQuestions()
->pluck('question_id')
->toArray();

return Question::query()
->published()
->whereNotIn('id', $answeredQuestions)
->exists();
}

public function answeredQuestionsCount(): int
{
return $this->answeredQuestions()->count();
Expand Down
34 changes: 34 additions & 0 deletions app/Observers/LevelObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Gamify\Observers;

use Gamify\Models\Level;
use Illuminate\Support\Facades\Cache;

class LevelObserver
{
public function created(Level $level): void
{
Cache::forget('levels');
}

public function updated(Level $level): void
{
Cache::forget('levels');
}

public function deleted(Level $level): void
{
Cache::forget('levels');
}

public function restored(Level $level): void
{
Cache::forget('levels');
}

public function forceDeleted(Level $level): void
{
//
}
}
4 changes: 4 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

namespace Gamify\Providers;

use Gamify\Models\Level;
use Gamify\Observers\LevelObserver;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Rules\Password;
Expand All @@ -51,5 +53,7 @@ public function boot(): void
});

Paginator::useBootstrapFour();

Level::observe(LevelObserver::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function up(): void
Schema::create('levels', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->unsignedInteger('required_points');
$table->unsignedInteger('required_points')->unique();
$table->boolean('active')->default(true);
$table->string('image_url')->nullable();
$table->timestamps();
Expand Down
4 changes: 2 additions & 2 deletions lang/en/question/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
'obtained_points' => 'Points obtained by answering this question',
'send' => 'Send',
'choices' => 'Choose your answer:',
'no_pending_questions' => 'Well done!. You have answered all questions.',
'only_hidden_questions_pending' => 'You have answered almost all questions, maybe be there are hidden questions pending.',

'no_pending_questions' => 'Well done!. You have answered all the public questions. Have you though about the hidden ones?',

'legend' => 'Legend:',
'correct_answer' => 'Correct answer.',
Expand Down
6 changes: 1 addition & 5 deletions resources/views/question/_empty-list.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
<p class="text-center">
@if (Auth::user()->hasQuestionsToAnswer())
{{ __('question/messages.ony_hidden_questions_pending') }}
@else
{{ __('question/messages.no_pending_questions') }}
@endif
{{ __('question/messages.no_pending_questions') }}
</p>

0 comments on commit 6a00501

Please sign in to comment.