From 4271562f6e621ce7ee68d202f1422e71c94840f1 Mon Sep 17 00:00:00 2001 From: WellingtonLFaria Date: Mon, 23 Sep 2024 17:28:48 -0300 Subject: [PATCH] corr: Alterando o funcionamento do metodo put do endpoint /usuarios/ --- src/tupan/usuarios/views.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/tupan/usuarios/views.py b/src/tupan/usuarios/views.py index c0e1d48..d578b62 100644 --- a/src/tupan/usuarios/views.py +++ b/src/tupan/usuarios/views.py @@ -34,12 +34,16 @@ def post(self, request, format=None): return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - def update(self, request, format=None): - serializer = UsuarioSerializer(data=request.data) - if serializer.is_valid(): - user = Usuario.get(id=serializer.data.id) - user.email = serializer.data.email - user.password = serializer.data.password + def put(self, request, format=None): + try: + new_data = request.data + user = Usuario.objects.get(id=new_data["id"]) + user.email = new_data["email"] + user.password = new_data["password"] user.save() - return Response(user, status=status.HTTP_200_OK) - return Response("", status=status.HTTP_400_BAD_REQUEST) \ No newline at end of file + serializer = UsuarioSerializer(user) + return Response(serializer.data, status=status.HTTP_200_OK) + except Usuario.DoesNotExist: + return Response(status=status.HTTP_404_NOT_FOUND) + except Exception as e: + return Response(str(e), status=status.HTTP_400_BAD_REQUEST) \ No newline at end of file