@@ -12,9 +12,12 @@ def list(self, request):
12
12
try :
13
13
user_id = get_user_id_from_token (request )
14
14
budget = Budget .objects .filter (username = user_id ).order_by ("-created_at" ).first ()
15
+
15
16
if not budget :
16
- return Response ({"error" : "Budget not found" }, status = status .HTTP_404_NOT_FOUND )
17
+ return Response ({"amount" : None })
18
+
17
19
serializer = BudgetSerializer (budget )
20
+
18
21
return Response (data = {"amount" : serializer .data ["amount" ]})
19
22
except Exception as e :
20
23
return Response ({"error" : str (e )}, status = status .HTTP_500_INTERNAL_SERVER_ERROR )
@@ -27,9 +30,12 @@ def create(self, request):
27
30
data ["username" ] = user_id
28
31
29
32
serializer = BudgetSerializer (data = data )
33
+
30
34
if serializer .is_valid ():
31
35
serializer .save ()
36
+
32
37
return Response (data = {"amount" : serializer .data ["amount" ]}, status = status .HTTP_201_CREATED )
38
+
33
39
return Response (serializer .errors , status = status .HTTP_400_BAD_REQUEST )
34
40
except Exception as e :
35
41
return Response ({"error" : str (e )}, status = status .HTTP_500_INTERNAL_SERVER_ERROR )
@@ -39,9 +45,12 @@ def destroy(self, request):
39
45
try :
40
46
user_id = get_user_id_from_token (request )
41
47
budget = Budget .objects .filter (username = user_id ).order_by ("-created_at" ).first ()
48
+
42
49
if not budget :
43
50
return Response ({"error" : "Budget not found" }, status = status .HTTP_404_NOT_FOUND )
51
+
44
52
budget .delete ()
53
+
45
54
return Response (status = status .HTTP_204_NO_CONTENT )
46
55
except Exception as e :
47
56
return Response ({"error" : str (e )}, status = status .HTTP_500_INTERNAL_SERVER_ERROR )
@@ -51,12 +60,26 @@ def partial_update(self, request):
51
60
try :
52
61
user_id = get_user_id_from_token (request )
53
62
budget = Budget .objects .filter (username = user_id ).order_by ("-created_at" ).first ()
63
+
54
64
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
+
55
74
return Response ({"error" : "Budget not found" }, status = status .HTTP_404_NOT_FOUND )
75
+
56
76
serializer = BudgetSerializer (budget , data = request .data , partial = True )
77
+
57
78
if serializer .is_valid ():
58
79
serializer .save ()
80
+
59
81
return Response (data = {"amount" : serializer .data ["amount" ]})
82
+
60
83
return Response (serializer .errors , status = status .HTTP_400_BAD_REQUEST )
61
84
except Exception as e :
62
85
return Response ({"error" : str (e )}, status = status .HTTP_500_INTERNAL_SERVER_ERROR )
0 commit comments