Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
.idea/
38 changes: 38 additions & 0 deletions app/Http/Controllers/Api/AuthenticationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Validator;

class AuthenticationController extends Controller
{
private $userRepository;

public function __construct(UserRepository $userRepository)
{
$this->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);
}
}
23 changes: 23 additions & 0 deletions app/Models/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
use HasFactory;

protected $fillable = ['user_id', 'name', 'description', 'photo'];
protected $dates = ['created_at', 'updated_at'];


/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* */

public function user(){
return $this->belongsTo(User::class);
}
}
16 changes: 8 additions & 8 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class User extends Authenticatable
'name',
'email',
'password',
'avatar'
];

/**
Expand All @@ -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);
}
}
23 changes: 23 additions & 0 deletions app/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Repositories;


use App\Models\User;
use Illuminate\Support\Facades\Hash;

class UserRepository
{
public function registerUser($userData){
return User::create([
'name' => $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);
}
}
3 changes: 1 addition & 2 deletions database/migrations/2014_10_12_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down

This file was deleted.

36 changes: 0 additions & 36 deletions database/migrations/2019_08_19_000000_create_failed_jobs_table.php

This file was deleted.

35 changes: 35 additions & 0 deletions database/migrations/2020_09_30_163303_create_products_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->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');
}
}
6 changes: 6 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});