Skip to content

Commit

Permalink
add method
Browse files Browse the repository at this point in the history
  • Loading branch information
ousid committed Feb 14, 2022
1 parent f323e0e commit 36841fe
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ After the visits get logged, you can retrieve the data by the following method:
| ----------- | ----------- | ----------- | ----------- |
| `withTotalVisitCount()` | `void` | get total visit count | `Post::withTotalVisitCount()->first()->visit_count_total` |
| `popularAllTime()` | `void` | get popular visits all time | `Post::popularAllTime()->get()` |
| `popularToday()` | `void` | get popular visits in the current day | `Post::popularToday(10)->get()` |
| `popularLastDays()` | int `$days` | get popular visits last given days | `Post::popularLastDays(10)->get()` |
| `popularThisWeek()` | `void` | get popular visits this week | `Post::popularThisWeek()->get()` |
| `popularLastWeek()` | `void` | get popular visits last week | `Post::popularLastWeek()->get()` |
Expand Down
14 changes: 13 additions & 1 deletion src/Concerns/FilterByPopularityTimeFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ public function scopePopularAllTime(Builder $builder): Builder
->orderBy('visit_count_total', 'desc');
}

// TODO: add popular today method with tests and documentation
/**
* Get the popular visits today
*
* @param Builder $builder
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePopularToday(Builder $builder): Builder
{
return $builder->popularBetween(
now()->startOfDay(),
now()->endOfDay()
);
}

/**
* Get the popular visits last given days
Expand Down
2 changes: 0 additions & 2 deletions src/Presenters/VisitPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,4 @@ public function user(): Model

return (new $user())->find($userId);
}

// TODO: get total visit count presenter method with tests and documentation
}
15 changes: 15 additions & 0 deletions tests/Feature/Visits/PopularityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@
expect($popularPosts->first()->visit_count_total)->toEqual(1);
});

it('gets popular records today', function () {
$post = Post::factory()->times(2)->create();

Carbon::setTestNow(now()->subDays(2));
$post->first()->visit();
$post->last()->visit();

Carbon::setTestNow();
$post->first()->visit();

$popularPosts = Post::popularToday()->get();

expect($popularPosts->count())->toBe(1);
});

it('gets popular records last x days', function () {
$post = Post::factory()->create();

Expand Down

0 comments on commit 36841fe

Please sign in to comment.