generated from UnBArqDsw2023-2/RepositoryTemplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adicionado padrao observer para adicionar promocao de produtos
- Loading branch information
1 parent
36823b3
commit a3e8c03
Showing
3 changed files
with
38 additions
and
9 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,31 @@ | ||
from django.db import models | ||
from abstract_factory import Base | ||
|
||
|
||
class Produto(Base): | ||
categoria = models.CharField(max_length=255) | ||
nome = models.CharField(max_length=255) | ||
tipo = models.CharField(max_length=255) | ||
preco = models.DecimalField(max_digits=10, decimal_places=2) | ||
desconto = models.FloatField() | ||
|
||
def adicionar_observer(self, observer): | ||
if not hasattr(self, '_observers'): | ||
self._observers = [] | ||
self._observers.append(observer) | ||
|
||
def remover_observer(self, observer): | ||
if hasattr(self, '_observers'): | ||
self._observers.remove(observer) | ||
|
||
def notificar_observers(self, **kwargs): | ||
if hasattr(self, '_observers'): | ||
for observer in self._observers: | ||
observer.update(self, **kwargs) | ||
|
||
def adicionar_promocao(self, nova_promocao): | ||
# Lógica para adicionar uma nova promoção ao produto | ||
print("Adicionar promoção chamado") | ||
self.notificar_observers(nova_promocao=nova_promocao) | ||
|
||
class Meta: | ||
db_table = 'produto' | ||
db_table = 'produto' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
from rest_framework import viewsets | ||
from rest_framework import viewsets, status | ||
from rest_framework.response import Response | ||
from americanas.models import Produto | ||
from americanas.serializers import ProdutoSerializers | ||
|
||
from observer.ProdutoObserver import PromocaoObserver | ||
|
||
class ProdutoViewSets(viewsets.ModelViewSet): | ||
serializer_class = ProdutoSerializers | ||
queryset = Produto.objects.all() | ||
queryset = Produto.objects.all() | ||
|
||
def perform_create(self, serializer): | ||
print("Perform create chamado") | ||
produto_instance = serializer.save() | ||
promocao_observer = PromocaoObserver() | ||
produto_instance.adicionar_promocao(nova_promocao=promocao_observer) | ||
promocao_observer.notificar_promocao(produto_instance) | ||
|
||
return Response(serializer.data, status=status.HTTP_201_CREATED) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
from observer import Observer | ||
|
||
|
||
class PromocaoObserver(Observer): | ||
def update(self, subject, *args, **kwargs): | ||
if "nova_promocao" in kwargs: | ||
nova_promocao = kwargs["nova_promocao"] | ||
self.notificar_promocao(nova_promocao) | ||
print("Update chamado") | ||
self.notificar_promocao(subject) | ||
|
||
def notificar_promocao(self, promocao): | ||
print("Notificar promoção chamado") | ||
# Lógica para notificar sobre uma nova promoção | ||
print(f"Nova promoção! {promocao.nome} com desconto de {promocao.desconto}%") | ||
x |