Skip to content

Commit

Permalink
feat: add currency field in balance model and modify balanceview endp…
Browse files Browse the repository at this point in the history
…oint to return balance with currency.
  • Loading branch information
sandronadiradze committed Nov 4, 2024
1 parent 01d6cae commit dd464db
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
22 changes: 22 additions & 0 deletions payments/migrations/0003_balance_currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 5.0.4 on 2024-11-04 17:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("payments", "0002_balance"),
]

operations = [
migrations.AddField(
model_name="balance",
name="currency",
field=models.CharField(
choices=[("GEL", "GEL"), ("USD", "USD"), ("EUR", "EUR")],
default="GEL",
max_length=3,
),
),
]
9 changes: 9 additions & 0 deletions payments/models/balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
from django.db import models, transaction


class CurrencyChoices(models.TextChoices):
GEL = "GEL", "GEL"
USD = "USD", "USD"
EUR = "EUR", "EUR"


class Balance(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="balance"
)
amount = models.DecimalField(max_digits=10, decimal_places=2, default=0.0)
currency = models.CharField(
max_length=3, choices=CurrencyChoices.choices, default=CurrencyChoices.GEL
)

def add(self, amount):
"""Add money to the balance."""
Expand Down
2 changes: 1 addition & 1 deletion payments/openapi/balance_openapi_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def balance_view_examples():
"Response example (Balance Retrieved)",
summary="Successful balance retrieval",
description="This example demonstrates the successful response containing the user's balance.",
value={"balance": 100.00},
value={"balance": 100.00, "currency": "GEL"},
response_only=True,
status_codes=[200],
),
Expand Down
5 changes: 4 additions & 1 deletion payments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ def get(self, request, user_id):
user=user, defaults={"amount": 0}
)

return Response({"balance": balance.amount}, status=status.HTTP_200_OK)
return Response(
{"balance": balance.amount, "currency": balance.currency},
status=status.HTTP_200_OK,
)


@extend_schema(
Expand Down

0 comments on commit dd464db

Please sign in to comment.