Skip to content
Browse files

finally successfully finished the goddamned SELL command and its tests

  • Loading branch information...
1 parent 66b71d6 commit 8c849b26f730d01a9fcd988c602a916e8e77e12e @robbles robbles committed
Showing with 18 additions and 19 deletions.
  1. +18 −19 sps/transactions/commands.py
View
37 sps/transactions/commands.py
@@ -194,42 +194,41 @@ def run(self, userid):
class SELLCommand(CommandHandler):
"""
- Sell the specified dollar mount of the stock currently held by the
+ Sell the specified dollar amount of the stock currently held by the
specified user at the current price.
"""
def run(self, userid, stock_symbol, amount):
+
# see if user exists
session = get_session()
user = session.query(User).filter_by(userid=userid).first()
- print user
if not user:
raise UserNotFoundError(userid)
- # check to see if stock symbol is valid
- transaction = session.query(Transaction).filter_by(
- stock_symbol=stock_symbol).first()
- print transaction
- if not transaction:
- raise InvalidInputError("no transactions exist for this stock")
-
-
- ################
- #STILL NEED TO MAKE SURE THAT APPROPRIATE AMOUNT IS THERE
- ################
+ #see if the user owns the requested stock and has enough for request
+ record = session.query(StockPurchase).filter_by(user_id=userid, stock_symbol=stock_symbol).first()
+ if not record:
+ raise InvalidInputError("user doesn't own this stock")
+ elif record.quantity < amount:
+ raise NotEnoughStockAvailable()
- # set up client to get quote
+
+ #set up client to get quote
quote_client = QuoteClient.get_quote_client()
quoted_stock_value = quote_client.get_quote(stock_symbol)
- print quoted_stock_value
-
+
# make transaction
self.trans = Transaction(user_id=userid, stock_symbol=stock_symbol,
operation='SELL', committed=False, quantity=amount,
stock_value=quoted_stock_value)
- # commit transaction
- self.session.add(self.trans)
- self.session.commit()
+ # modify records
+ record.quantity -= amount
+
+
+ # commit transaction after all actions for atomicity
+ session.add(self.trans)
+ session.commit()
return 'success\n'

0 comments on commit 8c849b2

Please sign in to comment.
Something went wrong with that request. Please try again.