-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from andeen171/transactions
transactions
- Loading branch information
Showing
45 changed files
with
1,420 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controller; | ||
|
||
use App\Request\TransferRequest; | ||
use App\Resource\TransactionResource; | ||
use App\Service\TransactionService; | ||
use Psr\Http\Message\ResponseInterface as PsrResponseInterface; | ||
use Throwable; | ||
|
||
class TransactionController | ||
{ | ||
public function __construct( | ||
protected readonly TransactionService $transactionService, | ||
) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws Throwable | ||
*/ | ||
public function transfer(TransferRequest $request): PsrResponseInterface | ||
{ | ||
$transaction = $this->transactionService->transfer($request->validated()); | ||
|
||
return TransactionResource::make($transaction); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controller; | ||
|
||
use App\Repository\UserRepository; | ||
use App\Request\ListShopkeepersRequest; | ||
use App\Request\ListUsersRequest; | ||
use App\Request\ShowUserRequest; | ||
use App\Resource\UserResource; | ||
use App\Service\AuthService; | ||
use Psr\Http\Message\ResponseInterface as PsrResponseInterface; | ||
|
||
class UserController | ||
{ | ||
public function __construct( | ||
protected readonly UserRepository $repository, | ||
) | ||
{ | ||
} | ||
|
||
public function loggedUser(AuthService $authService): PsrResponseInterface | ||
{ | ||
return UserResource::make($authService->getLoggedUser())->toResponse(); | ||
} | ||
|
||
public function listUsers(ListUsersRequest $request): PsrResponseInterface | ||
{ | ||
return UserResource::collection($this->repository->listUsers($request->validated()))->toResponse(); | ||
} | ||
|
||
public function listShopkeepers(ListShopkeepersRequest $request): PsrResponseInterface | ||
{ | ||
return UserResource::collection($this->repository->listShopKeepers($request->validated()))->toResponse(); | ||
} | ||
|
||
public function showUser(ShowUserRequest $request): PsrResponseInterface | ||
{ | ||
return UserResource::make($request->getItem())->toResponse(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Exception; | ||
|
||
use App\Enum\ExceptionMessageCodeEnum; | ||
use Swoole\Http\Status; | ||
|
||
class InsufficientFundsException extends AbstractException | ||
{ | ||
public function __construct() | ||
{ | ||
$code = Status::FORBIDDEN; | ||
$message = ExceptionMessageCodeEnum::INSUFFICIENT_FUNDS; | ||
parent::__construct($message, $code); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Exception; | ||
|
||
use App\Enum\ExceptionMessageCodeEnum; | ||
use Swoole\Http\Status; | ||
|
||
class ShopkeeperCannotTransferException extends AbstractException | ||
{ | ||
public function __construct() | ||
{ | ||
$code = Status::FORBIDDEN; | ||
$message = ExceptionMessageCodeEnum::SHOPKEEPER_CANNOT_TRANSFER; | ||
parent::__construct($message, $code); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Exception; | ||
|
||
use App\Enum\ExceptionMessageCodeEnum; | ||
use Swoole\Http\Status; | ||
use Throwable; | ||
|
||
class TransactionFailedException extends AbstractException | ||
{ | ||
public function __construct(Throwable $previous) | ||
{ | ||
$code = Status::FAILED_DEPENDENCY; | ||
$message = ExceptionMessageCodeEnum::TRANSACTION_FAILED; | ||
parent::__construct($message, $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Model; | ||
|
||
use Carbon\Carbon; | ||
use Hyperf\Database\Model\Relations\BelongsTo; | ||
use Hyperf\DbConnection\Model\Model; | ||
|
||
/** | ||
* @property int $id | ||
* @property int $amount | ||
* @property int $payer_user_id | ||
* @property int $payee_user_id | ||
* @property Carbon $created_at | ||
* @property Carbon $updated_at | ||
* @property-read User $payer | ||
* @property-read User $payee | ||
*/ | ||
class Transaction extends Model | ||
{ | ||
protected ?string $table = 'transactions'; | ||
|
||
protected array $fillable = [ | ||
'amount', | ||
'payer_user_id', | ||
'payee_user_id', | ||
]; | ||
|
||
protected array $casts = [ | ||
'id' => 'integer', | ||
'amount' => 'integer', | ||
'payer_user_id' => 'integer', | ||
'payee_user_id' => 'integer', | ||
'created_at' => 'datetime', | ||
'updated_at' => 'datetime' | ||
]; | ||
|
||
public function payer(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class, 'payer_id'); | ||
} | ||
|
||
|
||
public function payee(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class, 'payee_user_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Model; | ||
|
||
use Carbon\Carbon; | ||
use Hyperf\Database\Model\Relations\BelongsTo; | ||
use Hyperf\DbConnection\Model\Model; | ||
|
||
/** | ||
* @property int $id | ||
* @property int $balance | ||
* @property int $user_id | ||
* @property Carbon $created_at | ||
* @property Carbon $updated_at | ||
* @property-read User $user | ||
*/ | ||
class Wallet extends Model | ||
{ | ||
protected ?string $table = 'wallets'; | ||
|
||
protected array $fillable = [ | ||
'balance', | ||
'user_id' | ||
]; | ||
|
||
protected array $casts = [ | ||
'id' => 'integer', | ||
'created_at' => 'datetime', | ||
'updated_at' => 'datetime', | ||
'balance' => 'integer', | ||
'user_id' => 'integer' | ||
]; | ||
|
||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class, 'user_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\Repository; | ||
|
||
use App\Enum\UserTypeEnum; | ||
use App\Model\User; | ||
use Hyperf\Contract\LengthAwarePaginatorInterface; | ||
|
||
class UserRepository | ||
{ | ||
/** | ||
* @param array{ | ||
* page: ?integer, | ||
* perPage: ?integer, | ||
* } $filters | ||
*/ | ||
public function listShopKeepers(array $filters): LengthAwarePaginatorInterface | ||
{ | ||
return User::query() | ||
->where('type', UserTypeEnum::SHOPKEEPER->value) | ||
->paginate( | ||
perPage: $filters['perPage'] ?? null, | ||
page: $filters['page'] ?? null | ||
); | ||
} | ||
|
||
/** | ||
* @param array{ | ||
* page: ?integer, | ||
* perPage: ?integer, | ||
* } $filters | ||
*/ | ||
public function listUsers(array $filters): LengthAwarePaginatorInterface | ||
{ | ||
return User::query() | ||
->where('type', UserTypeEnum::COMMON->value) | ||
->paginate( | ||
perPage: $filters['perPage'] ?? null, | ||
page: $filters['page'] ?? null | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Request; | ||
|
||
use Hyperf\Validation\Request\FormRequest; | ||
|
||
class ListShopkeepersRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
|
||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Request; | ||
|
||
use Hyperf\Validation\Request\FormRequest; | ||
|
||
class ListUsersRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
|
||
]; | ||
} | ||
} |
Oops, something went wrong.