Skip to content

Commit 51cea5b

Browse files
authored
Merge pull request #11 from btaquee/trac
transactions
2 parents 86a6daa + fa0dadb commit 51cea5b

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

transactions/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
from django.contrib import admin
2+
from .models import Transaction
3+
4+
admin.site.register(Transaction)
25

36
# Register your models here.

transactions/models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
from django.db import models
2+
from django.conf import settings
3+
from cards.models import Card, RewardRule
24

35
# Create your models here.
6+
7+
8+
class Transaction(models.Model):
9+
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="transactions")
10+
card = models.ForeignKey(Card, on_delete=models.CASCADE, related_name="transactions")
11+
merchant = models.CharField(max_length=255)
12+
amount = models.DecimalField("Amount ($)", max_digits=10, decimal_places=2)
13+
category = models.CharField(max_length=255, choices=RewardRule.CATEGORY_CHOICES)
14+
created_at = models.DateTimeField(auto_now_add=True)
15+
updated_at = models.DateTimeField(auto_now=True)
16+
notes = models.TextField(blank=True, null=True)
17+
18+
def __str__(self):
19+
return f"{self.merchant} - ${self.amount} ({self.user.username})"

transactions/serializers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from rest_framework import serializers
2+
from .models import Transaction
3+
4+
class TransactionSerializer(serializers.ModelSerializer):
5+
class Meta:
6+
model = Transaction
7+
fields = ("id", "card", "merchant", "amount", "category", "created_at", "updated_at", "notes")
8+
read_only_fields = ("id", "user", "created_at", "updated_at")

transactions/urls.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
from django.urls import path
1+
from django.urls import path, include
22
from . import views
3+
from rest_framework.routers import DefaultRouter
4+
from .views import TransactionViewSet
5+
6+
router = DefaultRouter()
7+
router.register(r'transactions', TransactionViewSet, basename='transactions')
38

49
urlpatterns = [
10+
path('', include(router.urls)),
511
path('health/', views.HealthCheckView.as_view(), name='health'),
612
]

transactions/views.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,28 @@
22
from rest_framework.views import APIView
33
from rest_framework.response import Response
44
from rest_framework import status
5+
from .models import Transaction
6+
from .serializers import TransactionSerializer
7+
from rest_framework import viewsets, permissions
58

69
# Create your views here.
710
# Check API health
811
class HealthCheckView(APIView):
912
def get(self, request):
10-
return Response({"status": "ok"}, status=status.HTTP_200_OK)
13+
return Response({"status": "ok"}, status=status.HTTP_200_OK)
14+
15+
16+
class TransactionViewSet(viewsets.ModelViewSet):
17+
serializer_class = TransactionSerializer
18+
permission_classes = [permissions.IsAuthenticated]
19+
20+
# AI help me understand the concept of two functions below, what is does in detail
21+
22+
# Records the API should return when a user makes a GET request
23+
# No filter. Will return all transactions for that user, sorted by created_at in descending order
24+
def get_queryset(self):
25+
return Transaction.objects.filter(user=self.request.user).select_related('card', 'card__issuer').order_by("-created_at")
26+
27+
# Saves the transaction to the database
28+
def perform_create(self, serializer):
29+
serializer.save(user=self.request.user)

0 commit comments

Comments
 (0)