Skip to content

Commit

Permalink
feat: added new resources and table relations - orders, customers
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasbpro committed Apr 13, 2021
2 parents 2fa4e7d + 823be4f commit 77622a2
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
1 change: 1 addition & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

api.add_resource(Order, '/orders/<int:id>')
api.add_resource(Orders, '/orders')
api.add_resource(OrderList, '/customers/<int:id>/orders')

# api.add_resource(UserRegister, '/register')

Expand Down
6 changes: 3 additions & 3 deletions models/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
44 changes: 25 additions & 19 deletions models/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,49 @@
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):

# name of the table in database
__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):
return {
'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,
Expand Down
8 changes: 1 addition & 7 deletions models/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
return [item.json() for item in RecipeMaterialAmountModel.query.all()]
9 changes: 9 additions & 0 deletions resources/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:id>/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']}
3 changes: 3 additions & 0 deletions resources/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:id>
def get(self, id):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 77622a2

Please sign in to comment.