async def make_movement(movement_in: MovementIn): user_in_db = get_user(movement_in.username) if user_in_db == None: raise HTTPException(status_code=404, detail="El usuario ingresado no existe.") if movement_in.movement_type == 'outcome' and user_in_db.balance < movement_in.amount: raise HTTPException( status_code=400, detail="El gasto ingresado supera su balance actual.") if movement_in.movement_type == 'outcome': user_in_db.balance = user_in_db.balance - movement_in.amount else: user_in_db.balance = user_in_db.balance + movement_in.amount update_user(user_in_db) movement_in_db = MovementInDB(**movement_in.dict(), actual_balance=user_in_db.balance) movement_in_db = save_movement(movement_in_db) movement_out = MovementOut(**movement_in_db.dict()) return movement_out
async def make_transaction( transaction_in: TransactionIn): #entra modelo transaccion user_in_db = get_user(transaction_in.username) #valida usuario if user_in_db == None: raise HTTPException(status_code=404, detail="El usuario no existe") if user_in_db.balance < transaction_in.value: #valida el saldo con lo que se retirara raise HTTPException(status_code=400, detail="Sin fondos suficientes") # ya todo validado user_in_db.balance = user_in_db.balance - transaction_in.value #resta la transaccion del saldo update_user(user_in_db) #actualiza la base de datos #creamos el un objeto trnasaccionIN con el mapeo de la transaccion transaction_in_db = TransactionInDB( **transaction_in.dict(), actual_balance=user_in_db.balance ) #guarda el balance del usuario en ese momento como un atributo suyo transaction_in_db = save_transaction( transaction_in_db) #guarda esa transaccionIN transaction_out = TransactionOut( **transaction_in_db.dict()) #crea la TransaccionOUT con su mapeo return transaction_out #retorna el modelo de transaccion OUT
async def user_credito(user_cred: UserCash): user_in_db = get_user(user_cred.alias) if user_in_db == None: raise HTTPException(status_code=404, detail="El usuario no existe") else: user_in_db.saldo = user_in_db.saldo + user_cred.valor update_user(user_in_db) return "Usuario:", user_in_db.alias, "Nuevo saldo:", user_in_db.saldo
async def users_put(user_in:UserOut): user = get_user(user_in.username) if user != None: raise HTTPException(status_code=404, detail=False) update_user(user_in) raise HTTPException(status_code=200, detail=True)
async def create_gasto(gasto_in_db: Gasto): user_in_db = get_user(gasto_in_db.username) if user_in_db == None: raise HTTPException(status_code=404, detail="El usuario no existe") user_in_db.total_gastos = user_in_db.total_gastos + gasto_in_db.value update_user(user_in_db) gasto_in_db = GastoInDB(**gasto_in_db.dict()) gasto_in_db = save_gasto(gasto_in_db) gasto = Gasto(**gasto_in_db.dict(), total_gastos=user_in_db.total_gastos) return gasto
async def make_transaction(transaction_in: TransactionIn): user_in_db = get_user(transaction_in.username) if user_in_db == None: raise HTTPException(status_code=404, detail="El usuario no existe") if user_in_db.balance < transaction_in.value: raise HTTPException(status_code=400, detail="Sin fondos suficientes") user_in_db.balance = user_in_db.balance - transaction_in.value update_user(user_in_db) transaction_in_db = TransactionInDB(**transaction_in.dict(), actual_balance = user_in_db.balance) transaction_in_db = save_transaction(transaction_in_db) transaction_out = TransactionOut(**transaction_in_db.dict()) return transaction_out
async def make_movem_add(movem_in: MovemIn): user_in_db = get_user(movem_in.username) if user_in_db == None: raise HTTPException(status_code=404, detail="El usuario no existe") user_in_db.balance = user_in_db.balance + movem_in.value update_user(user_in_db) movem_in_db = MovemInDB(**movem_in.dict(), actual_saldo=user_in_db.balance) movem_in_db = save_movem(movem_in_db) movem_out = MovemOUT(**movem_in_db.dict()) return movem_out
async def update_user(user_select: userIn): user_in = get_user(user_select.user) if user_in == None: raise HTTPException(status_code=404, detail="Usuario ya existe") user_in.name = user_select.name user_in.user = user_select.user user_in.password = user_select.password user_in.email = user_select.email update_user(user_in) user_out = userOut(**user_in.dict()) return user_out
async def make_transaction(transaction_in: TransactionIn): user_in_db = get_user(transaction_in.username) if user_in_db == None: raise HTTPException(status_code=404, detail="El usuario no existe") user_in_db.balance = user_in_db.balance + transaction_in.income user_in_db.balance = user_in_db.balance - transaction_in.expense update_user(user_in_db) transaction_in_db = TransactionInDB(**transaction_in.dict(), actual_balance = user_in_db.balance) transaction_in_db = save_transaction(transaction_in_db) transaction_out = TransactionOut(**transaction_in_db.dict()) return transaction_out
async def make_update(username: str, password: str, email: str, dob: str): user_in_db = get_user(username) if user_in_db == None: raise HTTPException(status_code=404, detail="El usuario no existe") else: user_in_db = UserIn(**dict({ "username": username, "password": password, "email": email, "dob": dob })) update_user(user_in_db) return {"El usuario " + username + " ha sido actualizado"} return None
async def make_transaction(transaction_in: TransactionIn):#Recibe un usuario y valor a retirar user_in_db = get_user(transaction_in.username)#Verifica si el usuario existe if user_in_db == None: raise HTTPException(status_code=404, detail="El usuario no existe") if user_in_db.balance < transaction_in.value: raise HTTPException(status_code=400, detail="Sin fondos suficientes") user_in_db.balance = user_in_db.balance - transaction_in.value #Resta el balance actual con el valor que retiro update_user(user_in_db) #Actualiza el usuario transaction_in_db = TransactionInDB( **transaction_in.dict(), actual_balance=user_in_db.balance) transaction_in_db = save_transaction(transaction_in_db) #Guarda la transaccion transaction_out = TransactionOut(**transaction_in_db.dict()) #Pasa la transaccion para retornarla al usuario return transaction_out
async def make_transaction(transaction_in: TransactionIn): user_in_db = get_user(transaction_in) if user_in_db == None: #Puede entenderse como un tipo de return raise HTTPException( status_code=404, detail="El usuario no existe") #para cuando existe el error #(no se ejecutan las acciones siguientes) if user_in_db.balance < transaction_in.value: ##Esta tambiƩn(las que contienen rise) raise HTTPException(status_code=400, detail="Sin fondos suficientes") user_in_db.balance -= transaction_in.value update_user(user_in_db) transaction_in_db = TransactionInDB( **transaction_in.dict(), actual_balance=user_in_db.balance ) # "**" sirve para mapear en el objeto transaction_out = TransactionOut(**transaction_in_db.dict()) return transaction_out
async def make_transaction(transaction_in: TransactionIn): user_in_db = get_user(transaction_in.nombre) if user_in_db == None: raise HTTPException(status_code=404, detail="El hotel no existe") if user_in_db.totalHabitaciones < transaction_in.totalHabitaciones: raise HTTPException(status_code=400, detail="No se tienen los fondos suficientes") user_in_db.totalHabitaciones = user_in_db.totalHabitaciones - transaction_in.reserva update_user(user_in_db) transaction_in_db = TransactionInDB(**transaction_in.dict(), totalHabs=user_in_db.totalHabitaciones) transaction_in_db = save_transaction(transaction_in_db) transaction_out = TransactionOut(**transaction_in_db.dict()) return transaction_out
async def make_user(user_in: UserIn): user_in_db = get_user(user_in.username) if user_in_db == None: raise HTTPException(status_code=404, detail="El usuario no existe") if user_in_db.password == user_in.password: user_in_db.phone = user_in.phone user_in_db = update_user(user_in_db) user_out = UserOut(**user_in_db.dict()) return user_out return "ContraseƱa incorrecta"
async def Actualizar(put_user: dict, username: str): temporal = put_user temporal["username"] = username apodo = update_user(temporal) return apodo
return {"Autenticado": True} @api.get("/user/balance/{username}") async def get_balance(username: str): user_in_db = get_user(username) if user_in_db == None: raise HTTPException(status_code=404, detail="El usuario no existe") user_out = UserOut(**user_in_db.dict()) return user_out @api.put("/user/transaction/") async def make_transaction(transaction_in: TransactionIn): user_in_db = get_user(transaction_in.username) if user_in_db == None: raise HTTPException(status_code=404, detail="El usuario no existe") user_in_db.balance = user_in_db.balance + transaction_in.income user_in_db.balance = user_in_db.balance - transaction_in.expense update_user(user_in_db) transaction_in_db = TransactionInDB(**transaction_in.dict(), actual_balance = user_in_db.balance) transaction_in_db = save_transaction(transaction_in_db) transaction_out = TransactionOut(**transaction_in_db.dict()) return transaction_out