Skip to content

Commit

Permalink
chore: add patch / partial update method for transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
MustafaMunir123 committed Nov 1, 2023
1 parent cc3893d commit 4a3d834
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apps/transactions/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def create(self, validated_data):
return Transaction.objects.create(**validated_data)

def update(self, instance, validated_data):
transaction = Transaction.objects.filter(id=instance.id).update(
transaction = Transaction.objects.filter(entry_no=instance.entry_no).update(
**validated_data
)
return transaction
Expand Down
1 change: 1 addition & 0 deletions apps/transactions/api/v1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
path("account/all/", AccountAPIView.as_view()),
url("", include(router.urls)),
path("create/", TransactionsAPIView.as_view()),
path("update/", TransactionsAPIView.as_view()),
path("all/details/", TransactionsAPIView.as_view()),
path("<int:pk>/details/", TransactionsAPIView.as_view()),
path("export/all/", ExportAPIView.as_view()),
Expand Down
12 changes: 12 additions & 0 deletions apps/transactions/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ def get(self, request, pk=None):
except Exception as ex:
raise ex

def patch(self, request):
transactions = request.data.get('transactions', [])
try:
for transaction in transactions:
transaction_object = Transaction.objects.get(entry_no=transaction.pop("entry_no"))
serializer = TransactionSerializer(transaction_object, data=transaction, partial=True)
serializer.is_valid(raise_exception=True)
serializer.save()
return success_response(data={"message": "updated successfully"}, status=status.HTTP_200_OK)
except Exception as ex:
raise ex


class ExportAPIView(APIView):
permission_classes = [IsAuthenticated]
Expand Down
2 changes: 0 additions & 2 deletions apps/transactions/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from importlib._common import _

from django.db import models
from rest_framework.exceptions import ValidationError

Expand Down
Binary file modified db.sqlite3
Binary file not shown.

0 comments on commit 4a3d834

Please sign in to comment.