Created
October 27, 2020 21:54
-
-
Save pedrohsbarbosa99/f8e8e61c05aec458410529133f39df8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Categoria(models.Model): | |
nome = models.CharField(max_length=100) | |
class Produtos(models.Model): | |
prod_nome = models.CharField(max_length=250) | |
qtd_estoque= models.IntegerField() | |
preco = models.DecimalField(max_digits=8, decimal_places=2) | |
categoria = models.ForeignKey(Categoria, on_delete=models.CASCADE, default=None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import url,include | |
from django.contrib import admin | |
urlpatterns = [ | |
url('admin/', admin.site.urls), | |
url('estoque/', include('ml_estoque.urls')), | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework.views import APIView | |
from rest_framework import status | |
from rest_framework.response import Response | |
from ml_estoque.models import Produtos | |
from ml_estoque.serializers import EstoqueSerializer | |
from rest_framework.exceptions import NotFound | |
class EstoqueList_create(APIView): | |
def get(self, request): | |
estoque = Produtos.objects.all() | |
serializer = EstoqueSerializer(estoque, many=True) | |
return Response(serializer.data) | |
def post(self, request): | |
serializer = EstoqueSerializer(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) | |
class EstoqueList_ChangeAndDelete(APIView): | |
def get_objects(self, pk): | |
try: | |
return Produtos.objects.get(pk=pk) | |
except Produtos.DoesNotExist: | |
raise NotFound() | |
def get(self, request, pk): | |
Produtos = self.get_objects(pk) | |
serializer = EstoqueSerializer(Produtos) | |
return Response(serializer.data) | |
def put(self, request, pk): | |
Produtos = self.get_objects(pk) | |
serializer = EstoqueSerializer(Produtos, data=request.data) | |
if serializer.is_valid(): | |
serializer.save() | |
return Response(serializer.data) | |
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | |
def delete(self, request, pk): | |
Produtos = self.get_objects(pk) | |
Produtos.delete() | |
return Response(status=status.HTTP_204_NO_CONTENT) | |
class CategoriaList(APIView): | |
def post(self, request): | |
serializer = CategoriaProdSerializer(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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment