1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Repositories; |
4
|
|
|
|
5
|
|
|
use App\Models\NewsItem; |
6
|
|
|
use Illuminate\Contracts\Pagination\Paginator; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
class NewsItemRepository |
10
|
|
|
{ |
11
|
|
|
public static function getAllOnline(): Collection |
12
|
|
|
{ |
13
|
|
|
return NewsItem::online() |
14
|
|
|
->orderBy('publish_date', 'desc') |
15
|
|
|
->get(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public static function getLatest(int $amount): Collection |
19
|
|
|
{ |
20
|
|
|
return NewsItem::online() |
21
|
|
|
->orderBy('publish_date', 'desc') |
22
|
|
|
->take($amount) |
23
|
|
|
->get(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function findOnline(int $id): NewsItem |
27
|
|
|
{ |
28
|
|
|
return NewsItem::online()->findOrFail($id); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function findByUrl(string $url): NewsItem |
32
|
|
|
{ |
33
|
|
|
return NewsItem::online() |
34
|
|
|
->where('url->'.content_locale(), $url) |
35
|
|
|
->firstOrFail(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return \App\Models\NewsItem|null |
40
|
|
|
*/ |
41
|
|
|
public static function findNext(NewsItem $newsItem) |
42
|
|
|
{ |
43
|
|
|
return NewsItem::online() |
44
|
|
|
->where('publish_date', '>', $newsItem->publish_date) |
45
|
|
|
->orderBy('publish_date', 'desc') |
46
|
|
|
->first(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return \App\Models\NewsItem|null |
51
|
|
|
*/ |
52
|
|
|
public static function findPrevious(NewsItem $newsItem) |
53
|
|
|
{ |
54
|
|
|
return NewsItem::online() |
55
|
|
|
->where('publish_date', '<', $newsItem->publish_date) |
56
|
|
|
->orderBy('publish_date', 'desc') |
57
|
|
|
->first(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function paginate(int $perPage): Paginator |
61
|
|
|
{ |
62
|
|
|
return NewsItem::online()->paginate($perPage); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|