Initial commit
This commit is contained in:
61
app/Actions/PerformWalletTransaction.php
Normal file
61
app/Actions/PerformWalletTransaction.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions;
|
||||
|
||||
use App\Enums\WalletTransactionType;
|
||||
use App\Exceptions\InsufficientBalance;
|
||||
use App\Models\Wallet;
|
||||
use App\Models\WalletTransaction;
|
||||
use App\Models\WalletTransfer;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Throwable;
|
||||
|
||||
readonly class PerformWalletTransaction
|
||||
{
|
||||
/**
|
||||
* @throws InsufficientBalance
|
||||
*/
|
||||
public function execute(Wallet $wallet, WalletTransactionType $type, int $amount, string $reason, ?WalletTransfer $transfer = null, bool $force = false): WalletTransaction
|
||||
{
|
||||
if (! $force && $type === WalletTransactionType::DEBIT) {
|
||||
$this->ensureWalletHasEnoughFunds($wallet, $amount);
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($wallet, $type, $amount, $reason, $transfer) {
|
||||
$transaction = $wallet->transactions()->create([
|
||||
'amount' => $amount,
|
||||
'type' => $type,
|
||||
'reason' => $reason,
|
||||
'transfer_id' => $transfer?->id,
|
||||
]);
|
||||
|
||||
$this->updateWallet($wallet, $type, $amount);
|
||||
|
||||
return $transaction;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
protected function updateWallet(Wallet $wallet, WalletTransactionType $type, int $amount): void
|
||||
{
|
||||
if ($type === WalletTransactionType::CREDIT) {
|
||||
$wallet->increment('balance', $amount);
|
||||
} else {
|
||||
$wallet->decrement('balance', $amount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InsufficientBalance
|
||||
*/
|
||||
protected function ensureWalletHasEnoughFunds(Wallet $wallet, int $amount): void
|
||||
{
|
||||
if ($wallet->balance < $amount) {
|
||||
throw new InsufficientBalance($wallet, $amount);
|
||||
}
|
||||
}
|
||||
}
|
48
app/Actions/PerformWalletTransfer.php
Normal file
48
app/Actions/PerformWalletTransfer.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions;
|
||||
|
||||
use App\Enums\WalletTransactionType;
|
||||
use App\Exceptions\InsufficientBalance;
|
||||
use App\Models\User;
|
||||
use App\Models\WalletTransfer;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
readonly class PerformWalletTransfer
|
||||
{
|
||||
public function __construct(protected PerformWalletTransaction $performWalletTransaction) {}
|
||||
|
||||
/**
|
||||
* @throws InsufficientBalance
|
||||
*/
|
||||
public function execute(User $sender, User $recipient, int $amount, string $reason): WalletTransfer
|
||||
{
|
||||
return DB::transaction(function () use ($sender, $recipient, $amount, $reason) {
|
||||
$transfer = WalletTransfer::create([
|
||||
'amount' => $amount,
|
||||
'source_id' => $sender->wallet->id,
|
||||
'target_id' => $recipient->wallet->id,
|
||||
]);
|
||||
|
||||
$this->performWalletTransaction->execute(
|
||||
wallet: $sender->wallet,
|
||||
type: WalletTransactionType::DEBIT,
|
||||
amount: $amount,
|
||||
reason: $reason,
|
||||
transfer: $transfer
|
||||
);
|
||||
|
||||
$this->performWalletTransaction->execute(
|
||||
wallet: $recipient->wallet,
|
||||
type: WalletTransactionType::CREDIT,
|
||||
amount: $amount,
|
||||
reason: $reason,
|
||||
transfer: $transfer
|
||||
);
|
||||
|
||||
return $transfer;
|
||||
});
|
||||
}
|
||||
}
|
21
app/Enums/WalletTransactionType.php
Normal file
21
app/Enums/WalletTransactionType.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum WalletTransactionType: string
|
||||
{
|
||||
case CREDIT = 'credit';
|
||||
case DEBIT = 'debit';
|
||||
|
||||
public function isDebit(): bool
|
||||
{
|
||||
return $this === self::DEBIT;
|
||||
}
|
||||
|
||||
public function isCredit(): bool
|
||||
{
|
||||
return $this === self::CREDIT;
|
||||
}
|
||||
}
|
41
app/Exceptions/ApiException.php
Normal file
41
app/Exceptions/ApiException.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Support\Responsable;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Throwable;
|
||||
|
||||
class ApiException extends Exception implements Responsable
|
||||
{
|
||||
public readonly string $customCode;
|
||||
|
||||
public readonly int $status;
|
||||
|
||||
public readonly bool $silenced;
|
||||
|
||||
public function __construct(
|
||||
string $message,
|
||||
string $code,
|
||||
?int $status = null,
|
||||
bool $silenced = false,
|
||||
?Throwable $previous = null
|
||||
) {
|
||||
parent::__construct($message, 0, $previous);
|
||||
|
||||
$this->silenced = $silenced;
|
||||
$this->customCode = $code;
|
||||
$this->status = $status ?? 400;
|
||||
}
|
||||
|
||||
public function toResponse($request): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'code' => $this->customCode,
|
||||
'message' => $this->message,
|
||||
], $this->status);
|
||||
}
|
||||
}
|
15
app/Exceptions/InsufficientBalance.php
Normal file
15
app/Exceptions/InsufficientBalance.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use App\Models\Wallet;
|
||||
|
||||
class InsufficientBalance extends ApiException
|
||||
{
|
||||
public function __construct(public readonly Wallet $wallet, public readonly int $amount)
|
||||
{
|
||||
parent::__construct(message: 'Insufficient balance in wallet.', code: 'INSUFFICIENT_BALANCE', status: 400);
|
||||
}
|
||||
}
|
16
app/Http/Controllers/Api/V1/AccountController.php
Normal file
16
app/Http/Controllers/Api/V1/AccountController.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Resources\AccountResource;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AccountController
|
||||
{
|
||||
public function __invoke(Request $request): AccountResource
|
||||
{
|
||||
return AccountResource::make($request->user());
|
||||
}
|
||||
}
|
33
app/Http/Controllers/Api/V1/LoginController.php
Normal file
33
app/Http/Controllers/Api/V1/LoginController.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Requests\Api\V1\LoginRequest;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class LoginController
|
||||
{
|
||||
public function __invoke(LoginRequest $request): JsonResponse
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = User::query()->where('email', $request->validated('email'))->first();
|
||||
|
||||
if (is_null($user)) {
|
||||
return response()->json([
|
||||
'message' => 'Invalid credentials.',
|
||||
'code' => 'BAD_LOGIN',
|
||||
], 400);
|
||||
}
|
||||
|
||||
$token = $user->createToken($request->validated('device_name'))->plainTextToken;
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'token' => $token,
|
||||
],
|
||||
], 201);
|
||||
}
|
||||
}
|
26
app/Http/Controllers/Api/V1/SendMoneyController.php
Normal file
26
app/Http/Controllers/Api/V1/SendMoneyController.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Actions\PerformWalletTransfer;
|
||||
use App\Http\Requests\Api\V1\SendMoneyRequest;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class SendMoneyController
|
||||
{
|
||||
public function __invoke(SendMoneyRequest $request, PerformWalletTransfer $performWalletTransfer): Response
|
||||
{
|
||||
$recipient = $request->getRecipient();
|
||||
|
||||
$performWalletTransfer->execute(
|
||||
sender: $request->user(),
|
||||
recipient: $recipient,
|
||||
amount: $request->input('amount'),
|
||||
reason: $request->input('reason'),
|
||||
);
|
||||
|
||||
return response()->noContent(201);
|
||||
}
|
||||
}
|
39
app/Http/Controllers/Auth/AuthenticatedSessionController.php
Normal file
39
app/Http/Controllers/Auth/AuthenticatedSessionController.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Requests\LoginRequest;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class AuthenticatedSessionController
|
||||
{
|
||||
public function create(): View
|
||||
{
|
||||
return view('auth.login');
|
||||
}
|
||||
|
||||
public function store(LoginRequest $request): RedirectResponse
|
||||
{
|
||||
$request->authenticate();
|
||||
|
||||
$request->session()->regenerate();
|
||||
|
||||
return redirect()->intended(route('dashboard', absolute: false));
|
||||
}
|
||||
|
||||
public function destroy(Request $request): RedirectResponse
|
||||
{
|
||||
Auth::guard('web')->logout();
|
||||
|
||||
$request->session()->invalidate();
|
||||
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
return redirect('/');
|
||||
}
|
||||
}
|
43
app/Http/Controllers/Auth/RegisteredUserController.php
Normal file
43
app/Http/Controllers/Auth/RegisteredUserController.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rules;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class RegisteredUserController
|
||||
{
|
||||
public function create(): View
|
||||
{
|
||||
return view('auth.register');
|
||||
}
|
||||
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'string', 'email', 'max:255', 'unique:'.User::class],
|
||||
'password' => ['required', 'confirmed', Rules\Password::defaults()],
|
||||
]);
|
||||
|
||||
$user = User::create([
|
||||
'name' => $request->name,
|
||||
'email' => strtolower($request->email),
|
||||
'password' => Hash::make($request->password),
|
||||
]);
|
||||
|
||||
event(new Registered($user));
|
||||
|
||||
Auth::login($user);
|
||||
|
||||
return redirect(route('dashboard', absolute: false));
|
||||
}
|
||||
}
|
18
app/Http/Controllers/DashboardController.php
Normal file
18
app/Http/Controllers/DashboardController.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class DashboardController
|
||||
{
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
$transactions = $request->user()->wallet->transactions()->with('transfer')->orderByDesc('id')->get();
|
||||
$balance = $request->user()->wallet->balance;
|
||||
|
||||
return view('dashboard', compact('transactions', 'balance'));
|
||||
}
|
||||
}
|
35
app/Http/Controllers/SendMoneyController.php
Normal file
35
app/Http/Controllers/SendMoneyController.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\PerformWalletTransfer;
|
||||
use App\Exceptions\InsufficientBalance;
|
||||
use App\Http\Requests\SendMoneyRequest;
|
||||
|
||||
class SendMoneyController
|
||||
{
|
||||
public function __invoke(SendMoneyRequest $request, PerformWalletTransfer $performWalletTransfer)
|
||||
{
|
||||
$recipient = $request->getRecipient();
|
||||
|
||||
try {
|
||||
$performWalletTransfer->execute(
|
||||
sender: $request->user(),
|
||||
recipient: $recipient,
|
||||
amount: $request->getAmountInCents(),
|
||||
reason: $request->input('reason'),
|
||||
);
|
||||
|
||||
return redirect()->back()
|
||||
->with('money-sent-status', 'success')
|
||||
->with('money-sent-recipient-name', $recipient->name)
|
||||
->with('money-sent-amount', $request->getAmountInCents());
|
||||
} catch (InsufficientBalance $exception) {
|
||||
return redirect()->back()->with('money-sent-status', 'insufficient-balance')
|
||||
->with('money-sent-recipient-name', $recipient->name)
|
||||
->with('money-sent-amount', $request->getAmountInCents());
|
||||
}
|
||||
}
|
||||
}
|
21
app/Http/Middleware/ForceAcceptJson.php
Normal file
21
app/Http/Middleware/ForceAcceptJson.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class ForceAcceptJson
|
||||
{
|
||||
public function handle(Request $request, Closure $next): Response|RedirectResponse|JsonResponse
|
||||
{
|
||||
$request->headers->set('Accept', 'application/json');
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
24
app/Http/Requests/Api/V1/LoginRequest.php
Normal file
24
app/Http/Requests/Api/V1/LoginRequest.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class LoginRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'email' => 'required|email:rfc',
|
||||
'password' => 'required',
|
||||
'device_name' => 'required|string',
|
||||
];
|
||||
}
|
||||
}
|
43
app/Http/Requests/Api/V1/SendMoneyRequest.php
Normal file
43
app/Http/Requests/Api/V1/SendMoneyRequest.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class SendMoneyRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'recipient_email' => [
|
||||
'required',
|
||||
'email',
|
||||
Rule::exists(User::class, 'email')->whereNot('id', $this->user()->id),
|
||||
],
|
||||
'amount' => [
|
||||
'required',
|
||||
'integer',
|
||||
'min:1',
|
||||
],
|
||||
'reason' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function getRecipient(): User
|
||||
{
|
||||
return User::where('email', '=', $this->input('recipient_email'))->firstOrFail();
|
||||
}
|
||||
}
|
72
app/Http/Requests/LoginRequest.php
Normal file
72
app/Http/Requests/LoginRequest.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Auth\Events\Lockout;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class LoginRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'email' => ['required', 'string', 'email'],
|
||||
'password' => ['required', 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
public function authenticate(): void
|
||||
{
|
||||
$this->ensureIsNotRateLimited();
|
||||
|
||||
if (! Auth::attempt([
|
||||
'email' => strtolower($this->email),
|
||||
'password' => $this->password,
|
||||
])) {
|
||||
RateLimiter::hit($this->throttleKey());
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'email' => trans('auth.failed'),
|
||||
]);
|
||||
}
|
||||
|
||||
RateLimiter::clear($this->throttleKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function ensureIsNotRateLimited(): void
|
||||
{
|
||||
if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event(new Lockout($this));
|
||||
|
||||
$seconds = RateLimiter::availableIn($this->throttleKey());
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'email' => trans('auth.throttle', [
|
||||
'seconds' => $seconds,
|
||||
'minutes' => ceil($seconds / 60),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function throttleKey(): string
|
||||
{
|
||||
return Str::transliterate(Str::lower($this->string('email')).'|'.$this->ip());
|
||||
}
|
||||
}
|
48
app/Http/Requests/SendMoneyRequest.php
Normal file
48
app/Http/Requests/SendMoneyRequest.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class SendMoneyRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'recipient_email' => [
|
||||
'required',
|
||||
'email',
|
||||
Rule::exists(User::class, 'email')->whereNot('id', $this->user()->id),
|
||||
],
|
||||
'amount' => [
|
||||
'required',
|
||||
'numeric',
|
||||
'min:0.01',
|
||||
],
|
||||
'reason' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function getRecipient(): User
|
||||
{
|
||||
return User::where('email', '=', $this->input('recipient_email'))->firstOrFail();
|
||||
}
|
||||
|
||||
public function getAmountInCents(): int
|
||||
{
|
||||
return (int) ceil($this->float('amount') * 100);
|
||||
}
|
||||
}
|
25
app/Http/Resources/AccountResource.php
Normal file
25
app/Http/Resources/AccountResource.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/**
|
||||
* @mixin User
|
||||
*/
|
||||
class AccountResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'email' => $this->email,
|
||||
'balance' => $this->wallet->balance,
|
||||
];
|
||||
}
|
||||
}
|
41
app/Models/User.php
Normal file
41
app/Models/User.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasApiTokens, HasFactory, Notifiable;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
'password',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasOne<Wallet>
|
||||
*/
|
||||
public function wallet(): HasOne
|
||||
{
|
||||
return $this->hasOne(Wallet::class);
|
||||
}
|
||||
}
|
31
app/Models/Wallet.php
Normal file
31
app/Models/Wallet.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Wallet extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return BelongsTo<User>
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<WalletTransaction>
|
||||
*/
|
||||
public function transactions(): HasMany
|
||||
{
|
||||
return $this->hasMany(WalletTransaction::class);
|
||||
}
|
||||
}
|
43
app/Models/WalletTransaction.php
Normal file
43
app/Models/WalletTransaction.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\WalletTransactionType;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class WalletTransaction extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'type' => WalletTransactionType::class,
|
||||
];
|
||||
}
|
||||
|
||||
protected function isTransfer(): Attribute
|
||||
{
|
||||
return Attribute::get(fn () => filled($this->transfer_id));
|
||||
}
|
||||
|
||||
public function transfer(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(WalletTransfer::class, 'transfer_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Wallet>
|
||||
*/
|
||||
public function wallet(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Wallet::class);
|
||||
}
|
||||
}
|
48
app/Models/WalletTransfer.php
Normal file
48
app/Models/WalletTransfer.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class WalletTransfer extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Wallet>
|
||||
*/
|
||||
public function source(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Wallet::class, 'source_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Wallet>
|
||||
*/
|
||||
public function target(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Wallet::class, 'target_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<WalletTransaction>
|
||||
*/
|
||||
public function credit(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(WalletTransaction::class, 'credit_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Wallet>
|
||||
*/
|
||||
public function debit(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(WalletTransaction::class, 'debit_id');
|
||||
}
|
||||
}
|
41
app/Providers/AppServiceProvider.php
Normal file
41
app/Providers/AppServiceProvider.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Number;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Validation\Rules\Password;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void {}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
Number::useCurrency('EUR');
|
||||
|
||||
Number::macro('currencyCents', function (int|float $number, string $in = '', ?string $locale = null) {
|
||||
return Number::currency((int) $number / 100, $in, $locale);
|
||||
});
|
||||
|
||||
Password::defaults(function () {
|
||||
return app()->isProduction() ? Password::min(8)
|
||||
->numbers()
|
||||
->mixedCase()
|
||||
->uncompromised() : Password::min(6);
|
||||
});
|
||||
|
||||
RateLimiter::for('api.login', function (Request $request) {
|
||||
return Limit::perMinutes(5, 5)->by($request->ip());
|
||||
});
|
||||
|
||||
RateLimiter::for('api', function (Request $request) {
|
||||
return Limit::perMinute(120)->by($request->user()?->id ?: $request->ip());
|
||||
});
|
||||
}
|
||||
}
|
16
app/View/Components/AppLayout.php
Normal file
16
app/View/Components/AppLayout.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\View\Components;
|
||||
|
||||
use Illuminate\View\Component;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class AppLayout extends Component
|
||||
{
|
||||
public function render(): View
|
||||
{
|
||||
return view('layouts.app');
|
||||
}
|
||||
}
|
16
app/View/Components/GuestLayout.php
Normal file
16
app/View/Components/GuestLayout.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\View\Components;
|
||||
|
||||
use Illuminate\View\Component;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class GuestLayout extends Component
|
||||
{
|
||||
public function render(): View
|
||||
{
|
||||
return view('layouts.guest');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user