|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Services; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Requests\QuoteSearchRequest; |
|
6
|
|
|
use App\Http\Requests\QuoteStoreRequest; |
|
7
|
|
|
use App\Models\Quote; |
|
8
|
|
|
use App\Repositories\QuoteRepository; |
|
9
|
|
|
use Carbon\Carbon; |
|
10
|
|
|
|
|
11
|
|
|
class QuoteService |
|
12
|
|
|
{ |
|
13
|
|
|
private QuoteRepository $quoteRepository; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* QuoteService constructor. |
|
17
|
|
|
* |
|
18
|
|
|
* @param \App\Repositories\QuoteRepository $quoteRepository |
|
19
|
|
|
*/ |
|
20
|
7 |
|
public function __construct( |
|
21
|
|
|
QuoteRepository $quoteRepository |
|
22
|
|
|
) { |
|
23
|
7 |
|
$this->quoteRepository = $quoteRepository; |
|
24
|
7 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Return the team from the database |
|
28
|
|
|
* |
|
29
|
|
|
* @param int $quoteId |
|
30
|
|
|
* |
|
31
|
|
|
* @return \App\Models\Quote |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function getById(int $quoteId): Quote |
|
34
|
|
|
{ |
|
35
|
1 |
|
return $this->quoteRepository->getById($quoteId); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get all the quotes |
|
40
|
|
|
* |
|
41
|
|
|
* @param int|null $recordsPerPage |
|
42
|
|
|
* @param array|null $searchParameters |
|
43
|
|
|
* |
|
44
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function getQuotes(int $recordsPerPage = null, array $searchParameters = null) |
|
47
|
|
|
{ |
|
48
|
1 |
|
return $this->quoteRepository->getAll($recordsPerPage, $searchParameters); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Create a quote |
|
53
|
|
|
* |
|
54
|
|
|
* @param \App\Http\Requests\QuoteStoreRequest $request |
|
55
|
|
|
* |
|
56
|
|
|
* @return \App\Models\Quote |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function createQuote(QuoteStoreRequest $request): Quote |
|
59
|
|
|
{ |
|
60
|
1 |
|
$quote = $this->quoteRepository->store($request->all()); |
|
61
|
|
|
|
|
62
|
1 |
|
return $quote; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Update the Quote |
|
67
|
|
|
* |
|
68
|
|
|
* @param \App\Http\Requests\QuoteStoreRequest $request |
|
69
|
|
|
* @param int $quoteId |
|
70
|
|
|
* |
|
71
|
|
|
* @return \App\Models\Quote |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function updateQuote(QuoteStoreRequest $request, int $quoteId): Quote |
|
74
|
|
|
{ |
|
75
|
1 |
|
$quote = $this->quoteRepository->update($request->all(), $quoteId); |
|
76
|
|
|
|
|
77
|
1 |
|
return $quote; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Delete the quote from the database |
|
82
|
|
|
* |
|
83
|
|
|
* @param int $quoteId |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function deleteQuote(int $quoteId): void |
|
86
|
|
|
{ |
|
87
|
1 |
|
$this->quoteRepository->delete($quoteId); |
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Return the quote of the day. |
|
92
|
|
|
* And set the quote to shown, so it will not be show it again in the |
|
93
|
|
|
* next days until all the others has been shown. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $where - 'frontend' or 'backend' |
|
96
|
|
|
* |
|
97
|
2 |
|
* @return \App\Models\Quote |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function getQuoteOfTheDay(string $where): ?Quote |
|
100
|
|
|
{ |
|
101
|
2 |
|
$today = Carbon::today(); |
|
102
|
2 |
|
|
|
103
|
2 |
|
/*$quote = Quote::whereIn('show_where', [$where, 'both']) |
|
104
|
|
|
->where('is_published', true) |
|
105
|
|
|
->where("shown_{$where}_on", $today) //Quote already picked today |
|
106
|
2 |
|
->first();*/ |
|
107
|
|
|
|
|
108
|
1 |
|
$quote = Quote::whereIn('show_where', [$where, 'both']) |
|
109
|
1 |
|
->where('is_published', true) |
|
110
|
|
|
->orWhere(function ($query) use ($where, $today) { |
|
111
|
|
|
$query->where("shown_{$where}_on", $today) |
|
112
|
2 |
|
->where("shown_{$where}_on", null); |
|
113
|
2 |
|
})->first(); |
|
114
|
2 |
|
|
|
115
|
|
|
// Reset the quotes shown when all the quotes has already been shown |
|
116
|
2 |
|
if ($quote == null) { |
|
117
|
|
|
Quote::whereIn('show_where', [$where, 'both']) |
|
118
|
|
|
->update(["shown_{$where}_on" => null]); |
|
119
|
|
|
$quote = self::getQuoteOfTheDay($where); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$shownOnWhere = "shown_{$where}_on"; |
|
123
|
|
|
$quote->$shownOnWhere = $today; |
|
124
|
|
|
|
|
125
|
|
|
$quote->save(); |
|
126
|
|
|
|
|
127
|
|
|
return $quote; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|