diff --git a/.gitignore b/.gitignore index 0f7df0f..5740ff0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ Homestead.json Homestead.yaml npm-debug.log yarn-error.log +.idea/ diff --git a/app/Http/Controllers/Api/AuthenticationController.php b/app/Http/Controllers/Api/AuthenticationController.php new file mode 100644 index 0000000..ddc5c0b --- /dev/null +++ b/app/Http/Controllers/Api/AuthenticationController.php @@ -0,0 +1,38 @@ +userRepository = $userRepository; + } + + public function register(Request $request) + { + $rules = [ + 'name' => 'required|min:3|max:32', + 'email' => 'required|email|min:5|max:32', + 'password' => 'required|min:3|max:32|confirmed' + ]; + + $validator = Validator::make($request->all(), $rules); + if ($validator->fails()) { + return Response::json($validator->errors()); + } + + $user = $this->userRepository + ->registerUser($request->only(['name', 'email', 'password'])); + + return Response::json($user); + } +} diff --git a/app/Models/Product.php b/app/Models/Product.php new file mode 100644 index 0000000..6caf2db --- /dev/null +++ b/app/Models/Product.php @@ -0,0 +1,23 @@ +belongsTo(User::class); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 804799b..b6bfc75 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -20,6 +20,7 @@ class User extends Authenticatable 'name', 'email', 'password', + 'avatar' ]; /** @@ -29,15 +30,14 @@ class User extends Authenticatable */ protected $hidden = [ 'password', - 'remember_token', ]; /** - * The attributes that should be cast to native types. - * - * @var array - */ - protected $casts = [ - 'email_verified_at' => 'datetime', - ]; + * @return \Illuminate\Database\Eloquent\Relations\HasMany + * */ + + public function products() + { + return $this->hasMany(Product::class); + } } diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php new file mode 100644 index 0000000..2c5f8bd --- /dev/null +++ b/app/Repositories/UserRepository.php @@ -0,0 +1,23 @@ + $userData['name'], + 'email' => $userData['email'], + 'password' => Hash::make($userData['password']), + 'avatar' => $this->generateAvatarUrl($userData['name']) + ]); + } + + public function generateAvatarUrl($name){ + return "https://ui-avatars.com/api/?name=" . urlencode($name); + } +} 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..b7ce7e3 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -17,9 +17,8 @@ public function up() $table->id(); $table->string('name'); $table->string('email')->unique(); - $table->timestamp('email_verified_at')->nullable(); $table->string('password'); - $table->rememberToken(); + $table->string('avatar'); $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/database/migrations/2020_09_30_163303_create_products_table.php b/database/migrations/2020_09_30_163303_create_products_table.php new file mode 100644 index 0000000..7646362 --- /dev/null +++ b/database/migrations/2020_09_30_163303_create_products_table.php @@ -0,0 +1,35 @@ +id(); + $table->unsignedBigInteger('user_id'); + $table->string('name'); + $table->text('description'); + $table->string('photo'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('products'); + } +} diff --git a/routes/api.php b/routes/api.php index bcb8b18..2046081 100644 --- a/routes/api.php +++ b/routes/api.php @@ -17,3 +17,9 @@ Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); }); + +Route::group([ + 'prefix' => 'v1', +], function(){ + Route::post('/register', [\App\Http\Controllers\Api\AuthenticationController::class, 'register']); +});