diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..84b7724 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Alauddin Afif Cassandra + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..662b8e2 --- /dev/null +++ b/README.md @@ -0,0 +1,216 @@ +# Mesosfer Library +This library for make to easy use mesosfer database, this package available to crud method. + +## Install +```composer require aacassandra/mesosfer-lib``` +or +Visit [composer](https://packagist.org/packages/aacassandra/mesosfer-lib) + +## Usage +This library available 5 options +- getObject +- getAllObject +- storeObject +- updateObject +- deleteObject + +### getObject +``` +use Mesosfer\MesosferLib; + +class ExamplesController extends Controller +{ + public function index(Request $request, $className, $id) + { + $options = [ + "include" => [ + 'floor' + ] + ]; + + $mesosfer = MesosferLib::getObject($className, $id, $options); + if ($mesosfer->status) { + $data = $mesosfer->output; + return view($this->indexView, compact('data')); + } else { + //If server down + $data = []; + return view($this->indexView, compact('data')); + } + } +} +``` + ### getAllObject + ``` +use Mesosfer\MesosferLib; + +class ExamplesController extends Controller +{ + public function index(Request $request, $className) + { + $options = [ + "include" => [ + 'floor' + ] + ]; + + $mesosfer = MesosferLib::getAllObject($className, $options); + if ($mesosfer->status) { + $data = $mesosfer->output; + } else { + //If server down + } + } +} + ``` + + ### storeObject + ``` +use Mesosfer\MesosferLib; + +class ExamplesController extends Controller +{ + public function create(Request $request, $className) + { + $data = [ + ['pointer','floor',$request->adFloor,'Floor'], + ['array','videoAds',$request->adVideo], + ['array','smallAds',$request->adSmall], + ['string','mediumAds',$request->adMedium,], + ['array','bigAds',$request->adBig] + ]; + + $mesosfer = MesosferLib::storeObject($className, $data); + if ($mesosfer->status) { + // If Success + } else { + // If failed + } + } +} + ``` + +### updateObject + ``` + use Mesosfer\MesosferLib; + +class ExamplesController extends Controller +{ + public function update(Request $request, $className, $id) + { + $data = [ + ['pointer','floor',$request->adFloor,'Floor'], + ['array','videoAds',$request->adVideo], + ['array','smallAds',$request->adSmall], + ['string','mediumAds',$request->adMedium,], + ['array','bigAds',$request->adBig] + ]; + + $mesosfer = MesosferLib::updateObject($className, $id, $data); + if ($mesosfer->status) { + // If Success + } else { + // If failed + } + } +} + ``` + +### deleteObject + ``` + use Mesosfer\MesosferLib; + +class ExamplesController extends Controller +{ + public function update(Request $request, $className, $id) + { + $mesosfer = MesosferLib::deleteObject($className, $id); + if ($mesosfer->status) { + // If Success + } else { + // If failed + } + } +} + ``` + +### Available Data Format +Will work only if used of storeObject and updateObject functions +##### data +- pointer +- string +- date +- number +- boolean +- array +- object +- image +- geopoint + +``` +$data = [ + ['pointer','floor',$request->adFloor,'Floor'], + ['array','videoAds',$request->adVideo], + ['array','smallAds',$request->adSmall], + ['string','mediumAds',$request->adMedium,], + ['array','bigAds',$request->adBig], + ['date','end',new DateTime($clientRequest->end)], + ['number','numberOfParticipants',$clientRequest->numberOfParticipants], + ['boolean','withVideoConference',$clientRequest->videoConference], + ['image','photo1',$request->file('photo0')], + ['geopoint','location',[-1.9924,23.1233]] +]; +``` + +### Available Options +Will work only if used of getObject and getAllObject functions +##### Include + ``` + $options = [ + "include" => [ + 'floor','floor.room' + ] + ]; +``` + +##### where +- equalTo +- equalToNumber +- notEqualTo +- notEqualToNumber +- containedIn +- notContainedIn +- greaterThan +- lessThan +- greaterThanOrEqualTo +- lessThanOrEqualTo +- greaterThanRelativeTime +- lessThanRelativeTime +- greaterThanOrEqualToRelativeTime +- lessThanOrEqualToRelativeTime + + ``` + $options1 = [ + "include" => [ + 'floor' + ], + "where" => [ + [ + "object" => "capacity", + "greaterThanOrEqualTo" => $clientRequest->numberOfParticipants + ], + [ + "object" => "withVideoConference", + "equalTo" => $clientRequest->videoConference + ], + [ + "object" => "allowGuests", + "equalTo" => $clientRequest->allowGuests + ] + ] + ]; +``` + +## License + +This project is licensed under the MIT License - see the [LICENSE.md](https://github.com/aacassandra/mesosfer-lib/blob/master/LICENSE) file for details diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a1f25c0 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "alauddincassandra/laraquent", + "description": "Save your time with Laraquent, for CRUD your laravel project with MySQL DB", + "type": "composer-plugin", + "license": "MIT", + "authors": [ + { + "name": "aacassandra", + "email": "alauddinafifcassandra@programmer.net" + } + ], + "autoload": { + "psr-4": { + "Laraquent\\": "src/" + } + } +} diff --git a/src/BeautyEloquent.php b/src/BeautyEloquent.php new file mode 100644 index 0000000..9aaed7c --- /dev/null +++ b/src/BeautyEloquent.php @@ -0,0 +1,236 @@ +API->Create('user', $data); + */ + public function Create($table, $data=[]) + { + $table = 'App\\'.$table; + $request = new $table; + foreach ($data as $key => $value) { + $columnName = $value[0]; + $request->$columnName = $value[1]; + } + $request->save(); + return BeautyEloquentTools::arr2Json([ + 'status' => true + ]); + } + + /** + * $users = $this->API->Read('user', [ + * 'where' => [ + * ['role','=','admin'], + * ], + * 'first' => true + * ]); + */ + public function Read($table='', $options=[]) + { + $table = 'App\\'.$table; + $request = new $table; + + $finalWhere = []; + $finalOrWhere = []; + + if (isset($options['id']) && $options['id']) { + array_push($finalWhere, ['id', '=', $options['id']]); + } + + if (isset($options['where']) && count($options['where'])) { + foreach ($options['where'] as $key => $value) { + if (isset($value[3]) && $value[3] === 'or') { + array_push($finalOrWhere, [$value[0],$value[1],$value[2]]); + } else { + array_push($finalWhere, [$value[0],$value[1],$value[2]]); + } + } + } + + foreach ($finalWhere as $key => $value) { + $request = $request->where($value[0], $value[1], $value[2]); + } + + if (isset($finalOrWhere) && count($finalOrWhere)) { + foreach ($finalOrWhere as $key => $value) { + $request = $request->orWhere($value[0], $value[1], $value[2]); + } + } + + if (isset($options['first']) && $options['first']) { + $request = $request->get()->first(); + $status = BeautyEloquentTools::arr2Json($request); + if ($status) { + $response = BeautyEloquentTools::arr2Json([ + 'status' => true, + 'output' => null + ]); + + $response->output = $request; + return $response; + } else { + return BeautyEloquentTools::arr2Json([ + 'status' => false + ]); + } + } else { + $request = $request->get(); + $status = BeautyEloquentTools::arr2Json($request); + if (isset($status) && count($status)) { + $response = BeautyEloquentTools::arr2Json([ + 'status' => true, + 'output' => null + ]); + + $response->output = $request; + return $response; + } else { + return BeautyEloquentTools::arr2Json([ + 'status' => false + ]); + } + } + } + + /** + * $users = $this->API->Update('user',[ + * ['name', 'Jhon'], + * ['age', 26] + * ] ,[ + * 'where' => [ + * ['role','=','user','or'] + * ], + * 'orwhere => [], + * 'id' => '' + * ]); + */ + public function Update($table='', $data=[], $options=[]) + { + $table = 'App\\'.$table; + $request = new $table; + + $finalWhere = []; + $finalOrWhere = []; + + if (isset($options['id']) && $options['id']) { + array_push($finalWhere, ['id', '=', $options['id']]); + } + + if (isset($options['where']) && count($options['where'])) { + foreach ($options['where'] as $key => $value) { + if (isset($value[3]) && $value[3] === 'or') { + array_push($finalOrWhere, [$value[0],$value[1],$value[2]]); + } else { + array_push($finalWhere, [$value[0],$value[1],$value[2]]); + } + } + } + + foreach ($finalWhere as $key => $value) { + $request = $request->where($value[0], $value[1], $value[2]); + } + + if (isset($finalOrWhere) && count($finalOrWhere)) { + foreach ($finalOrWhere as $key => $value) { + $request = $request->orWhere($value[0], $value[1], $value[2]); + } + } + + $status = $request->get(); + $status = BeautyEloquentTools::arr2Json($status); + if (isset($status) && count($status) && isset($data) && count($data)) { + $finalData = []; + foreach ($data as $key => $value) { + $finalData[$value[0]] = $value[1]; + } + $request = $request->update($finalData); + return BeautyEloquentTools::arr2Json([ + 'status' => true + ]); + } else { + return BeautyEloquentTools::arr2Json([ + 'status' => false + ]); + } + } + + /** + * $users = $this->API->Delete('user', [ + * 'where' => [ + * ['role','=','user','or'] + * ], + * 'orwhere => [], + * 'id' => '' + * ]); + */ + public function Delete($table='', $options=[]) + { + $table = 'App\\'.$table; + $request = new $table; + + $finalWhere = []; + $finalOrWhere = []; + + if (isset($options['id']) && $options['id']) { + $request = $request->where('id', $options['id']); + } + + if (isset($options['where']) && count($options['where'])) { + foreach ($options['where'] as $key => $value) { + if (isset($value[3]) && $value[3] === 'or') { + array_push($finalOrWhere, [$value[0],$value[1],$value[2]]); + } else { + array_push($finalWhere, [$value[0],$value[1],$value[2]]); + } + } + } + + foreach ($finalWhere as $key => $value) { + $request = $request->where($value[0], $value[1], $value[2]); + } + + if (isset($finalOrWhere) && count($finalOrWhere)) { + foreach ($finalOrWhere as $key => $value) { + $request = $request->orWhere($value[0], $value[1], $value[2]); + } + } + + $request = $request->delete(); + + //Check status + $status = new $table; + foreach ($finalWhere as $key => $value) { + $status = $status->where($value[0], $value[1], $value[2]); + } + + if (isset($finalOrWhere) && count($finalOrWhere)) { + foreach ($finalOrWhere as $key => $value) { + $status = $status->orWhere($value[0], $value[1], $value[2]); + } + } + + $status = $status->get(); + $status = BeautyEloquentTools::arr2Json($status); + if (isset($status) && count($status)) { + return BeautyEloquentTools::arr2Json([ + 'status' => false + ]); + } else { + return BeautyEloquentTools::arr2Json([ + 'status' => true + ]); + } + } +} diff --git a/src/BeautyEloquentTools.php b/src/BeautyEloquentTools.php new file mode 100644 index 0000000..c0c5384 --- /dev/null +++ b/src/BeautyEloquentTools.php @@ -0,0 +1,13 @@ +