Skip to content

Commit

Permalink
Merge pull request #14 from Grupo-Syntax-Squad/refa/parametros-12
Browse files Browse the repository at this point in the history
Refa/parametros 12
  • Loading branch information
WellingtonLFaria authored Sep 27, 2024
2 parents c6e7bd2 + 784ddca commit 158ccdc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 65 deletions.
4 changes: 2 additions & 2 deletions src/tupan/estacoes/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.1 on 2024-09-23 13:45
# Generated by Django 5.1.1 on 2024-09-26 22:15

import django.db.models.deletion
from django.db import migrations, models
Expand Down Expand Up @@ -59,7 +59,7 @@ class Migration(migrations.Migration):
('nome', models.CharField(max_length=127)),
('topico', models.CharField(help_text='Tópico do broker MQTT', max_length=127)),
('ativo', models.BooleanField(default=True)),
('endereco', models.OneToOneField(null=True, on_delete=django.db.models.deletion.SET_NULL, to='estacoes.endereco')),
('endereco', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='estacoes.endereco')),
('parametros', models.ManyToManyField(blank=True, to='estacoes.parametro')),
],
options={
Expand Down
2 changes: 1 addition & 1 deletion src/tupan/estacoes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __str__(self):

class Estacao(Base):
nome = models.CharField(max_length=127)
endereco = models.OneToOneField(Endereco, null=True, on_delete=models.SET_NULL)
endereco = models.OneToOneField(Endereco, on_delete=models.CASCADE)
topico = models.CharField(help_text="Tópico do broker MQTT", max_length=127)
ativo = models.BooleanField(default=True)
parametros = models.ManyToManyField(Parametro, blank=True)
Expand Down
14 changes: 7 additions & 7 deletions src/tupan/estacoes/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django.urls import path
from .views import estacao_list, estacao_details, parametro_list, parametro_details, endereco_list, endereco_details
from .views import EstacoesView, EstacoesDetalhesView, EnderecosView, EnderecosDetalhesView, ParametrosView, ParametrosDetalhesView

urlpatterns = [
path("estacoes", estacao_list, name="Estações"),
path("estacoes/<int:pk>", estacao_details, name="Estação"),
path("enderecos", endereco_list, name="Endereços"),
path("enderecos/<int:pk>", endereco_details, name="Endereço"),
path("parametros", parametro_list, name="Parâmetros"),
path("parametros/<int:pk>", parametro_details, name="parâmetro")
path("estacoes", EstacoesView.as_view(), name="Estações"),
path("estacoes/<int:pk>", EstacoesDetalhesView.as_view(), name="Estação"),
path("enderecos", EnderecosView.as_view(), name="Endereços"),
path("enderecos/<int:pk>", EnderecosDetalhesView.as_view(), name="Endereço"),
path("parametros", ParametrosView.as_view(), name="Parâmetros"),
path("parametros/<int:pk>", ParametrosDetalhesView.as_view(), name="Parâmetro")
]
120 changes: 65 additions & 55 deletions src/tupan/estacoes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,112 +2,122 @@
from .serializers import EstacaoSerializer, EnderecoSerializer, ParametroSerializer
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.views import APIView

@api_view(['GET', 'POST'])
def estacao_list(request):
if request.method == 'GET':
class EstacoesView(APIView):
def get(self, request, *args, **kwargs):
ativo = request.query_params.get('ativo')
estacoes = Estacao.objects.all()
if (ativo):
estacoes = estacoes.filter(ativo=ativo)
serializer = EstacaoSerializer(estacoes, many=True)
return Response(serializer.data)
elif request.method == 'POST':
def post(self, request, *args, **kwargs):
serializer = EstacaoSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET', 'PUT', 'DELETE'])
def estacao_details(request, pk):
try:
estacao = Estacao.objects.get(pk=pk)
except Estacao.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

if request.method == 'GET':

class EstacoesDetalhesView(APIView):
def get(self, request, pk, *args, **kwargs):
try:
estacao = Estacao.objects.get(pk=pk)
except Estacao.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = EstacaoSerializer(estacao)
return Response(serializer.data)

elif request.method == 'PUT':
def put(self, request, pk, *args, **kwargs):
try:
estacao = Estacao.objects.get(pk=pk)
except Estacao.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = EstacaoSerializer(estacao, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

elif request.method == 'DELETE':
estacao.delete()
def delete(self, request, pk, *args, **kwargs):
try:
estacao = Estacao.objects.get(pk=pk)
except Estacao.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
estacao.ativo = False
estacao.save()
return Response(status=status.HTTP_204_NO_CONTENT)

@api_view(['GET', 'POST'])
def endereco_list(request):
if request.method == 'GET':
class EnderecosView(APIView):
def get(self, request, *args, **kwargs):
enderecos = Endereco.objects.all()
serializer = EnderecoSerializer(enderecos, many=True)
return Response(serializer.data)
elif request.method == 'POST':
def post(self, request, *args, **kwargs):
serializer = EnderecoSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET', 'PUT', 'DELETE'])
def endereco_details(request, pk):
try:
endereco = Endereco.objects.get(pk=pk)
except Endereco.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

if request.method == 'GET':
class EnderecosDetalhesView(APIView):
def get(self, request, pk, *args, **kwargs):
try:
endereco = Endereco.objects.get(pk=pk)
except Endereco.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = EnderecoSerializer(endereco)
return Response(serializer.data)

elif request.method == 'PUT':
def put(self, request, pk, *args, **kwargs):
try:
endereco = Endereco.objects.get(pk=pk)
except Endereco.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = EnderecoSerializer(endereco, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

elif request.method == 'DELETE':
def delete(self, request, pk, *args, **kwargs):
try:
endereco = Endereco.objects.get(pk=pk)
except Endereco.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
endereco.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_204_NO_CONTENT)

@api_view(['GET', 'POST'])
def parametro_list(request):
if request.method == 'GET':
class ParametrosView(APIView):
def get(self, request, *args, **kwargs):
parametros = Parametro.objects.all()
serializer = ParametroSerializer(parametros, many=True)
return Response(serializer.data)
elif request.method == 'POST':
def post (self, request, *args, **kwargs):
serializer = ParametroSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET', 'PUT', 'DELETE'])
def parametro_details(request, pk):
try:
parametro = Parametro.objects.get(pk=pk)
except Parametro.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

if request.method == 'GET':
class ParametrosDetalhesView(APIView):
def get(self, request, pk, *args, **kwargs):
try:
parametro = Parametro.objects.get(pk=pk)
except Parametro.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = ParametroSerializer(parametro)
return Response(serializer.data)

elif request.method == 'PUT':
def put(self, request, pk, *args, **kwargs):
try:
parametro = Parametro.objects.get(pk=pk)
except Parametro.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = ParametroSerializer(parametro, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

elif request.method == 'DELETE':
def delete(self, request, pk, *args, **kwargs):
try:
parametro = Parametro.objects.get(pk=pk)
except Parametro.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
parametro.delete()
return Response(status=status.HTTP_204_NO_CONTENT)



0 comments on commit 158ccdc

Please sign in to comment.