Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.
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
8 changes: 5 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=sqlite
DB_CONNECTION=mysql
#DB_HOST=127.0.0.1
#DB_PORT=3306
#DB_DATABASE=laravel
Expand All @@ -15,15 +15,15 @@ DB_CONNECTION=sqlite

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
QUEUE_CONNECTION=database
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_DRIVER=log
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
Expand All @@ -42,3 +42,5 @@ PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

POST_CODE_CHECK_URL=https://api.postcodes.io/postcodes/
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/node_modules
/public/hot
/public/storage
/storage/*.key
/storage/
/vendor
.env
.env.backup
Expand All @@ -12,3 +12,4 @@ npm-debug.log
yarn-error.log

/public/
/nbproject/
49 changes: 49 additions & 0 deletions app/Console/Commands/UsersList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\User;

class UsersList extends Command {

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'users:list';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Listing all the registered users in the system';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct() {
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle() {
$data = User::get(['name', 'email', 'postcode'])->toArray();
if (count($data)) {
$headers = ['Name', 'Email', 'Postcode'];
$this->table($headers, $data);
} else {
$this->info("No users yet");
}
return 0;
}

}
2 changes: 1 addition & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function report(Throwable $exception)
* @return Response
* @throws Throwable
*/
public function render($request, Throwable $exception): Response
public function render($request, Throwable $exception)
{
return parent::render($request, $exception);
}
Expand Down
37 changes: 37 additions & 0 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoringUserRequest;
use App\Services\CreatingUserService;

class UsersController extends Controller {

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
return view('register');
}

/**
* Store a newly created resource in storage.
*
* @param StoringUserRequestRequest $request
* @param CreatingUserService $service
* @return \Illuminate\Http\Response
*/
public function store(
StoringUserRequest $request,
CreatingUserService $service
) {
$user_saved = $service->execute($request->validated());
if ($user_saved) {
return back()->with('success', 'Your account has been added!');
}
return back()->with('error', 'Something went wrong, please try again later!');
}

}
38 changes: 38 additions & 0 deletions app/Http/Requests/StoringUserRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Rules\PostCode;

/**
* Description of StoringUserRequest
*
* @author ali
*/
class StoringUserRequest extends FormRequest {

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize() {
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules() {
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'postcode' => ['bail', 'required', 'string', 'max:255', new PostCode],
];
}

}
9 changes: 5 additions & 4 deletions app/User.php → app/Models/User.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace App;
namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
class User extends Authenticatable {

use Notifiable;

/**
Expand All @@ -16,7 +16,7 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
'name', 'email', 'password', 'postcode'
];

/**
Expand All @@ -36,4 +36,5 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

}
36 changes: 36 additions & 0 deletions app/Notifications/WelcomingEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class WelcomingEmail extends Notification implements ShouldQueue {

use Queueable;

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable) {
return ['mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable) {
return (new MailMessage)
->line('Welcome ' . $notifiable->name)
->line('Thank you for using our application!');
}

}
20 changes: 20 additions & 0 deletions app/Observers/UserObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Observers;

use App\Models\User;
use App\Notifications\WelcomingEmail;

class UserObserver {

/**
* Handle the user "created" event.
*
* @param \App\User $user
* @return void
*/
public function created(User $user) {
$user->notify(new WelcomingEmail);
}

}
5 changes: 3 additions & 2 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use App\Models\User;
use App\Observers\UserObserver;

class EventServiceProvider extends ServiceProvider
{
Expand All @@ -28,7 +30,6 @@ class EventServiceProvider extends ServiceProvider
public function boot()
{
parent::boot();

//
User::observe(UserObserver::class);
}
}
36 changes: 36 additions & 0 deletions app/Repositories/BaseRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Repositories;

use Illuminate\Database\Eloquent\Model;

/**
* Description of BaseRepository
*
* @author ali
*/
class BaseRepository {

/**
*
* @var Model
*/
protected $model = null;

/**
*
* @param Model $model
*/
protected function __construct(Model $model) {
$this->model = $model;
}

public function all() {
return $this->model->all();
}

public function create(array $data) {
return $this->model->create($data);
}

}
22 changes: 22 additions & 0 deletions app/Repositories/UsersRepositories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Repositories;

use App\Models\User;

/**
* Description of UsersRepositories
*
* @author ali
*/
class UsersRepositories extends BaseRepository {

/**
*
* @param User $user
*/
public function __construct(User $user) {
parent::__construct($user);
}

}
34 changes: 34 additions & 0 deletions app/Rules/PostCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class PostCode implements Rule {

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value) {
$response = guzzleGet(
['Content-Type' => 'application/json'],
getenv('POST_CODE_CHECK_URL') . $value . '/validate'
);

return $response['status'] ? $response['response']['result'] : false;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message() {
return 'Invalid Postcode';
}

}
Loading