Skip to content

Commit 68fd546

Browse files
authored
Merge pull request #40 from Kalgoc/develop
Hotfix: Release
2 parents 18231f0 + ed164f8 commit 68fd546

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

budget/views.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ def list(self, request):
1212
try:
1313
user_id = get_user_id_from_token(request)
1414
budget = Budget.objects.filter(username=user_id).order_by("-created_at").first()
15+
1516
if not budget:
16-
return Response({"error": "Budget not found"}, status=status.HTTP_404_NOT_FOUND)
17+
return Response({"amount": None})
18+
1719
serializer = BudgetSerializer(budget)
20+
1821
return Response(data={"amount": serializer.data["amount"]})
1922
except Exception as e:
2023
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@@ -27,9 +30,12 @@ def create(self, request):
2730
data["username"] = user_id
2831

2932
serializer = BudgetSerializer(data=data)
33+
3034
if serializer.is_valid():
3135
serializer.save()
36+
3237
return Response(data={"amount": serializer.data["amount"]}, status=status.HTTP_201_CREATED)
38+
3339
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
3440
except Exception as e:
3541
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@@ -39,9 +45,12 @@ def destroy(self, request):
3945
try:
4046
user_id = get_user_id_from_token(request)
4147
budget = Budget.objects.filter(username=user_id).order_by("-created_at").first()
48+
4249
if not budget:
4350
return Response({"error": "Budget not found"}, status=status.HTTP_404_NOT_FOUND)
51+
4452
budget.delete()
53+
4554
return Response(status=status.HTTP_204_NO_CONTENT)
4655
except Exception as e:
4756
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@@ -51,12 +60,26 @@ def partial_update(self, request):
5160
try:
5261
user_id = get_user_id_from_token(request)
5362
budget = Budget.objects.filter(username=user_id).order_by("-created_at").first()
63+
5464
if not budget:
65+
data = request.data.copy()
66+
data["username"] = user_id
67+
serializer = BudgetSerializer(data=data)
68+
69+
if serializer.is_valid():
70+
serializer.save()
71+
72+
return Response(data={"amount": serializer.data["amount"]})
73+
5574
return Response({"error": "Budget not found"}, status=status.HTTP_404_NOT_FOUND)
75+
5676
serializer = BudgetSerializer(budget, data=request.data, partial=True)
77+
5778
if serializer.is_valid():
5879
serializer.save()
80+
5981
return Response(data={"amount": serializer.data["amount"]})
82+
6083
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
6184
except Exception as e:
6285
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

0 commit comments

Comments
 (0)