|
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 HRis\Api\Eloquent\Employee; |
|
15
|
|
|
use Illuminate\Routing\Controller; |
|
16
|
|
|
use Swagger\Annotations as SWG; |
|
17
|
|
|
use Tymon\JWTAuth\Facades\JWTAuth; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @SWG\Swagger( |
|
21
|
|
|
* schemes={"https", "http"}, |
|
22
|
|
|
* host="api.hris.dev", |
|
23
|
|
|
* basePath="/api", |
|
24
|
|
|
* @SWG\Info( |
|
25
|
|
|
* version="1.0.0", |
|
26
|
|
|
* title="HRis", |
|
27
|
|
|
* description="Human Resource and Payroll System", |
|
28
|
|
|
* @SWG\Contact( |
|
29
|
|
|
* email="[email protected]" |
|
30
|
|
|
* ) |
|
31
|
|
|
* ), |
|
32
|
|
|
* @SWG\ExternalDocumentation( |
|
33
|
|
|
* description="Fork HRis on GitHub", |
|
34
|
|
|
* url="https://github.com/bkintanar/HRis" |
|
35
|
|
|
* ) |
|
36
|
|
|
* ) |
|
37
|
|
|
*/ |
|
38
|
|
|
class BaseController extends Controller |
|
39
|
|
|
{ |
|
40
|
|
|
use Helpers; |
|
41
|
|
|
|
|
42
|
|
|
public $data = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return \Dingo\Api\Http\Response |
|
46
|
|
|
*/ |
|
47
|
|
|
public function apiInformation() |
|
48
|
|
|
{ |
|
49
|
|
|
$data = [ |
|
50
|
|
|
'schema' => ['http', 'https'], |
|
51
|
|
|
'host' => 'api.hris.dev', |
|
52
|
|
|
'version' => '1.0.0', |
|
53
|
|
|
'title' => 'HRis', |
|
54
|
|
|
'description' => 'Human Resource and Payroll System', |
|
55
|
|
|
'contact' => [ |
|
56
|
|
|
'email' => '[email protected]', |
|
57
|
|
|
], |
|
58
|
|
|
'documentation' => [ |
|
59
|
|
|
'description' => 'Fork HRis on GitHub', |
|
60
|
|
|
'url' => 'https://github.com/bkintanar/HRis', |
|
61
|
|
|
], |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
return $this->responseAPI(200, SUCCESS_RETRIEVE_MESSAGE, ['api' => $data]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return Employee |
|
69
|
|
|
*/ |
|
70
|
2 |
|
protected function loggedEmployee() |
|
71
|
|
|
{ |
|
72
|
2 |
|
$user = JWTAuth::parseToken()->authenticate(); |
|
73
|
|
|
|
|
74
|
2 |
|
return $user->employee; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Standard saving of model. |
|
79
|
|
|
* |
|
80
|
|
|
* @param $request |
|
81
|
|
|
* @param $model |
|
82
|
|
|
* @param $name |
|
83
|
|
|
* |
|
84
|
|
|
* @return \Dingo\Api\Http\Response |
|
85
|
|
|
* |
|
86
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
87
|
|
|
*/ |
|
88
|
76 |
|
protected function storeModel($request, $model, $name) |
|
89
|
|
|
{ |
|
90
|
|
|
try { |
|
91
|
76 |
|
$item = $model->create($request->all()); |
|
92
|
76 |
|
} catch (Exception $e) { |
|
93
|
6 |
|
return $this->responseAPI(422, UNABLE_ADD_MESSAGE); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
70 |
|
return $this->responseAPI(201, SUCCESS_ADD_MESSAGE, [$name => $item]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Standard response for any request. |
|
101
|
|
|
* |
|
102
|
|
|
* @param $status_code |
|
103
|
|
|
* @param string $message |
|
104
|
|
|
* @param array $data |
|
105
|
|
|
* |
|
106
|
|
|
* @return \Dingo\Api\Http\Response |
|
107
|
|
|
* |
|
108
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
109
|
|
|
*/ |
|
110
|
192 |
|
protected function responseAPI($status_code, $message = '', $data = []) |
|
111
|
|
|
{ |
|
112
|
192 |
|
$response = []; |
|
113
|
192 |
|
$response['message'] = $message; |
|
114
|
192 |
|
$response['status_code'] = $status_code; |
|
115
|
|
|
|
|
116
|
192 |
|
if (!empty($data)) { |
|
117
|
190 |
|
$response = array_merge($response, $data); |
|
118
|
190 |
|
} |
|
119
|
|
|
|
|
120
|
192 |
|
return $this->response->withArray($response)->statusCode($status_code); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Standard updating of model. |
|
125
|
|
|
* |
|
126
|
|
|
* @param $request |
|
127
|
|
|
* @param $model |
|
128
|
|
|
* @param array $only |
|
129
|
|
|
* |
|
130
|
|
|
* @return \Dingo\Api\Http\Response |
|
131
|
|
|
* |
|
132
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
133
|
|
|
*/ |
|
134
|
32 |
|
protected function updateModel($request, $model, $only = []) |
|
135
|
|
|
{ |
|
136
|
32 |
|
$model_id = $request->get('id'); |
|
137
|
|
|
|
|
138
|
32 |
|
$item = $model->whereId($model_id)->first(); |
|
139
|
|
|
|
|
140
|
32 |
|
if (!$item) { |
|
141
|
16 |
|
return $this->responseAPI(404, UNABLE_RETRIEVE_MESSAGE); |
|
142
|
|
|
} |
|
143
|
|
|
try { |
|
144
|
16 |
|
if (!empty($only)) { |
|
145
|
4 |
|
$item->update($request->only($only)); |
|
146
|
4 |
|
} else { |
|
147
|
12 |
|
$item->update($request->all()); |
|
148
|
|
|
} |
|
149
|
16 |
|
} catch (Exception $e) { |
|
150
|
|
|
return $this->responseAPI(422, UNABLE_UPDATE_MESSAGE); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
16 |
|
return $this->responseAPI(200, SUCCESS_UPDATE_MESSAGE); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Standard deleting of model. |
|
158
|
|
|
* |
|
159
|
|
|
* @param $item |
|
160
|
|
|
* @param $model |
|
161
|
|
|
* |
|
162
|
|
|
* @return \Dingo\Api\Http\Response |
|
163
|
|
|
* |
|
164
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
165
|
|
|
*/ |
|
166
|
20 |
|
protected function destroyModel($item, $model) |
|
167
|
|
|
{ |
|
168
|
20 |
|
$response_code = $model->whereId($item->id)->delete(); |
|
169
|
|
|
|
|
170
|
20 |
|
if (!$response_code) { |
|
171
|
|
|
return $this->responseAPI(422, UNABLE_DELETE_MESSAGE); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
20 |
|
return $this->responseAPI(200, SUCCESS_DELETE_MESSAGE); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|