Browse Source

fix very small gifts

Eren Yilmaz 6 years ago
parent
commit
7e2e19ef1d
2 changed files with 18 additions and 4 deletions
  1. 13 3
      model.py
  2. 5 1
      server_controller.py

+ 13 - 3
model.py

@@ -405,9 +405,6 @@ def get_ownable_orders(ownable_id):
 def sell_ordered_amount(user_id, ownable_id):
     connect()
 
-    # if ownable_id == currency_id():
-    #     return 0
-
     cursor.execute('''
                 SELECT COALESCE(SUM(orders.ordered_amount - orders.executed_amount),0)
                 FROM orders, ownership
@@ -420,6 +417,19 @@ def sell_ordered_amount(user_id, ownable_id):
     return cursor.fetchone()[0]
 
 
+def available_amount(user_id, ownable_id):
+    connect()
+
+    cursor.execute('''
+                SELECT amount
+                FROM ownership
+                WHERE user_id = ?
+                AND ownable_id = ?
+                ''', (user_id, ownable_id))
+
+    return cursor.fetchone()[0] - sell_ordered_amount(user_id, ownable_id)
+
+
 def user_owns_at_least(amount, user_id, ownable_id):
     connect()
 

+ 5 - 1
server_controller.py

@@ -83,6 +83,10 @@ def order():
     session_id = request.json['session_id']
     amount = request.json['amount']
     amount = float(amount)
+    if amount < 0:
+        return bad_request('You can not order a negative amount.')
+    if amount < 1:
+        return bad_request('The minimum order size is 1.')
     ownable_name = request.json['ownable']
     time_until_expiration = float(request.json['time_until_expiration'])
     if time_until_expiration < 0:
@@ -140,7 +144,7 @@ def gift():
     sender_id = model.get_user_id_by_session_id(request.json['session_id'])
     recipient_id = model.get_user_id_by_name(request.json['username'])
     if not model.user_owns_at_least(amount, sender_id, ownable_id):
-        return bad_request('You do not own enough.')
+        amount = model.available_amount(sender_id, ownable_id)
 
     model.send_ownable(sender_id, recipient_id, request.json['object_name'], amount)