Skip to content

Commit

Permalink
Merge pull request #3 from PazBazak/remove-withdrawl-from-db
Browse files Browse the repository at this point in the history
updating DB when withdrawing
  • Loading branch information
PazBazak authored Jan 5, 2021
2 parents 2a0740c + c561d9a commit 4ceb8ba
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
23 changes: 20 additions & 3 deletions ATM_api/api/consts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

from decimal import Decimal

# region bills

Expand All @@ -9,6 +9,13 @@

VALID_BILLS = [BILL_200, BILL_100, BILL_50, BILL_20]

BILLS_VALUES = {
BILL_200: 200,
BILL_100: 100,
BILL_50: 50,
BILL_20: 20,
}

# endregion

# region coins
Expand All @@ -21,6 +28,14 @@

VALID_COINS = [COIN_10, COIN_5, COIN_1, COIN_01, COIN_001]

COINS_VALUES = {
COIN_10: Decimal('10.0'),
COIN_5: Decimal('5.0'),
COIN_1: Decimal('1.0'),
COIN_01: Decimal('0.1'),
COIN_001: Decimal('0.01'),
}

# endregion

# region responses
Expand All @@ -29,8 +44,10 @@

# endregion

CURRENCY = "currency"
AMOUNT = "amount"
CURRENCY = 'currency'
AMOUNT = 'amount'

PK = 'pk'

VALID_CURRENCIES = ['ILS', 'USD']

Expand Down
24 changes: 24 additions & 0 deletions ATM_api/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ def is_float(string):
return is_float_


def withdraw_from_db(bills: dict, coins: dict) -> None:
"""
Receives the bills and coins that the ATM is wishing to withdraw, after all the verification needed, thus
it trust that the DB has this amount of records, and removes them from the DB.
"""
for bill_type, bill_amount in bills.items():

if bill_amount:
# Bill.objects.filter(value=BILLS_VALUES[bill_type])[:bill_amount].delete()
Bill.objects.filter(pk__in=Bill.objects.filter(
value=BILLS_VALUES[bill_type]
).values_list(PK)[:bill_amount]).delete()

for coin_type, coin_amount in coins.items():

if coin_amount:
Coin.objects.filter(pk__in=Coin.objects.filter(
value=COINS_VALUES[coin_type]
).values_list(PK)[:coin_amount]).delete()


def withdraw_from_atm(inventory: dict, amount: float) -> dict:
"""
withdraw the amount from the inventory in the most optimal way!
Expand Down Expand Up @@ -169,6 +190,9 @@ def withdraw_from_atm(inventory: dict, amount: float) -> dict:
if amount != 0:
return ERROR_AMOUNT_RES

# withdraws from the DB
withdraw_from_db(bills, coins)

return {"results": {
"bills": bills,
"coins": coins
Expand Down

0 comments on commit 4ceb8ba

Please sign in to comment.