From 823be4fa1f167edf4830d28d598ee1c414457128 Mon Sep 17 00:00:00 2001 From: lucasbpro Date: Mon, 12 Apr 2021 23:30:14 -0300 Subject: [PATCH] feat: added new resources and table relations - orders, customers --- app.py | 1 + data.db | Bin 36864 -> 45056 bytes models/customers.py | 6 +++--- models/orders.py | 44 +++++++++++++++++++++++------------------ models/recipe.py | 8 +------- resources/customers.py | 9 +++++++++ resources/orders.py | 3 +++ 7 files changed, 42 insertions(+), 29 deletions(-) diff --git a/app.py b/app.py index 2df2cb9..ec23819 100644 --- a/app.py +++ b/app.py @@ -46,6 +46,7 @@ api.add_resource(Order, '/orders/') api.add_resource(Orders, '/orders') +api.add_resource(OrderList, '/customers//orders') # api.add_resource(UserRegister, '/register') diff --git a/data.db b/data.db index fefbf583b1ec414afcd21976ef36202a534cbe37..c01baabc495f91d0a82f2c4c6aef05bcbcf0c5f2 100644 GIT binary patch delta 1299 zcma)5&ubGw6rLZOO?KDxHB=f}i%tjxzm&Zlf1QQ=U?V?M_bDCq-%?-wsM0>)2ruM=ER{$mQi+RGiP@cVSb%TK!6{kv zUs5qDl=J$v%9Ucl(lc7sG8{Y8N-3kZ?kTIwp380#!xnp9c{Lg z+lq6kcL3pHox-Mw%UN`Mi`S&h){I5=^pWB zyWeF8V;)S-H2a$Epul4#J2v0!+GE#j{o?)9jU=1Ie{cvAJJ!biCxrm!p*Dqs8FVx% z7`Fu?u!!IlEOKE?s7>&5&f4!_R?8Y?UU{ZhxZYZ>S~Mz#!J0L05o>~B$U{nnaM3P$8EabGc tCu@%krk7Z*YF3OHZC)$sWy@d-I`4Ddcl*>BkHn0kw$q1Bdq13Ge*h(3I@16E delta 129 zcmZp8z|^pSX@ayMCj$cm2*UvTL>*&UP6j=h4ZPgb88|prG4Pl3N%1Q4xO30vQsWNb zShZPDK#ybdDvl_24krF<4E(S8uK|Tl@rw#Gu`q}$CZ^ diff --git a/models/customers.py b/models/customers.py index ff0a60d..649685a 100644 --- a/models/customers.py +++ b/models/customers.py @@ -14,9 +14,6 @@ class CustomerModel(db.Model): email = db.Column(db.String(constants['MEDIUM_LENGTH'])) birth_date = db.Column(db.String(constants['SHORT_LENGTH'])) - # define relationships with other tables - orders = db.relationship('OrderModel', lazy='dynamic') - def __init__(self, name, email, birth_date): self.name = name self.email = email @@ -45,3 +42,6 @@ def find_by_name(cls, name): @classmethod def find_by_id(cls, id_): return cls.query.filter_by(id=id_).first() + + def get_orders_from_customer(self): + return [order.json() for order in self.orders] diff --git a/models/orders.py b/models/orders.py index 7339610..db07b2a 100644 --- a/models/orders.py +++ b/models/orders.py @@ -3,6 +3,11 @@ from constants import constants # constants dictionary from datetime import datetime +orders_by_customer = db.Table('orders_by_customer', + db.Column('customer_id', db.Integer, db.ForeignKey('customers.id'), primary_key=True), + db.Column('order_id', db.Integer, db.ForeignKey('orders.id'), primary_key=True) +) + # defines the model for 'orders' table in db class OrderModel(db.Model): @@ -10,28 +15,30 @@ class OrderModel(db.Model): __tablename__ = 'orders' # define columns in table - id = db.Column(db.Integer, primary_key=True); - product_id = db.Column(db.Integer, db.ForeignKey('recipes.id')); - customer_id = db.Column(db.Integer, db.ForeignKey('customers.id')); - order_total = db.Column(db.String(constants['SHORT_LENGTH'])); - status_fabrication = db.Column(db.String(constants['MEDIUM_LENGTH'])); - status_payment = db.Column(db.String(constants['MEDIUM_LENGTH'])); - order_date = db.Column(db.String(constants['MEDIUM_LENGTH'])); - notes = db.Column(db.String(constants['MEDIUM_LENGTH'])); + id = db.Column(db.Integer, primary_key=True) + product_id = db.Column(db.Integer, db.ForeignKey('recipes.id')) + customer_id = db.Column(db.Integer, db.ForeignKey('customers.id')) + order_total = db.Column(db.String(constants['SHORT_LENGTH'])) + status_fabrication = db.Column(db.String(constants['MEDIUM_LENGTH'])) + status_payment = db.Column(db.String(constants['MEDIUM_LENGTH'])) + order_date = db.Column(db.String(constants['MEDIUM_LENGTH'])) + notes = db.Column(db.String(constants['MEDIUM_LENGTH'])) # define relationships with other tables - product = db.relationship('RecipeModel'); - customer = db.relationship('CustomerModel'); + customers = db.relationship('CustomerModel', + secondary=orders_by_customer, + backref=db.backref('orders'), + lazy='dynamic') def __init__(self, product_id, customer_id, notes): - self.product_id = product_id; - self.customer_id = customer_id; - self.notes = notes; - self.order_total = 0; - self.status_fabrication = "Fabricação a iniciar"; - self.status_payment = "Pagamento pendente"; - self.order_date = datetime.now().strftime("%d/%m/%Y %H:%M"); - self.notes = notes; + self.product_id = product_id + self.customer_id = customer_id + self.notes = notes + self.order_total = 0 + self.status_fabrication = "Fabricação a iniciar" + self.status_payment = "Pagamento pendente" + self.order_date = datetime.now().strftime("%d/%m/%Y %H:%M") + self.notes = notes def json(self): @@ -39,7 +46,6 @@ def json(self): 'id' : self.id, 'product_id' : self.product_id, 'customer_id' : self.customer_id, - 'notes' : self.notes, 'order_total' : self.order_total, 'status_fabrication' : self.status_fabrication, 'status_payment' : self.status_payment, diff --git a/models/recipe.py b/models/recipe.py index 7ef198d..f1442a4 100644 --- a/models/recipe.py +++ b/models/recipe.py @@ -61,10 +61,4 @@ def get_materials(self): return [material.json() for material in self.materials] def get_materials_amount(self): - materialList = {} - return [item.json() for item in RecipeMaterialAmountModel.query.all()] - #for material in self.materials: - # recipeMaterialItem = RecipeMaterialAmountModel.find_by_map(self.id,material.id) - # if recipeMaterialItem: - # materialList[str(material.id)] = recipeMaterialItem.amount - #return materialList \ No newline at end of file + return [item.json() for item in RecipeMaterialAmountModel.query.all()] \ No newline at end of file diff --git a/resources/customers.py b/resources/customers.py index 60bc1db..0eaeea4 100644 --- a/resources/customers.py +++ b/resources/customers.py @@ -101,3 +101,12 @@ def post(self): # returns JSON with the created Material and returns CREATED status (201) return customer.json(), 201 +# class used to get the orders from customer +class OrderList(Resource): + # route: customer//orders + def get(self, id): + customer = CustomerModel.find_by_id(id) + if customer: + return customer.get_orders_from_customer() + else: + return {'message' : constants['ID_NOT_FOUND']} \ No newline at end of file diff --git a/resources/orders.py b/resources/orders.py index b449bc0..1dd712d 100644 --- a/resources/orders.py +++ b/resources/orders.py @@ -13,6 +13,7 @@ class Order(Resource): parser = reqparse.RequestParser() parser.add_argument('status_fabrication',type=str,required=False) parser.add_argument('status_payment',type=str,required=False) + parser.add_argument('product_id',type=str,required=False) # to handle HTTP GET /orders/ def get(self, id): @@ -49,6 +50,8 @@ def put(self, id): order.status_fabrication = data['status_fabrication'] if key=='status_payment': order.status_payment = data['status_payment'] + if key=='product_id': + order.product_id = data['product_id'] # tries to insert in database # returns 500 (internal server error) in case of database failure