|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the HRis Software package. |
|
5
|
|
|
* |
|
6
|
|
|
* HRis - Human Resource and Payroll System |
|
7
|
|
|
* |
|
8
|
|
|
* @link http://github.com/HB-Co/HRis |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace HRis\Api\Controllers; |
|
11
|
|
|
|
|
12
|
|
|
use Dingo\Api\Routing\Helpers; |
|
13
|
|
|
use Exception; |
|
14
|
|
|
use Illuminate\Routing\Controller; |
|
15
|
|
|
use Swagger\Annotations as SWG; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @SWG\Swagger( |
|
19
|
|
|
* schemes={"https", "http"}, |
|
20
|
|
|
* host="api.hris.dev", |
|
21
|
|
|
* basePath="/api", |
|
22
|
|
|
* @SWG\Info( |
|
23
|
|
|
* version="1.0.0", |
|
24
|
|
|
* title="HRis", |
|
25
|
|
|
* description="Human Resource and Payroll System", |
|
26
|
|
|
* @SWG\Contact( |
|
27
|
|
|
* email="[email protected]" |
|
28
|
|
|
* ) |
|
29
|
|
|
* ), |
|
30
|
|
|
* @SWG\ExternalDocumentation( |
|
31
|
|
|
* description="Fork HRis on GitHub", |
|
32
|
|
|
* url="https://github.com/bkintanar/HRis" |
|
33
|
|
|
* ) |
|
34
|
|
|
* ) |
|
35
|
|
|
*/ |
|
36
|
|
|
class BaseController extends Controller |
|
37
|
|
|
{ |
|
38
|
|
|
use Helpers; |
|
39
|
|
|
|
|
40
|
|
|
public $data = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Standard response for any request. |
|
44
|
|
|
* |
|
45
|
|
|
* @param $status_code |
|
46
|
|
|
* @param string $message |
|
47
|
|
|
* @param array $data |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Dingo\Api\Http\Response |
|
50
|
|
|
* |
|
51
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
52
|
|
|
*/ |
|
53
|
188 |
|
protected function responseAPI($status_code, $message = '', $data = []) |
|
54
|
|
|
{ |
|
55
|
188 |
|
$response = []; |
|
56
|
188 |
|
$response['message'] = $message; |
|
57
|
188 |
|
$response['status_code'] = $status_code; |
|
58
|
|
|
|
|
59
|
188 |
|
if (!empty($data)) { |
|
60
|
186 |
|
$response = array_merge($response, $data); |
|
61
|
186 |
|
} |
|
62
|
|
|
|
|
63
|
188 |
|
return $this->response->withArray($response)->statusCode($status_code); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Standard saving of model. |
|
68
|
|
|
* |
|
69
|
|
|
* @param $request |
|
70
|
|
|
* @param $model |
|
71
|
|
|
* @param $name |
|
72
|
|
|
* |
|
73
|
|
|
* @return \Dingo\Api\Http\Response |
|
74
|
|
|
* |
|
75
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
76
|
|
|
*/ |
|
77
|
76 |
|
protected function storeModel($request, $model, $name) |
|
78
|
|
|
{ |
|
79
|
|
|
try { |
|
80
|
76 |
|
$item = $model->create($request->all()); |
|
81
|
76 |
|
} catch (Exception $e) { |
|
82
|
6 |
|
return $this->responseAPI(422, UNABLE_ADD_MESSAGE); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
70 |
|
return $this->responseAPI(201, SUCCESS_ADD_MESSAGE, [$name => $item]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Standard updating of model. |
|
90
|
|
|
* |
|
91
|
|
|
* @param $request |
|
92
|
|
|
* @param $model |
|
93
|
|
|
* @param array $only |
|
94
|
|
|
* |
|
95
|
|
|
* @return \Dingo\Api\Http\Response |
|
96
|
|
|
* |
|
97
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
98
|
|
|
*/ |
|
99
|
32 |
|
protected function updateModel($request, $model, $only = []) |
|
100
|
|
|
{ |
|
101
|
32 |
|
$model_id = $request->get('id'); |
|
102
|
|
|
|
|
103
|
32 |
|
$item = $model->whereId($model_id)->first(); |
|
104
|
|
|
|
|
105
|
32 |
|
if (!$item) { |
|
106
|
16 |
|
return $this->responseAPI(404, UNABLE_RETRIEVE_MESSAGE); |
|
107
|
|
|
} |
|
108
|
|
|
try { |
|
109
|
16 |
|
if (!empty($only)) { |
|
110
|
4 |
|
$item->update($request->only($only)); |
|
111
|
4 |
|
} else { |
|
112
|
12 |
|
$item->update($request->all()); |
|
113
|
|
|
} |
|
114
|
16 |
|
} catch (Exception $e) { |
|
115
|
|
|
return $this->responseAPI(422, UNABLE_UPDATE_MESSAGE); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
16 |
|
return $this->responseAPI(200, SUCCESS_UPDATE_MESSAGE); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Standard deleting of model. |
|
123
|
|
|
* |
|
124
|
|
|
* @param $item |
|
125
|
|
|
* @param $model |
|
126
|
|
|
* |
|
127
|
|
|
* @return \Dingo\Api\Http\Response |
|
128
|
|
|
* |
|
129
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
130
|
|
|
*/ |
|
131
|
20 |
|
protected function destroyModel($item, $model) |
|
132
|
|
|
{ |
|
133
|
20 |
|
$response_code = $model->whereId($item->id)->delete(); |
|
134
|
|
|
|
|
135
|
20 |
|
if (!$response_code) { |
|
136
|
|
|
return $this->responseAPI(422, UNABLE_DELETE_MESSAGE); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
20 |
|
return $this->responseAPI(200, SUCCESS_DELETE_MESSAGE); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|