Skip to content

Commit bf04aad

Browse files
committed
fix: change user_expense_type url and added new endpoint retrieve by id
1 parent 8d70c5e commit bf04aad

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

Diff for: piggywallet/urls.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
path("auth/", include("authentication.urls")),
2424
path("playground/", include("playground.urls")),
2525
path("budget/", include("budget.urls")),
26-
path("expense-types/", include("user_expense_type.urls")),
26+
path("user_expense_type/", include("user_expense_type.urls")),
2727
]

Diff for: user_expense_type/tests.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ def test_create(self, mock_login_user, mock_create):
2525
}
2626
mock_create.return_value = Response(status=status.HTTP_201_CREATED, data=self.user_expense_type_data)
2727

28-
request = self.client.post("/expense-types/", self.user_expense_type_data)
28+
request = self.client.post("/user_expense_type/", self.user_expense_type_data)
2929
request.headers["Authorization"] = "Bearer mock_access_token"
3030

31-
response = self.client.post("/expense-types/", self.user_expense_type_data)
31+
response = self.client.post("/user_expense_type/", self.user_expense_type_data)
3232

3333
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
3434
self.assertEqual(response.data["name"], "Viajes")
@@ -41,10 +41,10 @@ def test_list(self, mock_login_user, mock_list):
4141
}
4242
mock_list.return_value = Response(status=status.HTTP_200_OK, data=self.user_expense_type_data)
4343

44-
request = self.client.get("/expense-types/")
44+
request = self.client.get("/user_expense_type/")
4545
request.headers["Authorization"] = "Bearer mock_access_token"
4646

47-
response = self.client.get("/expense-types/")
47+
response = self.client.get("/user_expense_type/")
4848

4949
self.assertEqual(response.status_code, status.HTTP_200_OK)
5050
self.assertEqual(response.data["name"], "Viajes")
@@ -57,11 +57,10 @@ def test_destroy(self, mock_login_user, mock_destroy):
5757
}
5858
mock_destroy.return_value = Response(status=status.HTTP_204_NO_CONTENT)
5959

60-
request = self.client.delete("/expense-types/")
60+
request = self.client.delete("/user_expense_type/1/")
6161
request.headers["Authorization"] = "Bearer mock_access_token"
62-
request.body = {"id": 1}
6362

64-
response = self.client.delete("/expense-types/")
63+
response = self.client.delete("/user_expense_type/1/")
6564

6665
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
6766

@@ -78,11 +77,10 @@ def test_partial_update(self, mock_login_user, mock_partial_update):
7877
}
7978
mock_partial_update.return_value = Response(status=status.HTTP_200_OK, data=new_user_expense_type_data)
8079

81-
request = self.client.put("/expense-type/", {"description": "Gastos incurridos en viajes por placer"})
80+
request = self.client.put("/user_expense_type/1/", {"description": "Gastos incurridos en viajes por placer"})
8281
request.headers["Authorization"] = "Bearer mock_access_token"
83-
request.body = {"id": 1}
8482

85-
response = self.client.put("/expense-types/", {"description": "Gastos incurridos en viajes por placer"})
83+
response = self.client.put("/user_expense_type/1/", {"description": "Gastos incurridos en viajes por placer"})
8684

8785
self.assertEqual(response.status_code, status.HTTP_200_OK)
8886
self.assertEqual(response.data["description"], "Gastos incurridos en viajes por placer")

Diff for: user_expense_type/urls.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
urlpatterns = [
66
path(
77
"",
8-
UserExpenseTypeViewSet.as_view({"get": "list", "post": "create", "delete": "destroy", "put": "partial_update"}),
8+
UserExpenseTypeViewSet.as_view({"get": "list", "post": "create"}),
99
name="user_expense_type",
1010
),
11+
path(
12+
"<int:pk>/",
13+
UserExpenseTypeViewSet.as_view(
14+
{"get": "retrieve", "delete": "destroy", "put": "partial_update"}
15+
),
16+
name="user_expense_type_detail",
17+
),
1118
]

Diff for: user_expense_type/views.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ def list(self, request):
4141
return Response({"error": "UserExpenseType not found"}, status=status.HTTP_404_NOT_FOUND)
4242
except Exception as e:
4343
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
44+
45+
@cognito_authenticated
46+
def retrieve(self, request, pk=None):
47+
try:
48+
username = self.get_user_id_from_token(request)
49+
user_expense_type = UserExpenseType.objects.get(id=pk, username=username)
50+
serializer = UserExpenseTypeSerializer(user_expense_type)
51+
response = {
52+
"name": serializer.data["name"],
53+
"description": serializer.data["description"],
54+
"set_by_user": serializer.data["set_by_user"],
55+
}
56+
return Response(data=response)
57+
except UserExpenseType.DoesNotExist:
58+
return Response({"error": "UserExpenseType not found"}, status=status.HTTP_404_NOT_FOUND)
59+
except Exception as e:
60+
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
4461

4562
@cognito_authenticated
4663
def create(self, request):
@@ -63,11 +80,10 @@ def create(self, request):
6380
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
6481

6582
@cognito_authenticated
66-
def destroy(self, request):
83+
def destroy(self, request, pk=None ):
6784
try:
6885
username = self.get_user_id_from_token(request)
69-
id = request.data.get("id")
70-
user_expense_type = UserExpenseType.objects.get(id=id, username=username)
86+
user_expense_type = UserExpenseType.objects.get(id=pk, username=username)
7187
user_expense_type.delete()
7288
return Response(status=status.HTTP_204_NO_CONTENT)
7389
except UserExpenseType.DoesNotExist:
@@ -76,11 +92,10 @@ def destroy(self, request):
7692
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
7793

7894
@cognito_authenticated
79-
def partial_update(self, request):
95+
def partial_update(self, request, pk=None):
8096
try:
8197
username = self.get_user_id_from_token(request)
82-
id = request.data.get("id")
83-
user_expense_type = UserExpenseType.objects.get(id=id, username=username)
98+
user_expense_type = UserExpenseType.objects.get(id=pk, username=username)
8499
serializer = UserExpenseTypeSerializer(user_expense_type, data=request.data, partial=True)
85100
if serializer.is_valid():
86101
serializer.save()

0 commit comments

Comments
 (0)