Skip to content

Commit

Permalink
added register form with coins compatibale
Browse files Browse the repository at this point in the history
  • Loading branch information
arashactive committed Jun 13, 2022
1 parent be2594e commit 220faed
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 23 deletions.
8 changes: 7 additions & 1 deletion app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Actions\Fortify;

use App\Models\User;
use App\Services\Coins\CoinsForRegisterNewUser;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
Expand All @@ -20,6 +21,7 @@ class CreateNewUser implements CreatesNewUsers
*/
public function create(array $input)
{

Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => [
Expand All @@ -32,10 +34,14 @@ public function create(array $input)
'password' => $this->passwordRules(),
])->validate();

return User::create([
$user = User::create([
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
]);

app(CoinsForRegisterNewUser::class)->executed($user);

return $user;
}
}
2 changes: 2 additions & 0 deletions app/Models/CoinsLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
class CoinsLog extends Model
{
use HasFactory;

protected $guarded = [];
}
9 changes: 9 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,13 @@ public function Participants(): HasMany
'user_id'
);
}


public function CoinsLogs()
{
return $this->hasMany(
CoinsLog::class,
'user_id'
);
}
}
1 change: 1 addition & 0 deletions app/Providers/FortifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function register()
*/
public function boot()
{

Fortify::createUsersUsing(CreateNewUser::class);
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
Expand Down
39 changes: 39 additions & 0 deletions app/Services/Coins/CoinsForRegisterNewUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Services\Coins;

use App\Models\Configuration;
use App\Models\User;

class CoinsForRegisterNewUser extends UserCoins
{
protected int $coins = 0;


protected function setCoins()
{
$configuration = Configuration::where('config_type', 'CoinsForNewUserRegister')->first();
if ($configuration->config_value > 0)
$this->coins = $configuration->config_value;
}

public function executed(User $user): bool
{
$this->setCoins();

if ($this->coins == 0)
return false;

$user->CoinsLogs()->create([
'coins' => $this->coins,
'description' => trans('message/description.user_register_got_coins', ['coins' => $this->coins]),
'causable_type' => User::class,
'causable_id' => $user->id,
'is_manual' => 0
]);

$this->userUpdateCoins($user, $this->coins);

return true;
}
}
20 changes: 20 additions & 0 deletions app/Services/Coins/UserCoins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Services\Coins;

use App\Models\User;

class UserCoins
{

protected function userUpdateCoins(User $user, int $coins): bool
{
if ($coins == 0)
return false;

$user->coins = $user->coins + ($coins);
$user->save();

return true;
}
}
4 changes: 3 additions & 1 deletion database/migrations/2014_10_12_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ public function up()
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->integer('coins')->default(0);

$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('avatar')->nullable();
$table->rememberToken();
$table->string('theme')->default('default');
$table->rememberToken();
$table->timestamps();
});
}
Expand Down
35 changes: 14 additions & 21 deletions resources/views/auth/register.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
@extends('layouts.guest')

@extends('layouts.guest')
@section('content')

@section('content')

<div class="container">
<div class="container">

<div class="card o-hidden border-0 shadow-lg my-5">
<div class="card-body p-0">
Expand All @@ -15,34 +14,28 @@
<div class="text-center">
<h1 class="h4 text-gray-900 mb-4">Create an Account!</h1>
</div>
<form class="user" action="{{ URL::to('/login') }}" method="post">
<form class="user" action="{{ URL::to('/register') }}" method="post">
@csrf
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<input type="text" class="form-control form-control-user" id="exampleFirstName"
placeholder="First Name">
</div>
<div class="col-sm-6">
<input type="text" class="form-control form-control-user" id="exampleLastName"
placeholder="Last Name">
<div class="col-sm-12 mb-3 mb-sm-0">
<input type="text" name="name" class="form-control form-control-user" id="exampleFirstName" placeholder="full Name">
</div>

</div>
<div class="form-group">
<input type="email" class="form-control form-control-user" id="exampleInputEmail"
placeholder="Email Address">
<input name="email" type="email" class="form-control form-control-user" id="exampleInputEmail" placeholder="Email Address">
</div>
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<input type="password" class="form-control form-control-user"
id="exampleInputPassword" placeholder="Password">
<input name="password" type="password" class="form-control form-control-user" id="exampleInputPassword" placeholder="Password">
</div>
<div class="col-sm-6">
<input type="password" class="form-control form-control-user"
id="exampleRepeatPassword" placeholder="Repeat Password">
<input name="password_confirmation" type="password" class="form-control form-control-user" id="exampleRepeatPassword" placeholder="Repeat Password">
</div>
</div>
<a href="login.html" class="btn btn-primary btn-user btn-block">
<button type="submit" class="btn btn-primary btn-user btn-block">
Register Account
</a>
</button>
<hr>
<a href="index.html" class="btn btn-google btn-user btn-block">
<i class="fab fa-google fa-fw"></i> Register with Google
Expand All @@ -66,4 +59,4 @@

</div>

@endsection
@endsection
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Actions\Fortify\CreateNewUser;
use App\Http\Controllers\Acl\PermissionController;
use App\Http\Controllers\Acl\RoleController;
use App\Http\Controllers\Acl\UserController;
Expand Down
44 changes: 44 additions & 0 deletions tests/Feature/TDD/General/registerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tests\Feature\TDD\General;

use Tests\BaseTest;

class registerTest extends BaseTest
{

protected function setUp(): void
{
parent::setUp();
$this->seed();

$this->setBaseRoute('activity');
$this->setBaseModel('App\Models\Activity');
}


public function test_it_can_register_a_new_user()
{
$user = [
'name' => 'Test User',
'email' => '[email protected]',
'password' => 'password',
'password_confirmation' => 'password',
];
$this->post('/register', $user);

$this->assertDatabaseHas('users', [
'name' => $user['name'],
'email' => $user['email'],
]);
}


public function test_register_a_new_user_get_coins()
{

$this->test_it_can_register_a_new_user();

$this->assertDatabaseCount('coins_logs', 1);
}
}

0 comments on commit 220faed

Please sign in to comment.