Filippo Bertilotti
2024-07-26 2bfce618a9d3b4cd86be2acd551a56aa45800efd
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
<?php
 
namespace App\Traits;
 
use Illuminate\Database\Eloquent\Model;
 
trait Service
{
    /**
     * Containing our repository to make all our database calls to
     */
    protected $repository;
 
    /**
     * Get the record with the given id
     *
     * @param int $id
     * @param bool $trashed
     * @param array $relations
     * @return \App\Models\User
     * @throws \Exception
     */
    public function get(int $id, $trashed = false, array $relations = [])
    {
        try {
            return $this->repository->get($id, $trashed, $relations);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * Update the record with the given id
     *
     * @param array $data
     * @param Model $modelInstance
     * @return mixed
     * @throws \Exception
     */
    public function update(array $data, Model $modelInstance)
    {
        try {
            return $this->repository->update($data, $modelInstance);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param array $params
     * @param bool $onlyTrashed
     * @param array $relations
     * @param bool $paginate
     * @param false $withTrashed
     * @param null $groupBy
     * @return mixed
     * @throws \Exception
     */
    public function search(array $params = [], bool $onlyTrashed = false, array $relations = [], bool $paginate = false, $withTrashed = false, $groupBy = null)
    {
        try {
            return $this->repository->search($params, $onlyTrashed, $relations, $paginate, $withTrashed, $groupBy);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param int $id
     * @return mixed
     * @throws \Exception
     */
    public function destroy(int $id)
    {
        try {
            return $this->repository->destroy($id);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param int $id
     * @return mixed
     * @throws \Exception
     */
    public function restore(int $id)
    {
        try {
            $item = $this->get($id, true);
 
            return $this->repository->restore($item);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param array $params
     * @return mixed
     * @throws \Exception
     */
    public function store(array $params)
    {
        try {
            return $this->repository->store($params);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param Model $model
     * @param array $relations
     * @return mixed
     * @throws \Exception
     */
    public function load(Model $model, array $relations)
    {
        try {
            return $this->repository->load($model, $relations);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @return mixed
     * @throws \Exception
     */
    public function beginTransaction()
    {
        try {
            return $this->repository->beginTransaction();
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @return mixed
     * @throws \Exception
     */
    public function commitTransaction()
    {
        try {
            return $this->repository->commitTransaction();
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @return mixed
     * @throws \Exception
     */
    public function rollbackTransaction()
    {
        try {
            return $this->repository->rollbackTransaction();
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param $attributes
     * @param $params
     * @return mixed
     * @throws \Exception
     */
    public function firstOrCreate($attributes, $params)
    {
        try {
            return $this->repository->firstOrCreate($attributes, $params);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param array $params
     * @return mixed
     * @throws \Exception
     */
    public function deleteByCondition(array $params)
    {
        try {
            return $this->repository->deleteByCondition($params);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param $attributes
     * @param $params
     * @param bool $withTrashed
     * @return mixed
     * @throws \Exception
     */
    public function updateOrCreate($attributes, $params, $withTrashed = false)
    {
        try {
            return $this->repository->updateOrCreate($attributes, $params, $withTrashed);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @param Model $model
     * @param $relation
     * @param array $values
     * @return mixed
     * @throws \Exception
     */
    public function syncRelations(Model $model, $relation, array $values)
    {
        try {
            return $this->repository->syncRelations($model, $relation, $values);
        } catch (\Exception $e) {
            throw ($e);
        }
    }
 
    /**
     * @return mixed
     */
    public function truncate()
    {
        return $this->repository->truncate();
    }
}