Completed
Push — master ( 2250ba...f4130d )
by Sherif
02:44
created
src/Modules/V1/Core/Http/Controllers/BaseApiController.php 1 patch
Indentation   +209 added lines, -209 removed lines patch added patch discarded remove patch
@@ -6,215 +6,215 @@
 block discarded – undo
6 6
 
7 7
 class BaseApiController extends Controller
8 8
 {
9
-    /**
10
-     * The model implementation.
11
-     * 
12
-     * @var model
13
-     */
14
-    protected $model;
15
-
16
-    /**
17
-     * The config implementation.
18
-     * 
19
-     * @var config
20
-     */
21
-    protected $config;
22
-
23
-    public function __construct()
24
-    {
25
-        \Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0);
9
+	/**
10
+	 * The model implementation.
11
+	 * 
12
+	 * @var model
13
+	 */
14
+	protected $model;
15
+
16
+	/**
17
+	 * The config implementation.
18
+	 * 
19
+	 * @var config
20
+	 */
21
+	protected $config;
22
+
23
+	public function __construct()
24
+	{
25
+		\Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0);
26 26
         
27
-        $this->config              = \CoreConfig::getConfig();
28
-        $this->model               = property_exists($this, 'model') ? $this->model : false;
29
-        $this->validationRules     = property_exists($this, 'validationRules') ? $this->validationRules : false;
30
-        $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : [];
31
-        $this->skipLoginCheck      = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : [];
32
-        $this->relations           = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false;
33
-        $route                     = explode('@',\Route::currentRouteAction())[1];
34
-        $this->checkPermission($route);
35
-    }
36
-
37
-    /**
38
-     * Fetch all records with relations from model repository.
39
-     * 
40
-     * @return \Illuminate\Http\Response
41
-     */
42
-    public function index() 
43
-    {
44
-        if ($this->model)
45
-        {
46
-            $relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : [];
47
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200);
48
-        }
49
-    }
50
-
51
-    /**
52
-     * Fetch the single object with relations from model repository.
53
-     * 
54
-     * @param  integer $id
55
-     * @return \Illuminate\Http\Response
56
-     */
57
-    public function find($id) 
58
-    {
59
-        if ($this->model) 
60
-        {
61
-            $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : [];
62
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200);
63
-        }
64
-    }
65
-
66
-    /**
67
-     * Paginate all records with relations from model repository
68
-     * that matche the given query.
69
-     * 
70
-     * @param  string  $query
71
-     * @param  integer $perPage
72
-     * @param  string  $sortBy
73
-     * @param  boolean $desc
74
-     * @return \Illuminate\Http\Response
75
-     */
76
-    public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) 
77
-    {
78
-        if ($this->model) 
79
-        {
80
-            $relations = $this->relations && $this->relations['search'] ? $this->relations['search'] : [];
81
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200);
82
-        }
83
-    }
84
-
85
-    /**
86
-     * Fetch records from the storage based on the given
87
-     * condition.
88
-     * 
89
-     * @param  \Illuminate\Http\Request  $request
90
-     * @param  string  $sortBy
91
-     * @param  boolean $desc
92
-     * @return \Illuminate\Http\Response
93
-     */
94
-    public function findby(Request $request, $sortBy = 'created_at', $desc = 1) 
95
-    {
96
-        if ($this->model) 
97
-        {
98
-            $relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : [];
99
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200);
100
-        }
101
-    }
102
-
103
-    /**
104
-     * Fetch the first record from the storage based on the given
105
-     * condition.
106
-     * 
107
-     * @param  \Illuminate\Http\Request  $request
108
-     * @return \Illuminate\Http\Response
109
-     */
110
-    public function first(Request $request) 
111
-    {
112
-        if ($this->model) 
113
-        {
114
-            $relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : [];
115
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200);
116
-        }
117
-    }
118
-
119
-    /**
120
-     * Paginate all records with relations from model repository.
121
-     * 
122
-     * @param  integer $perPage
123
-     * @param  string  $sortBy
124
-     * @param  boolean $desc
125
-     * @return \Illuminate\Http\Response
126
-     */
127
-    public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) 
128
-    {
129
-        if ($this->model) 
130
-        {
131
-            $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : [];
132
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200);
133
-        }
134
-    }
135
-
136
-    /**
137
-     * Fetch all records with relations based on
138
-     * the given condition from storage in pages.
139
-     * 
140
-     * @param  \Illuminate\Http\Request  $request
141
-     * @param  integer $perPage
142
-     * @param  string  $sortBy
143
-     * @param  boolean $desc
144
-     * @return \Illuminate\Http\Response
145
-     */
146
-    public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) 
147
-    {
148
-        if ($this->model) 
149
-        {
150
-            $relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : [];
151
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200);
152
-        }
153
-    }
154
-
155
-    /**
156
-     * Save the given model to repository.
157
-     * 
158
-     * @param  \Illuminate\Http\Request  $request
159
-     * @return \Illuminate\Http\Response
160
-     */
161
-    public function save(Request $request) 
162
-    {
163
-        foreach ($this->validationRules as &$rule) 
164
-        {
165
-            if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) 
166
-            {
167
-                $rule .= ',deleted_at,NULL';
168
-            }
169
-
170
-            if ($request->has('id')) 
171
-            {
172
-                $rule = str_replace('{id}', $request->get('id'), $rule);
173
-            }
174
-            else
175
-            {
176
-                $rule = str_replace(',{id}', '', $rule);
177
-            }
178
-        }
27
+		$this->config              = \CoreConfig::getConfig();
28
+		$this->model               = property_exists($this, 'model') ? $this->model : false;
29
+		$this->validationRules     = property_exists($this, 'validationRules') ? $this->validationRules : false;
30
+		$this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : [];
31
+		$this->skipLoginCheck      = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : [];
32
+		$this->relations           = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false;
33
+		$route                     = explode('@',\Route::currentRouteAction())[1];
34
+		$this->checkPermission($route);
35
+	}
36
+
37
+	/**
38
+	 * Fetch all records with relations from model repository.
39
+	 * 
40
+	 * @return \Illuminate\Http\Response
41
+	 */
42
+	public function index() 
43
+	{
44
+		if ($this->model)
45
+		{
46
+			$relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : [];
47
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200);
48
+		}
49
+	}
50
+
51
+	/**
52
+	 * Fetch the single object with relations from model repository.
53
+	 * 
54
+	 * @param  integer $id
55
+	 * @return \Illuminate\Http\Response
56
+	 */
57
+	public function find($id) 
58
+	{
59
+		if ($this->model) 
60
+		{
61
+			$relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : [];
62
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200);
63
+		}
64
+	}
65
+
66
+	/**
67
+	 * Paginate all records with relations from model repository
68
+	 * that matche the given query.
69
+	 * 
70
+	 * @param  string  $query
71
+	 * @param  integer $perPage
72
+	 * @param  string  $sortBy
73
+	 * @param  boolean $desc
74
+	 * @return \Illuminate\Http\Response
75
+	 */
76
+	public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) 
77
+	{
78
+		if ($this->model) 
79
+		{
80
+			$relations = $this->relations && $this->relations['search'] ? $this->relations['search'] : [];
81
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200);
82
+		}
83
+	}
84
+
85
+	/**
86
+	 * Fetch records from the storage based on the given
87
+	 * condition.
88
+	 * 
89
+	 * @param  \Illuminate\Http\Request  $request
90
+	 * @param  string  $sortBy
91
+	 * @param  boolean $desc
92
+	 * @return \Illuminate\Http\Response
93
+	 */
94
+	public function findby(Request $request, $sortBy = 'created_at', $desc = 1) 
95
+	{
96
+		if ($this->model) 
97
+		{
98
+			$relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : [];
99
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200);
100
+		}
101
+	}
102
+
103
+	/**
104
+	 * Fetch the first record from the storage based on the given
105
+	 * condition.
106
+	 * 
107
+	 * @param  \Illuminate\Http\Request  $request
108
+	 * @return \Illuminate\Http\Response
109
+	 */
110
+	public function first(Request $request) 
111
+	{
112
+		if ($this->model) 
113
+		{
114
+			$relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : [];
115
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200);
116
+		}
117
+	}
118
+
119
+	/**
120
+	 * Paginate all records with relations from model repository.
121
+	 * 
122
+	 * @param  integer $perPage
123
+	 * @param  string  $sortBy
124
+	 * @param  boolean $desc
125
+	 * @return \Illuminate\Http\Response
126
+	 */
127
+	public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) 
128
+	{
129
+		if ($this->model) 
130
+		{
131
+			$relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : [];
132
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200);
133
+		}
134
+	}
135
+
136
+	/**
137
+	 * Fetch all records with relations based on
138
+	 * the given condition from storage in pages.
139
+	 * 
140
+	 * @param  \Illuminate\Http\Request  $request
141
+	 * @param  integer $perPage
142
+	 * @param  string  $sortBy
143
+	 * @param  boolean $desc
144
+	 * @return \Illuminate\Http\Response
145
+	 */
146
+	public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) 
147
+	{
148
+		if ($this->model) 
149
+		{
150
+			$relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : [];
151
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200);
152
+		}
153
+	}
154
+
155
+	/**
156
+	 * Save the given model to repository.
157
+	 * 
158
+	 * @param  \Illuminate\Http\Request  $request
159
+	 * @return \Illuminate\Http\Response
160
+	 */
161
+	public function save(Request $request) 
162
+	{
163
+		foreach ($this->validationRules as &$rule) 
164
+		{
165
+			if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) 
166
+			{
167
+				$rule .= ',deleted_at,NULL';
168
+			}
169
+
170
+			if ($request->has('id')) 
171
+			{
172
+				$rule = str_replace('{id}', $request->get('id'), $rule);
173
+			}
174
+			else
175
+			{
176
+				$rule = str_replace(',{id}', '', $rule);
177
+			}
178
+		}
179 179
         
