33namespace DummyNamespace;
44
55use DB;
6+ use Exception;
7+ use App\Models\DummyModel;
8+ use App\Events\CRUDErrorOccurred;
9+ use App\Http\Resources\DummyResource;
10+ use App\Http\Resources\DummyResourceCollection;
611
712class DummyClass
813{
@@ -13,7 +18,7 @@ class DummyClass
1318 *
1419 * @return void
1520 */
16- public function __construct(Model $model)
21+ public function __construct(DummyModel $model)
1722 {
1823 $this->model = $model;
1924 }
@@ -23,35 +28,35 @@ class DummyClass
2328 *
2429 * @param string $pagination
2530 *
26- * @return ModelResourceCollection
31+ * @return DummyResourceCollection
2732 */
2833 public function get($pagination = 'm')
2934 {
3035 $data = $this->model->latest()
31- ->paginate(core_paginate ($pagination));
36+ ->paginate(pagination ($pagination));
3237
33- return new ModelResourceCollection ($data);
38+ return new DummyResourceCollection ($data);
3439 }
3540
3641 /**
3742 * find model record
3843 * @param $id
3944 *
40- * @return ModelResource
45+ * @return DummyResource
4146 */
4247 public function find($id)
4348 {
4449 $item = $this->model->findOrFail($id);
4550
46- return new ModelResource ($item);
51+ return new DummyResource ($item);
4752 }
4853
4954 /**
5055 * search records
5156 *
5257 * @param string $pagination
5358 *
54- * @return ModelResourceCollection
59+ * @return DummyResourceCollection
5560 */
5661 public function search($pagination = 'm')
5762 {
@@ -60,15 +65,15 @@ class DummyClass
6065 $data = $this->model
6166 ->where('name', 'LIKE', '%' . $term . '%')
6267 ->latest()
63- ->paginate(core_paginate ($pagination));
68+ ->paginate(pagination ($pagination));
6469
65- return new ModelResourceCollection ($data);
70+ return new DummyResourceCollection ($data);
6671 }
6772
6873 /**
6974 * create a new record
7075 *
71- * @return ModelResource
76+ * @return DummyResource
7277 */
7378 public function create()
7479 {
@@ -80,21 +85,23 @@ class DummyClass
8085 ]);
8186
8287 DB::commit();
83- } catch (\ Exception $e) {
88+ } catch (Exception $e) {
8489 DB::rollBack();
8590
86- debugOn() ? dde($e->getMessage()) : false;
91+ ddOnError($e);
92+
93+ event(new CRUDErrorOccurred($e->getMessage()));
8794
8895 return false;
8996 }
9097
91- return new ModelResource ($data);
98+ return new DummyResource ($data);
9299 }
93100
94101 /**
95102 * update a record
96103 *
97- * @return ModelResource
104+ * @return DummyResource
98105 */
99106 public function update($id)
100107 {
@@ -108,15 +115,17 @@ class DummyClass
108115 ]);
109116
110117 DB::commit();
111- } catch (\ Exception $e) {
118+ } catch (Exception $e) {
112119 DB::rollBack();
113120
114- debugOn() ? dde($e->getMessage()) : false;
121+ ddOnError($e);
122+
123+ event(new CRUDErrorOccurred($e->getMessage()));
115124
116125 return false;
117126 }
118127
119- return new ModelResource ($data);
128+ return new DummyResource ($data);
120129 }
121130
122131 /**
@@ -128,6 +137,16 @@ class DummyClass
128137 */
129138 public function delete($id)
130139 {
131- return $this->model->findOrFail($id)->delete();
140+ try {
141+ $resp = $this->model->findOrFail($id)->delete();
142+ } catch (Exception $e) {
143+ event(new CRUDErrorOccurred($e->getMessage()));
144+
145+ return false;
146+ }
147+
148+ // event - alert
149+
150+ return $resp;
132151 }
133152}
0 commit comments