Skip to content

Commit

Permalink
Add order creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimond committed Oct 26, 2019
1 parent 3ae699a commit ff92b26
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/middlewares.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from functools import wraps

from app.models import Shipper, Carrier

from flask import abort, g, request


def session_middleware(func):
# @wraps(func)
@wraps(func)
def decorator(*args, **kwargs):
request_token = request.headers.get('Authorization')
if request_token is None:
Expand Down
41 changes: 38 additions & 3 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,50 @@
from app import db
import enum


class OrderStatus(enum.Enum):
NOT_SENT = 'NOT_SENT'
CANCELLED = 'CANCELLED'
ON_THE_WAY = 'ON_THE_WAY'
DELIVERED = 'DELIVERED'


class Order(db.Model):
id = db.Column(db.Integer, primary_key=True)

cost = db.Column(db.Integer)
reward = db.Column(db.Integer)
weight = db.Column(db.Integer)
dimension = db.Column(db.String(50))

dimensions = db.Column(db.String(50))
pickup_location = db.Column(db.String(50))
destination = db.Column(db.String(50))
distance = db.Column(db.Integer)
coverage = db.Column(db.Integer)
shipment_date = db.Column(db.String(50))
delivery_date = db.Column(db.String(50))
shipper_id = db.Column(db.Integer, db.ForeignKey('shipper.id'))
carrier_id = db.Column(db.Integer, db.ForeignKey('carrier.id'))
secret = db.Column(db.String(50), unique=True)
status = db.Column(db.Enum(OrderStatus))

def get_json(self, role):
data = {
'id': self.id,
'status': self.status,
'pickupLocation': self.pickup_location,
'destination': self.destination,
'distance': self.distance,
'dimensions': self.dimensions,
'weight': self.weight,
'coverage': self.coverage,
'reward': self.reward,
'shipmentDate': self.shipment_date,
'deliveryDate': self.delivery_date,
'carrier': self.carrier,
}
if role == 'SHIPPER':
data['orderSecret'] = self.secret

return data

def __repr__(self):
return f'Order {self.cost} {self.weight}'
Expand All @@ -27,6 +61,7 @@ class Shipper(db.Model):
address = db.Column(db.String(100))

orders = db.relationship('Order', backref='shipper', lazy=True)

def get_role(self):
return 'SHIPPER'

Expand Down
46 changes: 46 additions & 0 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,52 @@ def user_info():
}


@app.route('/api/user/orders', methods=['POST', 'GET'])
@session_middleware
def user_orders():
user = g.user
if request.method == 'POST' and user.get_role() == 'SHIPPER':
request_body = request.json

pickup_location = request_body['pickupLocation']
destination = request_body['destination']
dimensions = request_body['dimensions']
weight = request_body['weight']
coverage = request_body['coverage']
shipment_date = request_body['shipmentDate']
delivery_date = request_body['deliveryDate']
print(pickup_location, destination, dimensions, weight, coverage, shipment_date, delivery_date)
if (
pickup_location
and destination
and dimensions
and weight is not None
and coverage is not None
and shipment_date
and delivery_date
):
o = Order(
pickup_location=pickup_location,
destination=destination,
dimensions=dimensions,
weight=weight,
coverage=coverage,
shipment_date=shipment_date,
delivery_date=delivery_date,
)
user.orders.append(o)
db.session.add(o)
db.session.add(user)
db.session.commit()
return jsonify({'result': 'success'})
else:
return jsonify({'error': 'wrong request'})
elif request.method == 'GET':
return jsonify([o.get_json(user.get_role()) for o in user.orders])
else:
return jsonify({'error': 'only shipper can create orders'})


@app.route('/api/debug/increase-balance', methods=['POST'])
def balance_increase():
request_body = request.json
Expand Down

0 comments on commit ff92b26

Please sign in to comment.