diff --git a/ci4/app/Config/Filters.php b/ci4/app/Config/Filters.php index f17d1127..178fffba 100755 --- a/ci4/app/Config/Filters.php +++ b/ci4/app/Config/Filters.php @@ -26,6 +26,7 @@ class Filters extends BaseConfig 'honeypot' => Honeypot::class, 'invalidchars' => InvalidChars::class, 'secureheaders' => SecureHeaders::class, + 'cors' => \App\Filters\Cors::class ]; /** diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 74a9f57d..1cf1170e 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -945,14 +945,31 @@ $routes->group('produccion', ['namespace' => 'App\Controllers\Produccion'], func */ $routes->post('auth/jwt', '\App\Controllers\Sistema\AuthAPIController::jwtLogin'); -$routes->group('api', ['filter' => 'jwt'], static function ($routes) { - $routes->get('test', 'Test::echo'); - // ... -}); +$routes->group( + 'api', + [ + 'namespace' => 'App\Controllers\API', + 'filter' => 'jwt' + ], + static function ($routes) { + + $routes->resource('items', [ + 'controller' => 'ItemsController', + 'filter' => 'cors' + ]); + + $routes->options('items', static function () { }); + $routes->options('items/(:any)', static function () { }); + + + + // ... + } +); /* * -------------------------------------------------------------------- - * Translation + * Translation * -------------------------------------------------------------------- */ $routes->group('translate', ['namespace' => 'App\Controllers'], function ($routes) { diff --git a/ci4/app/Controllers/API/ItemsController.php b/ci4/app/Controllers/API/ItemsController.php new file mode 100644 index 00000000..b4ccbfe8 --- /dev/null +++ b/ci4/app/Controllers/API/ItemsController.php @@ -0,0 +1,103 @@ +findAll(); + return $this->respond($data); + } + + public function show($id = null) + { + $model = new ItemModel(); + $data = $model->find(['id' => $id]); + if (!$data) + return $this->failNotFound('No Data Found'); + return $this->respond($data[0]); + } + + public function create() + { + helper(['form']); + $rules = [ + 'title' => 'required', + 'price' => 'required' + ]; + $data = [ + 'title' => $this->request->getVar('title'), + 'price' => $this->request->getVar('price') + ]; + + if (!$this->validate($rules)) + return $this->fail($this->validator->getErrors()); + $model = new ItemModel(); + $model->save($data); + $response = [ + 'status' => 201, + 'error' => null, + 'messages' => [ + 'success' => 'Data Inserted' + ] + ]; + return $this->respondCreated($response); + } + + public function update($id = null) + { + helper(['form']); + $rules = [ + 'title' => 'required', + 'price' => 'required' + ]; + $data = [ + 'title' => $this->request->getVar('title'), + 'price' => $this->request->getVar('price') + ]; + + if (!$this->validate($rules)) + return $this->fail($this->validator->getErrors()); + $model = new ItemModel(); + $find = $model->find(['id' => $id]); + if (!$find) + return $this->failNotFound('No Data Found'); + $model->update($id, $data); + + $response = [ + 'status' => 200, + 'error' => null, + 'messages' => [ + 'success' => 'Data updated' + ] + ]; + return $this->respond($response); + } + + public function delete($id = null) + { + $model = new ItemModel(); + $find = $model->find(['id' => $id]); + if (!$find) + return $this->failNotFound('No Data Found'); + $model->delete($id); + + $response = [ + 'status' => 200, + 'error' => null, + 'messages' => [ + 'success' => 'Data deleted' + ] + ]; + return $this->respond($response); + } +} diff --git a/ci4/app/Filters/Cors.php b/ci4/app/Filters/Cors.php new file mode 100644 index 00000000..19c78a38 --- /dev/null +++ b/ci4/app/Filters/Cors.php @@ -0,0 +1,56 @@ +