Esempio n. 1
0
def addFriend(first_user_id, second_user_id):
    result_list = q.selectInfoByConditions(
        "Friends",
        "user_id",
        "user_id = '%s' AND friend_id = '%s'",
        (first_user_id, second_user_id),
    )

    res = {}
    if len(result_list) > 0 or first_user_id == second_user_id:
        res["status"] = False
    else:

        first_res = q.selectInfoByConditions(
            "Users",
            "user_name",
            "user_id = '%s'",
            (first_user_id),
        )

        second_res = q.selectInfoByConditions(
            "Users",
            "user_name",
            "user_id = '%s'",
            (second_user_id),
        )

        if len(first_res) == 0 or len(second_res) == 0:
            res["status"] = False
            return res

        print(first_res)
        res["status"] = True
        q.insertRecordTo(
            "Friends",
            "(user_id, friend_id, friend_name, title)",
            (
                first_user_id,
                second_user_id,
                second_res[0]["user_name"],
                second_res[0]["user_name"][0].upper(),
            ),
            "(%s, %s, %s, %s)",
        )
        q.insertRecordTo(
            "Friends",
            "(user_id, friend_id, friend_name, title)",
            (
                second_user_id,
                first_user_id,
                first_res[0]["user_name"],
                first_res[0]["user_name"][0].upper(),
            ),
            "(%s, %s, %s, %s)",
        )

    return res
Esempio n. 2
0
def createAllocation(userId, itemId, alloAmount):
    if not itemExists(itemId):
        return -1

    leftAmount = q.selectInfoByConditions(
        "Items",
        "left_amount",
        "item_id = '%s'",
        itemId,
    )[0]['left_amount']
    if alloAmount > leftAmount:
        return -1

    if not deductItemAmount(itemId, alloAmount):  # Modify left_amount
        return -1

    if not q.insertRecordTo(
            "Allocations",
            "(item_id, user_id, allo_amount)",
        (str(itemId), str(userId), str(alloAmount)),
            "(%s, %s, %s)",
    ):
        return -1

    return q.selectInfoByConditions(
        "Allocations",
        "allo_id",
        "item_id = '%s' AND user_id = '%s'",
        (itemId, userId),
    )[-1]['allo_id']
Esempio n. 3
0
def createGroup(gName, ownerId):
    if groupNameExists(gName):
        return False
    return q.insertRecordTo(
        "gGroups", "(group_name, owner_id)",
        (gName, ownerId), "(%s, %s)",
    )
Esempio n. 4
0
def createItem(itemName, leftAmount, orderId):
    q.insertRecordTo(
        "Items",
        "(item_name, left_amount, order_id)",
        (itemName, leftAmount, orderId),
        "(%s, %s, %s)",
    )
    result_list = q.selectInfoByConditions(
        "Items",
        "item_id",
        "order_id = '%s' AND item_name = '%s'",
        (orderId, itemName),
    )
    if len(result_list) > 0:
        return result_list[0]['item_id']
    else:
        return -1
Esempio n. 5
0
def insertNewReceipt(sender, receiver, receiptId, data):
    if q.insertRecordTo(
        "MessageQueue", "(sender, receiver, event, receipt_id, data)",
        (sender, receiver, "new", receiptId, data), "(%s, %s, %s, %s, %s)",
    ):
        return True
    else:
        return False
Esempio n. 6
0
def sendChallenge(sender, receiver, receiptId):
    if q.insertRecordTo(
        "MessageQueue", "(sender_id, receiver_id, event, receipt_id)",
        (sender, receiver, "challenge", receiptId), "(%s, %s, %s, %s)",
    ):
        return True
    else:
        return False
Esempio n. 7
0
def createUser(username, userpwd):
    if not usernameExists(username):
        if q.insertRecordTo(
            "Users", "(user_name, user_pwd)",
            (username, userpwd), "(%s, %s)",
        ):
            return getUserIdByUsername(username)
    return -1
Esempio n. 8
0
def addGroupChat(group_id, user_id, user_name, text):
    ts = time.time()
    timestamp = datetime.datetime.fromtimestamp(ts).strftime(
        '%Y-%m-%d %H:%M:%S')

    return q.insertRecordTo(
        "GroupChats",
        "(group_id, user_id, user_name, text, timestamp)",
        (group_id, user_id, user_name, text, timestamp),
        "(%s, %s, %s, %s, %s)",
    )
Esempio n. 9
0
def createReceipt(orderId, receiptPath, receiptIdx=1):
    if not (orderExists(orderId)):
        return (-1, -1)

    if not q.insertRecordTo(
            "OrderReceipts",
            "(order_id, receipt_idx, "
            "receipt_path)",
        (orderId, receiptIdx, receiptPath),
            "(%s, %s, %s)",
    ):
        return (-1, -1)

    return (orderId, receiptIdx)
Esempio n. 10
0
def insertMemberToGroup(groupId, memberId):
    result_list = q.selectInfoByConditions(
        "GroupUsers", "group_id",
        "group_id = '%s' AND member_id = '%s'", (groupId, memberId),
    )

    if (len(result_list) > 0):
        print("MATCH")
        return True

    return q.insertRecordTo(
        "GroupUsers", "(group_id, member_id)",
        (groupId, memberId), "(%s, %s)",
    )
Esempio n. 11
0
def createOrder(groupId, orderName, numOfItems):
    check = q.insertRecordTo(
        "Orders",
        "(group_id, order_name, item_unres_num)",
        (groupId, orderName, numOfItems),
        "(%s, %s, %s)",
    )

    if check:
        result_list = q.selectInfoByConditions(
            "Orders",
            "order_id",
            "order_name = '%s' AND group_id = '%s'",
            (orderName, groupId),
        )
        if len(result_list) > 0:
            return result_list[-1]['order_id']  # get the newly inserted
    return -1  # failed