180
-        $this->validate($request, $this->validationRules);
181
-
182
-        if ($this->model) 
183
-        {
184
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200);
185
-        }
186
-    }
187
-
188
-    /**
189
-     * Delete by the given id from model repository.
190
-     * 
191
-     * @param  integer  $id
192
-     * @return \Illuminate\Http\Response
193
-     */
194
-    public function delete($id) 
195
-    {
196
-        if ($this->model) 
197
-        {
198
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200);
199
-        }
200
-    }
201
-
202
-    /**
203
-     * Check if the logged in user can do the given permission.
204
-     * 
205
-     * @param  string $permission
206
-     * @return void
207
-     */
208
-    private function checkPermission($permission)
209
-    {
210
-        $permission = $permission !== 'index' ? $permission : 'list';
211
-        if ( ! in_array($permission, $this->skipLoginCheck)) 
212
-        {
213
-            \JWTAuth::parseToken()->authenticate();
214
-            if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model))
215
-            {
216
-                \ErrorHandler::noPermissions();
217
-            }
218
-        }
219
-    }
180
+		$this->validate($request, $this->validationRules);
181
+
182
+		if ($this->model) 
183
+		{
184
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200);
185
+		}
186
+	}
187
+
188
+	/**
189
+	 * Delete by the given id from model repository.
190
+	 * 
191
+	 * @param  integer  $id
192
+	 * @return \Illuminate\Http\Response
193
+	 */
194
+	public function delete($id) 
195
+	{
196
+		if ($this->model) 
197
+		{
198
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200);
199
+		}
200
+	}
201
+
202
+	/**
203
+	 * Check if the logged in user can do the given permission.
204
+	 * 
205
+	 * @param  string $permission
206
+	 * @return void
207
+	 */
208
+	private function checkPermission($permission)
209
+	{
210
+		$permission = $permission !== 'index' ? $permission : 'list';
211
+		if ( ! in_array($permission, $this->skipLoginCheck)) 
212
+		{
213
+			\JWTAuth::parseToken()->authenticate();
214
+			if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model))
215
+			{
216
+				\ErrorHandler::noPermissions();
217
+			}
218
+		}
219
+	}
220 220
 }
