davide.cucurnia@vola.it
2024-02-01 555a537e2e5d9220e3777c30b185606823c817f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
 
namespace App\Traits;
 
use Exception;
use Illuminate\Database\Eloquent\Model;
 
trait Repository
{
    /**
     * Eloquent model
     */
    protected $modelInstance;
 
    /**
     * Get the record with the given id
     *
     * @param int $id
     * @param bool $trashed
     * @param array $relations
     * @return Model
     * @throws Exception
     */
    public function get(int $id, bool $trashed = false, array $relations = []) : Model
    {
        try {
            $modelInstance = ($trashed) ?
                $this->modelInstance->withTrashed()->findOrFail($id) : $this->modelInstance->findOrFail($id);
 
            if(!empty($relations)) {
                $modelInstance->load($relations);
            }
 
            return $modelInstance;
        } catch (Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * Update the record with the given id
     *
     * @param array $data
     * @param Model $instance
     * @return bool
     * @throws Exception
     */
    public function update(array $data, Model $instance)
    {
        try {
            return $instance->update($data);
        } catch (Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param array $params
     * @param bool $onlyTrashed
     * @param array $relations
     * @param bool $paginate
     * @param bool $withTrashed
     * @return mixed
     * @throws Exception
     */
    public function search(array $params = [], bool $onlyTrashed = false, array $relations = [], bool $paginate = false, $withTrashed = false, $groupBy = null)
    {
        try {
            $query = $this->modelInstance::searchWithParams($params);
 
            if($withTrashed) {
                $query->withTrashed();
            } elseif($onlyTrashed) {
                $query->onlyTrashed();
            }
 
            if(!empty($relations)) {
                $query->with($relations);
            }
 
            if(!empty($groupBy)) {
                $query->groupBy($groupBy);
            }
 
            return ($paginate) ? $query->paginate($this->modelInstance->perPage) : $query->get();
        } catch (Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param int $id
     * @return bool|null
     * @throws Exception
     */
    public function destroy(int $id)
    {
        try {
            return $this->get($id)->delete();
        } catch (Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param Model $instance
     * @return bool|null
     * @throws Exception
     */
    public function restore(Model $instance)
    {
        try {
            return $instance->restore();
        } catch (Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param array $params
     * @return mixed
     * @throws Exception
     */
    public function store(array $params)
    {
        try {
            return $this->modelInstance->create($params);
        } catch (Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @throws Exception
     */
    public function beginTransaction()
    {
        try {
            \DB::beginTransaction();
        } catch (Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @throws Exception
     */
    public function commitTransaction()
    {
        try {
            \DB::commit();
        } catch (Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @throws Exception
     */
    public function rollbackTransaction()
    {
        try {
            \DB::rollback();
        } catch (Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param Model $model
     * @param array $relations
     * @return Model
     * @throws Exception
     */
    public function load(Model $model, array $relations)
    {
        try {
            $model->load($relations);
 
            return $model;
        } catch (Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param array $attributes
     * @param array $params
     * @return mixed
     * @throws Exception
     */
    public function firstOrCreate(array $attributes, array $params)
    {
        try {
            return $this->modelInstance->firstOrCreate($attributes, $params);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param array $params
     * @return mixed
     * @throws Exception
     */
    public function deleteByCondition(array $params)
    {
        try {
            $queryBuilder = $this->modelInstance;
            foreach($params as $fields => $value) {
                $queryBuilder = $queryBuilder->where($fields, "=", $value);
            }
            return $queryBuilder->delete();
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param array $attributes
     * @param array $params
     * @param bool $withTrashed
     * @return mixed
     * @throws Exception
     */
    public function updateOrCreate(array $attributes, array $params, $withTrashed = false)
    {
        try {
            return ($withTrashed) ?  $this->modelInstance->withTrashed()->updateOrCreate($attributes, $params)
                : $this->modelInstance->updateOrCreate($attributes, $params);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param Model $model
     * @param $relation
     * @param $values
     * @return mixed
     * @throws Exception
     */
    public function syncRelations(Model $model, $relation, array $values)
    {
        try {
            return $model->{$relation}()->sync($values);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @throws Exception
     */
    public function truncate() {
        try {
            $this->modelInstance->whereNotNull('id')->delete();
        } catch (\Exception $e) {
            throw($e);
        }
    }
}