Skip to content

Commit 7910d3f

Browse files
authored
Merge pull request #76 from DeliveryAPP-Project/feature/SCRUM-79-refatoracao-tabela-estabelecimentos
Feature/scrum 79 refatoracao tabela estabelecimentos
2 parents a8de24c + 4e4c5e5 commit 7910d3f

25 files changed

+437
-398
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# DeliveryAPP Backend
22

3-
Este é o backend do projeto DeliveryAPP. Ele é construído usando Flask e fornece uma API RESTful para gerenciar usuários, clientes, restaurantes, produtos e pedidos.
3+
Este é o backend do projeto DeliveryAPP. Ele é construído usando Flask e fornece uma API RESTful para gerenciar usuários, clientes, estabelecimentos, produtos e pedidos.
44

55
## Estrutura do Projeto
66

migrations/versions/7bc0658e9c3d_init_migrate.py renamed to migrations/versions/9255441d2a22_.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
"""init migrate
1+
"""empty message
22
3-
Revision ID: 7bc0658e9c3d
3+
Revision ID: 9255441d2a22
44
Revises:
5-
Create Date: 2025-03-23 13:21:15.057776
5+
Create Date: 2025-03-28 14:45:25.748190
66
77
"""
88
from alembic import op
99
import sqlalchemy as sa
1010

1111

1212
# revision identifiers, used by Alembic.
13-
revision = '7bc0658e9c3d'
13+
revision = '9255441d2a22'
1414
down_revision = None
1515
branch_labels = None
1616
depends_on = None
@@ -34,6 +34,22 @@ def upgrade():
3434
with op.batch_alter_table('client', schema=None) as batch_op:
3535
batch_op.create_index(batch_op.f('ix_client_email'), ['email'], unique=False)
3636

37+
op.create_table('establishment',
38+
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
39+
sa.Column('official_name', sa.String(length=40), nullable=False),
40+
sa.Column('fantasy_name', sa.String(length=40), nullable=False),
41+
sa.Column('cnpj', sa.String(length=14), nullable=False),
42+
sa.Column('telephone', sa.String(length=11), nullable=False),
43+
sa.Column('zip_code', sa.String(length=9), nullable=False),
44+
sa.Column('state', sa.String(length=19), nullable=False),
45+
sa.Column('city', sa.String(length=30), nullable=False),
46+
sa.Column('address', sa.String(length=120), nullable=False),
47+
sa.Column('complement', sa.String(length=120), nullable=True),
48+
sa.PrimaryKeyConstraint('id'),
49+
sa.UniqueConstraint('cnpj'),
50+
sa.UniqueConstraint('fantasy_name'),
51+
sa.UniqueConstraint('official_name')
52+
)
3753
op.create_table('lead',
3854
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
3955
sa.Column('created_at', sa.DateTime(), nullable=True),
@@ -43,19 +59,6 @@ def upgrade():
4359
with op.batch_alter_table('lead', schema=None) as batch_op:
4460
batch_op.create_index(batch_op.f('ix_lead_email'), ['email'], unique=True)
4561

46-
op.create_table('restaurant',
47-
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
48-
sa.Column('name', sa.String(length=40), nullable=False),
49-
sa.Column('description', sa.String(length=120), nullable=False),
50-
sa.Column('classification', sa.Float(precision=3), nullable=False),
51-
sa.Column('location', sa.String(length=120), nullable=False),
52-
sa.Column('url_image_logo', sa.String(length=800), nullable=True),
53-
sa.Column('url_image_banner', sa.String(length=800), nullable=True),
54-
sa.Column('telephone', sa.String(length=11), nullable=False),
55-
sa.Column('has_plastic', sa.Boolean(), nullable=False),
56-
sa.PrimaryKeyConstraint('id'),
57-
sa.UniqueConstraint('name')
58-
)
5962
op.create_table('user',
6063
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
6164
sa.Column('firstname', sa.String(length=20), nullable=False),
@@ -68,11 +71,11 @@ def upgrade():
6871
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
6972
sa.Column('created_at', sa.DateTime(), nullable=True),
7073
sa.Column('client_id', sa.Integer(), nullable=True),
71-
sa.Column('restaurant_id', sa.Integer(), nullable=True),
74+
sa.Column('establishment_id', sa.Integer(), nullable=True),
7275
sa.Column('total_value', sa.Float(), nullable=True),
7376
sa.Column('status', sa.Enum('pre_order', 'confirmed', 'doing', 'done', 'canceled', name='order_status'), nullable=True),
7477
sa.ForeignKeyConstraint(['client_id'], ['client.id'], ),
75-
sa.ForeignKeyConstraint(['restaurant_id'], ['restaurant.id'], ),
78+
sa.ForeignKeyConstraint(['establishment_id'], ['establishment.id'], ),
7679
sa.PrimaryKeyConstraint('id')
7780
)
7881
op.create_table('product',
@@ -86,8 +89,8 @@ def upgrade():
8689
sa.Column('has_lactose', sa.Boolean(), nullable=False),
8790
sa.Column('is_vegan', sa.Boolean(), nullable=False),
8891
sa.Column('is_vegetarian', sa.Boolean(), nullable=False),
89-
sa.Column('restaurant_id', sa.Integer(), nullable=False),
90-
sa.ForeignKeyConstraint(['restaurant_id'], ['restaurant.id'], ),
92+
sa.Column('establishment_id', sa.Integer(), nullable=False),
93+
sa.ForeignKeyConstraint(['establishment_id'], ['establishment.id'], ),
9194
sa.PrimaryKeyConstraint('id')
9295
)
9396
op.create_table('order_product_association',
@@ -111,28 +114,29 @@ def upgrade():
111114
sa.ForeignKeyConstraint(['order_id'], ['order.id'], ),
112115
sa.PrimaryKeyConstraint('id')
113116
)
114-
op.create_table('products_restaurants',
115-
sa.Column('product_id', sa.Integer(), nullable=True),
116-
sa.Column('restaurant_id', sa.Integer(), nullable=True),
117+
op.create_table('products_establishment',
118+
sa.Column('product_id', sa.Integer(), nullable=False),
119+
sa.Column('establishment_id', sa.Integer(), nullable=False),
120+
sa.ForeignKeyConstraint(['establishment_id'], ['establishment.id'], ),
117121
sa.ForeignKeyConstraint(['product_id'], ['product.id'], ),
118-
sa.ForeignKeyConstraint(['restaurant_id'], ['restaurant.id'], )
122+
sa.PrimaryKeyConstraint('product_id', 'establishment_id')
119123
)
120124
# ### end Alembic commands ###
121125

122126

123127
def downgrade():
124128
# ### commands auto generated by Alembic - please adjust! ###
125-
op.drop_table('products_restaurants')
129+
op.drop_table('products_establishment')
126130
op.drop_table('payment')
127131
op.drop_table('order_product_association')
128132
op.drop_table('product')
129133
op.drop_table('order')
130134
op.drop_table('user')
131-
op.drop_table('restaurant')
132135
with op.batch_alter_table('lead', schema=None) as batch_op:
133136
batch_op.drop_index(batch_op.f('ix_lead_email'))
134137

135138
op.drop_table('lead')
139+
op.drop_table('establishment')
136140
with op.batch_alter_table('client', schema=None) as batch_op:
137141
batch_op.drop_index(batch_op.f('ix_client_email'))
138142

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import json
2+
3+
from flask import abort, request
4+
from flask_restx import Resource
5+
6+
from project.doc_model.doc_models import api, establishment_model
7+
from project.ext.serializer import EstablishmentSchema
8+
from project.service.establishment_service import (
9+
delete_establishment,
10+
get_all_establishments,
11+
get_one_establishment_with_products,
12+
post_establishment,
13+
update_establishment,
14+
)
15+
from project.utils.redis_utils import (
16+
delete_redis_value,
17+
get_redis_value,
18+
set_redis_value,
19+
)
20+
21+
establishment_schema_list = EstablishmentSchema(many=True)
22+
establishment_schema = EstablishmentSchema(many=False)
23+
24+
25+
class EstablishmentResource(Resource):
26+
def get(self):
27+
key_redis = "establishment"
28+
establishments = get_redis_value(key_redis)
29+
if establishments:
30+
return establishments
31+
establishments = get_all_establishments()
32+
establishments = establishment_schema_list.dump(establishments)
33+
set_redis_value(key_redis, json.dumps(establishments))
34+
return establishments, 200
35+
36+
def post(self):
37+
try:
38+
establishment_data = request.json
39+
response = post_establishment(establishment_data)
40+
return response
41+
42+
except Exception as e:
43+
return {"error": str(e)}, 400
44+
45+
46+
class EstablishmentResourceID(Resource):
47+
def get(self, id: int):
48+
if establishment := get_one_establishment_with_products(id):
49+
return establishment # type: ignore
50+
else:
51+
return {"error": f"Estabelecimento com ID {id} não encontrado."}, 404
52+
53+
@api.expect(establishment_model)
54+
def patch(self, id: int):
55+
try:
56+
establishment_data = request.json
57+
result = update_establishment(id, establishment_data) # type: ignore
58+
59+
if "error" in result:
60+
abort(404, message=result["error"])
61+
delete_redis_value("clients")
62+
return {"message": result["message"]}, 200
63+
64+
except Exception as e:
65+
return {"error": str(e)}, 500
66+
67+
def delete(self, id: int):
68+
try:
69+
result = delete_establishment(id)
70+
71+
if "error" in result:
72+
return {"error": result["error"]}, 404
73+
delete_redis_value("establishments")
74+
return {"message": result["message"]}, 200
75+
76+
except Exception as e:
77+
return {"error": str(e)}, 500

project/controller/restaurant_controller.py

Lines changed: 0 additions & 79 deletions
This file was deleted.

project/doc_model/doc_models.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,23 @@
1111
},
1212
)
1313

14-
restaurant_model = api.model(
15-
"Restaurant",
14+
establishment_model = api.model(
15+
"Establishment",
1616
{
17-
"name": fields.String(required=True, description="Nome do restaurante"),
18-
"description": fields.String(
19-
required=True, description="Descrição do restaurante"
20-
),
21-
"classification": fields.Float(
22-
required=True, description="Classificação do restaurante"
23-
),
24-
"location": fields.String(
25-
required=True, description="Localização do restaurante"
26-
),
27-
"url_image_logo": fields.String(description="URL da logo do restaurante"),
28-
"url_image_banner": fields.String(description="URL do banner do restaurante"),
29-
"telephone": fields.String(
30-
required=True, description="Telefone do restaurante"
31-
),
32-
"has_plastic": fields.Boolean(
33-
required=True, description="Indica se o restaurante usa plástico"
34-
),
17+
"id": fields.Integer(description="ID do estabelecimento"),
18+
"official_name": fields.String(required=True, description="Nome oficial do estabelecimento"),
19+
"fantasy_name": fields.String(required=True, description="Nome fantasia do estabelecimento"),
20+
"cnpj": fields.String(required=True, description="CNPJ do estabelecimento"),
21+
"telephone": fields.String(required=True, description="Telefone do estabelecimento"),
22+
"zip_code": fields.String(required=True, description="CEP do estabelecimento"),
23+
"state": fields.String(required=True, description="Estado do estabelecimento"),
24+
"city": fields.String(required=True, description="Cidade do estabelecimento"),
25+
"address": fields.String(required=True, description="Endereço do estabelecimento"),
26+
"complement": fields.String(description="Complemento do endereço")
3527
},
3628
)
3729

30+
3831
user_model = api.model(
3932
"User",
4033
{
@@ -64,15 +57,15 @@
6457
"is_vegetarian": fields.Boolean(
6558
required=True, description="Indica se o produto é vegetariano"
6659
),
67-
"restaurant_id": fields.Integer(required=True, description="ID do restaurante"),
60+
"establishment_id": fields.Integer(required=True, description="ID do estabelecimento"),
6861
},
6962
)
7063

7164
order_model = api.model(
7265
"Order",
7366
{
7467
"client_id": fields.Integer(required=True, description="ID do cliente"),
75-
"restaurant_id": fields.Integer(required=True, description="ID do restaurante"),
68+
"establishment_id": fields.Integer(required=True, description="ID do estabelecimento"),
7669
"products": fields.List(fields.Integer, description="ID dos produtos"),
7770
},
7871
)

0 commit comments

Comments
 (0)