Please login to merge, or discard this patch.
src/Modules/V1/Core/Settings.php 1 patch
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -5,32 +5,32 @@
 block discarded – undo
5 5
 
6 6
 class Settings extends Model{
7 7
 
8
-    use SoftDeletes;
9
-    protected $table    = 'settings';
10
-    protected $dates    = ['created_at', 'updated_at', 'deleted_at'];
11
-    protected $hidden   = ['deleted_at'];
12
-    protected $guarded  = ['id', 'key'];
13
-    protected $fillable = ['name','value'];
14
-    public $searchable  = ['name', 'value', 'key'];
8
+	use SoftDeletes;
9
+	protected $table    = 'settings';
10
+	protected $dates    = ['created_at', 'updated_at', 'deleted_at'];
11
+	protected $hidden   = ['deleted_at'];
12
+	protected $guarded  = ['id', 'key'];
13
+	protected $fillable = ['name','value'];
14
+	public $searchable  = ['name', 'value', 'key'];
15 15
     
16
-    public function getCreatedAtAttribute($value)
17
-    {
18
-        return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString();
19
-    }
16
+	public function getCreatedAtAttribute($value)
17
+	{
18
+		return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString();
19
+	}
20 20
 
21
-    public function getUpdatedAtAttribute($value)
22
-    {
23
-        return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString();
24
-    }
21
+	public function getUpdatedAtAttribute($value)
22
+	{
23
+		return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString();
24
+	}
25 25
 
26
-    public function getDeletedAtAttribute($value)
27
-    {
28
-        return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString();
29
-    }
26
+	public function getDeletedAtAttribute($value)
27
+	{
28
+		return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString();
29
+	}
30 30
 
31
-    public static function boot()
32
-    {
33
-        parent::boot();
34
-        parent::observe(\App::make('App\Modules\V1\Core\ModelObservers\SettingsObserver'));
35
-    }
31
+	public static function boot()
32
+	{
33
+		parent::boot();
34
+		parent::observe(\App::make('App\Modules\V1\Core\ModelObservers\SettingsObserver'));
35
+	}
36 36
 }
Please login to merge, or discard this patch.