|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace App\Src\UseCases\Infra\Sql; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use App\Src\UseCases\Domain\Context\Dto\FollowerDto; |
|
8
|
|
|
use App\Src\UseCases\Domain\Context\Dto\PractiseVo; |
|
9
|
|
|
use App\Src\UseCases\Domain\Context\Model\CanInteract; |
|
10
|
|
|
use App\Src\UseCases\Domain\Context\Model\Interaction; |
|
11
|
|
|
use App\Src\UseCases\Domain\Ports\InteractionRepository; |
|
12
|
|
|
use App\Src\UseCases\Infra\Sql\Model\CharacteristicsModel; |
|
13
|
|
|
use App\Src\UseCases\Infra\Sql\Model\InteractionModel; |
|
14
|
|
|
use App\Src\UseCases\Infra\Sql\Model\PageModel; |
|
15
|
|
|
use App\User; |
|
16
|
|
|
use Illuminate\Contracts\Pagination\Paginator; |
|
17
|
|
|
|
|
18
|
|
|
class InteractionPageRepositorySql implements InteractionRepository |
|
19
|
|
|
{ |
|
20
|
|
|
public function save(CanInteract $canInteract, Interaction $interaction) |
|
21
|
|
|
{ |
|
22
|
|
|
$user = User::query()->where('uuid', $canInteract->identifier())->first(); |
|
23
|
|
|
|
|
24
|
|
|
$interactionModel = InteractionModel::query() |
|
25
|
|
|
->where($canInteract->key(), $user->id ?? $canInteract->identifier()) |
|
26
|
|
|
->where('page_id', $interaction->pageId()) |
|
27
|
|
|
->first(); |
|
28
|
|
|
|
|
29
|
|
|
if($interactionModel === null) { |
|
30
|
|
|
$interactionModel = new InteractionModel(); |
|
31
|
|
|
$interactionModel->{$canInteract->key()} = $user->id ?? $canInteract->identifier(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$interactionModel->fill($interaction->toArray()); |
|
35
|
|
|
$interactionModel->start_done_at = $interaction->toArray()['value']['start_at'] ?? null; |
|
36
|
|
|
$interactionModel->save(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getByInteractUser(CanInteract $canInteract, int $pageId): ?Interaction |
|
40
|
|
|
{ |
|
41
|
|
|
if($canInteract->key() == 'user_id') { |
|
42
|
|
|
$user = User::query()->where('uuid', $canInteract->identifier())->first(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$interactionModel = InteractionModel::query() |
|
46
|
|
|
->where($canInteract->key(), $user->id ?? $canInteract->identifier()) |
|
|
|
|
|
|
47
|
|
|
->where('page_id', $pageId) |
|
48
|
|
|
->first(); |
|
49
|
|
|
|
|
50
|
|
|
if(!isset($interactionModel)){ |
|
51
|
|
|
return null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$value = $interactionModel->value ?? []; |
|
55
|
|
|
return new Interaction($pageId, $interactionModel->follow, $interactionModel->applause, $interactionModel->done, $value); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function transfer(CanInteract $anonymous, CanInteract $registered) |
|
59
|
|
|
{ |
|
60
|
|
|
$user = User::query()->where('uuid', $registered->identifier())->first(); |
|
61
|
|
|
|
|
62
|
|
|
InteractionModel::query()->where($anonymous->key(), $anonymous->identifier()) |
|
63
|
|
|
->update([ |
|
64
|
|
|
$anonymous->key() => null, |
|
65
|
|
|
$registered->key() => $user->id |
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getCountInteractionsOnPage(int $pageId):array |
|
70
|
|
|
{ |
|
71
|
|
|
$follow = InteractionModel::query()->where('page_id', $pageId)->where('follow', true)->count(); |
|
72
|
|
|
$done = InteractionModel::query()->where('page_id', $pageId)->where('done', true)->count(); |
|
73
|
|
|
$applause = InteractionModel::query()->where('page_id', $pageId)->where('applause', true)->count(); |
|
74
|
|
|
|
|
75
|
|
|
return ['follow' => $follow, 'done' => $done, 'applause' => $applause]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getInteractionsByUser(string $userId): array |
|
79
|
|
|
{ |
|
80
|
|
|
$user = User::query()->where('uuid', $userId)->first(); |
|
81
|
|
|
$interactionsModel = InteractionModel::query() |
|
82
|
|
|
->where('user_id', $user->id) |
|
83
|
|
|
->orderBy('updated_at', 'desc') |
|
84
|
|
|
->get(); |
|
85
|
|
|
|
|
86
|
|
|
foreach($interactionsModel as $interaction) { |
|
87
|
|
|
$page = PageModel::query()->where('page_id', $interaction->page_id)->first(); |
|
88
|
|
|
if(!isset($page)){ |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
$applause = InteractionModel::query()->where('page_id', $interaction->page_id)->where('applause', true)->count(); |
|
92
|
|
|
if ($interaction->follow) { |
|
93
|
|
|
$interactions['follow'][] = array_merge($page->toArray(), ['applause' => $applause]); |
|
94
|
|
|
} |
|
95
|
|
|
if ($interaction->applause) { |
|
96
|
|
|
$interactions['applause'][] = array_merge($page->toArray(), ['applause' => $applause]); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
return $interactions ?? []; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
public function getDoneByUser(string $userId): array |
|
104
|
|
|
{ |
|
105
|
|
|
$user = User::query()->where('uuid', $userId)->first(); |
|
106
|
|
|
$records = InteractionModel::query() |
|
107
|
|
|
->with('page') |
|
108
|
|
|
->where('user_id', $user->id) |
|
109
|
|
|
->where('done', true) |
|
110
|
|
|
->get(); |
|
111
|
|
|
foreach ($records as $record){ |
|
112
|
|
|
$practises[] = new PractiseVo( |
|
113
|
|
|
$record->page_id, |
|
114
|
|
|
$record->page->title ?? '', |
|
115
|
|
|
$record->start_done_at ?? null |
|
|
|
|
|
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
return $practises ?? []; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param int $pageId |
|
124
|
|
|
* @param string $type |
|
125
|
|
|
* @param string|null $cp |
|
126
|
|
|
* @param string|null $characteristicId |
|
127
|
|
|
* @param string|null $characteristicIdCroppingSystem |
|
128
|
|
|
* @return Paginator |
|
129
|
|
|
* |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getFollowersPage(int $pageId, string $type = 'follow', ?string $departmentNumber = null, ?string $characteristicId = null, ?string $characteristicIdCroppingSystem = null): Paginator |
|
132
|
|
|
{ |
|
133
|
|
|
return InteractionModel::query() |
|
134
|
|
|
->with('user.context') |
|
135
|
|
|
->where(function ($query) use ($type){ |
|
136
|
|
|
$query->when($type === 'follow', function ($query) { |
|
137
|
|
|
$query->where('follow', true); |
|
138
|
|
|
$query->orWhere('done', true); |
|
139
|
|
|
}) |
|
140
|
|
|
->when($type === 'do', function ($query) { |
|
141
|
|
|
$query->where('done', true); |
|
142
|
|
|
}); |
|
143
|
|
|
}) |
|
144
|
|
|
->when($characteristicId !== null, function ($query) use($characteristicId) { |
|
145
|
|
|
$characteristic = CharacteristicsModel::query()->where('uuid', $characteristicId)->first(); |
|
146
|
|
|
if(!isset($characteristic)){ |
|
147
|
|
|
return; |
|
148
|
|
|
} |
|
149
|
|
|
$query->whereRaw( |
|
150
|
|
|
'exists(SELECT * FROM user_characteristics where characteristic_id = ? AND user_characteristics.user_id = interactions.user_id)', |
|
151
|
|
|
$characteristic->id |
|
152
|
|
|
); |
|
153
|
|
|
}) |
|
154
|
|
|
->when($characteristicIdCroppingSystem !== null, function ($query) use($characteristicIdCroppingSystem) { |
|
155
|
|
|
$characteristic = CharacteristicsModel::query()->where('uuid', $characteristicIdCroppingSystem)->first(); |
|
156
|
|
|
if(!isset($characteristic)){ |
|
157
|
|
|
return; |
|
158
|
|
|
} |
|
159
|
|
|
$query->whereRaw( |
|
160
|
|
|
'exists(SELECT * FROM user_characteristics where characteristic_id = ? AND user_characteristics.user_id = interactions.user_id)', |
|
161
|
|
|
$characteristic->id |
|
162
|
|
|
); |
|
163
|
|
|
}) |
|
164
|
|
|
->when($departmentNumber !== null, function ($query) use($departmentNumber) { |
|
165
|
|
|
$query |
|
166
|
|
|
->join('users', 'users.id', 'interactions.user_id') |
|
167
|
|
|
->join('contexts', 'users.context_id', 'contexts.id') |
|
168
|
|
|
->where('contexts.department_number', $departmentNumber); |
|
169
|
|
|
}) |
|
170
|
|
|
->where('page_id', $pageId) |
|
171
|
|
|
->whereNotNull('interactions.user_id') |
|
172
|
|
|
->orderBy('interactions.updated_at', 'desc') |
|
173
|
|
|
->paginate() |
|
174
|
|
|
->through(function ($item){ |
|
175
|
|
|
return new FollowerDto($item->user->toDto(), $item->user->context->toDto(), $item->toDto()); |
|
176
|
|
|
}) |
|
177
|
|
|
; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|