-
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.
- Loading branch information
Showing
14 changed files
with
417 additions
and
4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,20 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use Domain\Gifts\Models\Gift; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class DBGiftFactory extends Factory | ||
{ | ||
protected $model = Gift::class; | ||
|
||
public function definition(): array | ||
{ | ||
return [ | ||
'note' => $this->faker->text, | ||
'amount' => $this->faker->numberBetween(1, 1000), | ||
'currency' => 'USD', | ||
]; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
database/migrations/2023_12_18_013819_create_gifts_table.php
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,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('gifts', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('note', 255)->nullable(); | ||
$table->bigInteger('amount'); | ||
$table->char('currency', 3); | ||
$table->foreignId('user_id')->constrained('users'); | ||
$table->foreignId('sender_user_id')->constrained('users'); | ||
// TODO: Evaluate if add or not pocket_id field, it can be gotten from the user now | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('gifts'); | ||
} | ||
}; |
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,45 @@ | ||
<?php | ||
|
||
namespace Domain\Gifts\Actions; | ||
|
||
use Domain\Gifts\Data\StoreGiftData; | ||
use Domain\Gifts\Models\Gift; | ||
use Domain\Pockets\Models\Pocket; | ||
use Domain\Users\Models\User; | ||
use Money\Currency; | ||
use Money\Money; | ||
|
||
class StoreGiftAction | ||
{ | ||
public function __invoke(StoreGiftData $data, User $senderUser, User $user): Gift | ||
{ | ||
/** @var Pocket $userPocket */ | ||
$userPocket = Pocket::query()->whereId($user->pocket_id) | ||
->select(['id', 'balance', 'currency']) | ||
->first(); | ||
|
||
// TODO: Throw an error when there is no user pocket | ||
|
||
// TODO: Support conversion between currencies | ||
if ($userPocket->currency !== $data->currency) { | ||
// TODO: Create a custom exception to be handed in the controller | ||
throw new \DomainException('User pocket currency does not match gift currency'); | ||
} | ||
|
||
$userMoney = new Money($userPocket->balance, new Currency($userPocket->currency)); /* @phpstan-ignore-line */ | ||
$giftMoney = new Money($data->amount, new Currency($data->currency)); /* @phpstan-ignore-line */ | ||
|
||
$userPocket->balance = (int) $userMoney->add($giftMoney)->getAmount(); | ||
$userPocket->update(); | ||
|
||
$gift = new Gift(); | ||
$gift->note = $data->note; | ||
$gift->amount = $data->amount; | ||
$gift->currency = $data->currency; | ||
$gift->senderUser()->associate($senderUser); | ||
$gift->user()->associate($user); | ||
$gift->save(); | ||
|
||
return $gift; | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace Domain\Gifts\Data; | ||
|
||
use Spatie\LaravelData\Data; | ||
|
||
class StoreGiftData extends Data | ||
{ | ||
public function __construct( | ||
public ?string $note, | ||
public int $amount, | ||
public string $currency, | ||
) { | ||
} | ||
|
||
public static function rules(): array | ||
{ | ||
return [ | ||
'note' => ['required', 'string', 'min:3', 'max:255'], | ||
// TODO: Add a logic about payment method, where the money is supposed to come from | ||
'amount' => ['required', 'integer', 'min:100'], | ||
// TODO: Add validated currency rule | ||
'currency' => ['required', 'string'], | ||
]; | ||
} | ||
} |
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,57 @@ | ||
<?php | ||
|
||
namespace Domain\Gifts\Models; | ||
|
||
use Database\Factories\DBGiftFactory; | ||
use Domain\Quotes\QueryBuilders\QuoteQueryBuilder; | ||
use Domain\Users\Models\User; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Support\Carbon; | ||
|
||
/** | ||
* @property-read int $id | ||
* | ||
* @property ?string $note | ||
* @property int $amount | ||
* @property string $currency | ||
* @property int $user_id | ||
* @property int $sender_user_id | ||
* @property Carbon $created_at | ||
* @property Carbon $updated_at | ||
* | ||
* @property User $user | ||
* @property User $senderUser | ||
* | ||
* @method static DBGiftFactory factory(...$parameters) | ||
* @method static QuoteQueryBuilder query() | ||
*/ | ||
class Gift extends Model | ||
{ | ||
use HasFactory; | ||
|
||
public function newEloquentBuilder($query): QuoteQueryBuilder | ||
{ | ||
return new QuoteQueryBuilder($query); | ||
} | ||
|
||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
|
||
public function senderUser(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class, 'sender_user_id'); | ||
} | ||
|
||
/** | ||
* Create a new factory instance for the model. | ||
*/ | ||
protected static function newFactory(): Factory | ||
{ | ||
return DBGiftFactory::new(); | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace Domain\Gifts\QueryBuilders; | ||
|
||
use Domain\Gifts\Models\Gift; | ||
use Domain\Users\Models\User; | ||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
/** | ||
* @method select($columns = ['*']) | ||
* @method count() | ||
* @method Gift firstOrFail($columns = ['*']) | ||
*/ | ||
class GiftQueryBuilder extends Builder | ||
{ | ||
public function whereId(int $id): self | ||
{ | ||
return $this->where('id', $id); | ||
} | ||
|
||
public function whereUser(User $user): self | ||
{ | ||
return $this->where('user_id', $user->getKey()); | ||
} | ||
|
||
public function whereSenderUser(User $user): self | ||
{ | ||
return $this->where('sender_user_id', $user->getKey()); | ||
} | ||
} |
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
Oops, something went wrong.