Skip to content

Commit

Permalink
corr: url detalhes alerta
Browse files Browse the repository at this point in the history
  • Loading branch information
iagocpv committed Oct 11, 2024
1 parent 033b25e commit 17343bf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/tupan/alertas/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

urlpatterns = [
path('', AlertasView.as_view(), name='alertas'),
path('<int:id>', AlertasDetalhesView.as_view(), name='alertas-detalhes'),
path('/<int:id>', AlertasDetalhesView.as_view(), name='alertas-detalhes'),
path('historicos', HistoricoAlertaView.as_view(), name='historico-alertas'),
path('medicoes', MedicaoView.as_view(), name='medicoes'),
path('medicoes/<int:id>', MedicaoDetalhesView.as_view(), name='medicoes-detalhes')
Expand Down
16 changes: 5 additions & 11 deletions src/tupan/alertas/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def post(self, request, *args, **kwargs):
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

except json.JSONDecodeError:
return JsonResponse({'error': 'Dados inválidos'}, status=400)

Expand All @@ -77,18 +76,13 @@ def get(self, request, id, *args, **kwargs):

def put(self, request, id, *args, **kwargs):
try:
data = json.loads(request.body)
alerta = Alerta.objects.filter(id=id, ativo=True).first()
if alerta:
alerta.nome = data.get('nome', alerta.nome)
alerta.condicao = data.get('condicao', alerta.condicao)
alerta.save()
return JsonResponse({
'id': alerta.pk,
'nome': alerta.nome,
'condicao': alerta.condicao,
'ativo': alerta.ativo
}, status=200)
serializer = AlertaSerializer(alerta, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
return JsonResponse({'error': 'Alerta não encontrado ou inativo'}, status=404)
except json.JSONDecodeError:
Expand Down

0 comments on commit 17343bf

Please sign in to comment.