Skip to content
Permalink
Browse files

Actually add purchases controller.

  • Loading branch information
kodek16 committed Nov 17, 2019
1 parent f81eeea commit cb36d8953c01cb4a77f630f249790fd13589777b
Showing with 42 additions and 0 deletions.
  1. +26 −0 register-purchase.py
  2. +16 −0 server/controllers/purchases.py
@@ -0,0 +1,26 @@
import requests
import click

HOST = 'http://localhost:5000'
USER_ID = 'asdfsdafsdaf'

PRODUCT_IDS = {
'detergent': 2,
'chocolate': 4,
}


@click.command()
@click.option('--item', type=click.Choice(['detergent', 'chocolate']))
@click.option('--quantity', type=int)
def register_purchase(item, quantity):
payload = {
'userId': USER_ID,
'productId': PRODUCT_IDS[item],
'quantity': quantity,
}
requests.post(HOST + '/purchases', json=payload)


if __name__ == '__main__':
register_purchase()
@@ -0,0 +1,16 @@
from model import Purchase, Session


class PurchasesController:
def add_purchase(self, user, product, quantity, session=None):
own_session = False
if not session:
session = Session()
own_session = True

purchase = Purchase(user, product, quantity)
session.add(purchase)

if own_session:
session.commit()
session.close()

0 comments on commit cb36d89

Please sign in to comment.
You can’t perform that action at this time.