diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/api-testing-automation.iml b/.idea/api-testing-automation.iml new file mode 100644 index 0000000..9eddfbf --- /dev/null +++ b/.idea/api-testing-automation.iml @@ -0,0 +1,122 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d16c916 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..accdbd2 --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,121 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/phpunit.xml b/.idea/phpunit.xml new file mode 100644 index 0000000..4f8104c --- /dev/null +++ b/.idea/phpunit.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/Domain/UserManagements/Application/UserApplication.php b/app/Domain/UserManagements/Application/UserApplication.php new file mode 100644 index 0000000..d89258f --- /dev/null +++ b/app/Domain/UserManagements/Application/UserApplication.php @@ -0,0 +1,22 @@ +userRepository = $userRepository; + } + + public function register(UserModel $model) + { + $model->avatar = "https://ui-avatars.com/api/?name=" . urlencode($model->name); + $model->password = Hash::make($model->password); + return $this->userRepository->register($model); + } +} diff --git a/app/Domain/UserManagements/Model/UserModel.php b/app/Domain/UserManagements/Model/UserModel.php new file mode 100644 index 0000000..7ed48bb --- /dev/null +++ b/app/Domain/UserManagements/Model/UserModel.php @@ -0,0 +1,16 @@ +model = $model; + } + + public function register(UserModel $model) + { + $this->model->name = $model->name; + $this->model->email = $model->email; + $this->model->password = $model->password; + $this->model->avatar = $model->avatar; + $this->model->save(); + + return $this->model->only(['id', 'name', 'email', 'created_at']); + } +} diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 7e40d73..ce1122d 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -3,6 +3,8 @@ namespace App\Exceptions; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; +use Illuminate\Validation\ValidationException; +use Throwable; class Handler extends ExceptionHandler { @@ -34,4 +36,15 @@ public function register() { // } + + public function render($request, Throwable $e) + { + if ($e instanceof ValidationException && request()->segment(1) == 'api') { + $errors = collect($e->errors())->map(function ($data, $key) { + return $data[0]; + })->toArray(); + return rest_api($errors, $e->getMessage(), 422); + } + return parent::render($request, $e); + } } diff --git a/app/Http/Controllers/Api/V1/Auth/RegisterController.php b/app/Http/Controllers/Api/V1/Auth/RegisterController.php new file mode 100644 index 0000000..22f5524 --- /dev/null +++ b/app/Http/Controllers/Api/V1/Auth/RegisterController.php @@ -0,0 +1,28 @@ +userApplication = $userApplication; + } + + public function register(RegisterRequest $request) + { + $model = new UserModel(); + $model->name = $request->name; + $model->email = $request->email; + $model->password = $request->password; + $user = $this->userApplication->register($model); + + return rest_api($user); + } +} diff --git a/app/Http/Controllers/Api/V1/User/UserController.php b/app/Http/Controllers/Api/V1/User/UserController.php new file mode 100644 index 0000000..32820cb --- /dev/null +++ b/app/Http/Controllers/Api/V1/User/UserController.php @@ -0,0 +1,14 @@ +user()); + return rest_api($user); + } +} \ No newline at end of file diff --git a/app/Http/Requests/Auth/RegisterRequest.php b/app/Http/Requests/Auth/RegisterRequest.php new file mode 100644 index 0000000..22f66be --- /dev/null +++ b/app/Http/Requests/Auth/RegisterRequest.php @@ -0,0 +1,32 @@ + ['required', 'max:50'], + 'email' => ['required', 'max:50', 'unique:users,email'], + 'password' => ['required', 'confirmed'] + ]; + } +} diff --git a/app/Http/Resources/UserResource.php b/app/Http/Resources/UserResource.php new file mode 100644 index 0000000..545eff7 --- /dev/null +++ b/app/Http/Resources/UserResource.php @@ -0,0 +1,16 @@ + $this->name, + "email" => $this->email, + "avatar" => $this->avatar + ]; + } +} \ No newline at end of file diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 1966456..eb6eb1f 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -26,7 +26,7 @@ class RouteServiceProvider extends ServiceProvider * * @var string|null */ - // protected $namespace = 'App\\Http\\Controllers'; + protected $namespace = 'App\Http\Controllers'; /** * Define your route model bindings, pattern filters, etc. diff --git a/composer.json b/composer.json index 4dcf43f..461bc87 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,10 @@ "App\\": "app/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/" - } + }, + "files": [ + "helpers/ResponseHelper.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/composer.lock b/composer.lock index a9377ad..9ddafc1 100644 --- a/composer.lock +++ b/composer.lock @@ -4261,16 +4261,16 @@ }, { "name": "facade/ignition", - "version": "2.3.7", + "version": "2.3.8", "source": { "type": "git", "url": "https://github.com/facade/ignition.git", - "reference": "b364db8860a63c1fb58b72b9718863c21df08762" + "reference": "e8fed9c382cd1d02b5606688576a35619afdf82c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/facade/ignition/zipball/b364db8860a63c1fb58b72b9718863c21df08762", - "reference": "b364db8860a63c1fb58b72b9718863c21df08762", + "url": "https://api.github.com/repos/facade/ignition/zipball/e8fed9c382cd1d02b5606688576a35619afdf82c", + "reference": "e8fed9c382cd1d02b5606688576a35619afdf82c", "shasum": "" }, "require": { @@ -4329,7 +4329,7 @@ "laravel", "page" ], - "time": "2020-09-06T19:26:27+00:00" + "time": "2020-10-01T23:01:14+00:00" }, { "name": "facade/ignition-contracts", @@ -5038,16 +5038,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "9.1.11", + "version": "9.2.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "c9394cb9d07ecfa9351b96f2e296bad473195f4d" + "reference": "53a4b737e83be724efd2bc4e7b929b9a30c48972" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/c9394cb9d07ecfa9351b96f2e296bad473195f4d", - "reference": "c9394cb9d07ecfa9351b96f2e296bad473195f4d", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/53a4b737e83be724efd2bc4e7b929b9a30c48972", + "reference": "53a4b737e83be724efd2bc4e7b929b9a30c48972", "shasum": "" }, "require": { @@ -5075,7 +5075,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "9.1-dev" + "dev-master": "9.2-dev" } }, "autoload": { @@ -5101,7 +5101,13 @@ "testing", "xunit" ], - "time": "2020-09-19T05:29:17+00:00" + "funding": [ + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2020-10-02T03:37:32+00:00" }, { "name": "phpunit/php-file-iterator", @@ -5306,16 +5312,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.3.11", + "version": "9.4.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "f7316ea106df7c9507f4fdaa88c47bc10a3b27a1" + "reference": "ef533467a7974c4b6c354f3eff42a115910bd4e5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f7316ea106df7c9507f4fdaa88c47bc10a3b27a1", - "reference": "f7316ea106df7c9507f4fdaa88c47bc10a3b27a1", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/ef533467a7974c4b6c354f3eff42a115910bd4e5", + "reference": "ef533467a7974c4b6c354f3eff42a115910bd4e5", "shasum": "" }, "require": { @@ -5331,7 +5337,7 @@ "phar-io/version": "^3.0.2", "php": ">=7.3", "phpspec/prophecy": "^1.11.1", - "phpunit/php-code-coverage": "^9.1.11", + "phpunit/php-code-coverage": "^9.2", "phpunit/php-file-iterator": "^3.0.4", "phpunit/php-invoker": "^3.1", "phpunit/php-text-template": "^2.0.2", @@ -5362,7 +5368,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "9.3-dev" + "dev-master": "9.4-dev" } }, "autoload": { @@ -5391,7 +5397,17 @@ "testing", "xunit" ], - "time": "2020-09-24T08:08:49+00:00" + "funding": [ + { + "url": "https://phpunit.de/donate.html", + "type": "custom" + }, + { + "url": "https://github.com/sebastianbergmann", + "type": "github" + } + ], + "time": "2020-10-02T03:54:37+00:00" }, { "name": "scrivo/highlight.php", @@ -6364,5 +6380,6 @@ "platform": { "php": "^7.3" }, - "platform-dev": [] + "platform-dev": [], + "plugin-api-version": "1.1.0" } diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 621a24e..2c450d2 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -17,8 +17,10 @@ public function up() $table->id(); $table->string('name'); $table->string('email')->unique(); - $table->timestamp('email_verified_at')->nullable(); $table->string('password'); + $table->string('avatar')->nullable(); + $table->unsignedBigInteger('created_by')->nullable(); + $table->unsignedBigInteger('updated_by')->nullable(); $table->rememberToken(); $table->timestamps(); }); diff --git a/database/migrations/2014_10_12_100000_create_password_resets_table.php b/database/migrations/2014_10_12_100000_create_password_resets_table.php deleted file mode 100644 index 0ee0a36..0000000 --- a/database/migrations/2014_10_12_100000_create_password_resets_table.php +++ /dev/null @@ -1,32 +0,0 @@ -string('email')->index(); - $table->string('token'); - $table->timestamp('created_at')->nullable(); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::dropIfExists('password_resets'); - } -} diff --git a/database/migrations/2019_08_19_000000_create_failed_jobs_table.php b/database/migrations/2019_08_19_000000_create_failed_jobs_table.php deleted file mode 100644 index 6aa6d74..0000000 --- a/database/migrations/2019_08_19_000000_create_failed_jobs_table.php +++ /dev/null @@ -1,36 +0,0 @@ -id(); - $table->string('uuid')->unique(); - $table->text('connection'); - $table->text('queue'); - $table->longText('payload'); - $table->longText('exception'); - $table->timestamp('failed_at')->useCurrent(); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::dropIfExists('failed_jobs'); - } -} diff --git a/helpers/ResponseHelper.php b/helpers/ResponseHelper.php new file mode 100644 index 0000000..fa9223f --- /dev/null +++ b/helpers/ResponseHelper.php @@ -0,0 +1,34 @@ + true, + "message" => $message ?? 'Response Success', + "data" => $data + ]; + } else { + $rest = [ + "success" => false, + "error-code" => $code, + "errors" => $data, + "message" => $message ?? 'Error has occurred' + ]; + } + + return response() + ->json($rest, $code) + ->withHeaders($addHeaders); + } +} diff --git a/routes/api.php b/routes/api.php index bcb8b18..3855324 100644 --- a/routes/api.php +++ b/routes/api.php @@ -13,7 +13,8 @@ | is assigned the "api" middleware group. Enjoy building your API! | */ - -Route::middleware('auth:api')->get('/user', function (Request $request) { - return $request->user(); +Route::prefix('v1')->namespace('Api\V1')->group(function () { + Route::namespace('Auth')->group(function () { + Route::post('register', 'RegisterController@register'); + }); }); diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php new file mode 100644 index 0000000..d3a6641 --- /dev/null +++ b/tests/Unit/UserTest.php @@ -0,0 +1,50 @@ +email = 'user@gmail.com'; + $model->password = 'rahasia123'; + $model->name = 'user'; + + app()->make(UserApplication::class)->register($model); + $this->assertDatabaseHas('users', [ + 'email' => 'user@gmail.com' + ]); + } + + /** + * a user test profile + * + * @return void + */ + public function testUserProfile() + { + $controller = new UserController(); + $response = $controller->profile(); + + $this->assertContains([ + "name" => "irfan", + "email" => "irfan@gmail.com" + ], $response); + } +}