|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CSlant\Blog\Api\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use CSlant\Blog\Api\Models\PostView; |
|
7
|
|
|
use CSlant\Blog\Core\Http\Responses\Base\BaseHttpResponse; |
|
8
|
|
|
use CSlant\Blog\Core\Models\Post; |
|
9
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
11
|
|
|
use Illuminate\Support\Arr; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class PostService |
|
15
|
|
|
* |
|
16
|
|
|
* @package CSlant\Blog\Api\Services |
|
17
|
|
|
* |
|
18
|
|
|
* @method BaseHttpResponse httpResponse() |
|
19
|
|
|
*/ |
|
20
|
|
|
class PostService |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Get posts by filters. |
|
24
|
|
|
* |
|
25
|
|
|
* @param array<string, mixed> $filters |
|
26
|
|
|
* |
|
27
|
|
|
* @return LengthAwarePaginator<Post> |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getCustomFilters(array $filters): LengthAwarePaginator |
|
30
|
|
|
{ |
|
31
|
|
|
$data = Post::query(); |
|
32
|
|
|
|
|
33
|
|
|
if ($filters['categories'] !== null) { |
|
34
|
|
|
$categories = array_filter((array) $filters['categories']); |
|
35
|
|
|
|
|
36
|
|
|
$data = $data->whereHas('categories', function (Builder $query) use ($categories): void { |
|
37
|
|
|
$query->whereIn('categories.id', $categories); |
|
38
|
|
|
}); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ($filters['tags'] !== null) { |
|
42
|
|
|
$tags = array_filter((array) $filters['tags']); |
|
43
|
|
|
|
|
44
|
|
|
$data = $data->whereHas('tags', function (Builder $query) use ($tags): void { |
|
45
|
|
|
$query->whereIn('tags.id', $tags); |
|
46
|
|
|
}); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$orderBy = Arr::get($filters, 'order_by', 'updated_at'); |
|
50
|
|
|
$order = Arr::get($filters, 'order', 'desc'); |
|
51
|
|
|
|
|
52
|
|
|
$data = $data |
|
53
|
|
|
->wherePublished() |
|
54
|
|
|
->orderBy($orderBy, $order); |
|
55
|
|
|
|
|
56
|
|
|
return $data->paginate((int) $filters['per_page']); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function trackView(int $postId, string $ipAddress): bool |
|
60
|
|
|
{ |
|
61
|
|
|
$post = Post::find($postId); |
|
62
|
|
|
if(!$post) return false; |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
$postView = PostView::where('post_id', $postId) |
|
65
|
|
|
->where('ip_address', $ipAddress) |
|
66
|
|
|
->first(); |
|
67
|
|
|
|
|
68
|
|
|
$shouldIncrementView = false; |
|
69
|
|
|
|
|
70
|
|
|
if(!$postView) |
|
71
|
|
|
{ |
|
72
|
|
|
// Access this post for the first time from this IP |
|
73
|
|
|
PostView::create([ |
|
74
|
|
|
'post_id' => $postId, |
|
75
|
|
|
'ip_address' => $ipAddress, |
|
76
|
|
|
'time_check' => Carbon::now()->addHours(), |
|
77
|
|
|
]); |
|
78
|
|
|
$shouldIncrementView = true; |
|
79
|
|
|
} |
|
80
|
|
|
else { |
|
81
|
|
|
// Check if field time_check is passed |
|
82
|
|
|
if(Carbon::now()->isAfter($postView->time_check)) |
|
83
|
|
|
{ |
|
84
|
|
|
// Update field time_check |
|
85
|
|
|
$postView->update([ |
|
86
|
|
|
'time_check' => Caton::now()->addHours(), |
|
|
|
|
|
|
87
|
|
|
]); |
|
88
|
|
|
$shouldIncrementView = true; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if($shouldIncrementView) |
|
93
|
|
|
{ |
|
94
|
|
|
$post->increment('views'); |
|
95
|
|
|
} |
|
96
|
|
|
return $shouldIncrementView; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|