Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add way to access orders and items for a user #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apos/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from apos.extensions import app, api
from apos.resources.coupons import CouponResource, CouponListResource
from apos.resources.orders import OrderListResource, OrderResource, OrderActiveListResource
from apos.resources.items import ItemListResource, ItemResource
from apos.resources.orders import OrderListResource, OrderUserListResource, OrderResource, OrderActiveListResource
from apos.resources.items import ItemListResource, ItemUserListResource, ItemResource
from apos.resources.auth import Auth, Signup

api.add_resource(CouponResource, '/api/v1/coupons/<int:coupon_id>')
Expand All @@ -11,6 +11,8 @@
api.add_resource(OrderResource, '/api/v1/orders/<int:order_id>')
api.add_resource(ItemListResource, '/api/v1/orders/<int:order_id>/items')
api.add_resource(ItemResource, '/api/v1/orders/<int:order_id>/items/<int:item_id>')
api.add_resource(OrderUserListResource, '/api/v1/user/orders')
api.add_resource(ItemUserListResource, '/api/v1/user/items')
api.add_resource(Auth, '/api/v1/auth')
api.add_resource(Signup, '/api/v1/signup')

Expand Down
1 change: 1 addition & 0 deletions apos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def serialize(self):
item = {
'id': self.id,
'order_id': self.order.id,
'order': self.order.serialize,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am against serializing the order here. We should discuss that.

'user': self.user.serialize,
'name': self.name,
'tip_percent': self.tip_percent,
Expand Down
15 changes: 14 additions & 1 deletion apos/resources/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
item_create_parser = reqparse.RequestParser()
item_create_parser.add_argument('name', type=str, required=True)
item_create_parser.add_argument('tip_percent', type=int, required=False)
item_create_parser.add_argument('tip_absolute', type=int, required=False)
item_create_parser.add_argument('price', type=int, required=False) # Why is the price false?

item_patch_parser = reqparse.RequestParser()
item_patch_parser.add_argument('name', type=str, required=False)
item_patch_parser.add_argument('tip_percent', type=int, required=False)
item_patch_parser.add_argument('tip_absolute', type=int, required=False)
item_patch_parser.add_argument('price', type=int, required=False)


Expand All @@ -30,11 +32,21 @@ def put(self, order_id):
order = Order.query.get(order_id)
if order.deadline < datetime.utcnow():
abort(422, message="The order is expired, so no items can be added")
if args.get("tip_absolute", None) and args.get("tip_percent", None):
abort(422, message="The tip can only be provided in percent OR absolute value")
item = Item(order_id=order_id, user_id=get_jwt_identity(), **args)
db.session.add(item)
db.session.commit()
return item.serialize, 201


class ItemUserListResource(Resource):
@jwt_required
def get(self):
items = Item.query.filter_by(user_id=get_jwt_identity()).all()
return [item.serialize for item in items]


class ItemResource(Resource):
@jwt_required
def get(self, order_id, item_id):
Expand Down Expand Up @@ -62,6 +74,7 @@ def patch(self, order_id, item_id):
if item.order.deadline < datetime.utcnow():
abort(422, message="The order is expired, so no items can be modified")
item.update(args)
if item.tip_absolute and item.tip_percent:
abort(422, message="The tip can only be provided in percent OR a absolute value. Take care that not both are set in the db.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
abort(422, message="The tip can only be provided in percent OR a absolute value. Take care that not both are set in the db.")
abort(422, message="The tip can only be provided in percent OR absolute value. Take care that only one is set.")

db.session.commit()
return item.serialize

7 changes: 7 additions & 0 deletions apos/resources/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def get(self):
return [order.serialize for order in orders]


class OrderUserListResource(Resource):
@jwt_required
def get(self):
orders = Order.query.filter(Order.owner_id == get_jwt_identity()).all()
return [order.serialize for order in orders]


class OrderResource(Resource):
@jwt_required
def get(self, order_id):
Expand Down