db.add changes
Added _add_model helper function to reduce code duplication Added add_accounts for future use
This commit is contained in:
parent
9f64305050
commit
6bc8eb1413
@ -5,27 +5,42 @@ from sqlalchemy.future import Engine
|
||||
from . import models
|
||||
|
||||
|
||||
def add_account(engine: Engine, account: models.Account) -> bool:
|
||||
"""Adds account to the database. Returns true on success,
|
||||
def _add_model(
|
||||
session: sqlmodel.Session, model: models.Account | models.MasterPass
|
||||
) -> bool:
|
||||
"""Adds model to the session. Returns true on success,
|
||||
false otherwise"""
|
||||
try:
|
||||
with sqlmodel.Session(engine) as session:
|
||||
session.add(account)
|
||||
session.commit()
|
||||
session.add(model)
|
||||
except IntegrityError:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
def add_account(engine: Engine, account: models.Account) -> bool:
|
||||
"""Adds account to the database. Returns true on success,
|
||||
false otherwise"""
|
||||
with sqlmodel.Session(engine) as session:
|
||||
result = _add_model(session, account)
|
||||
session.commit()
|
||||
return result
|
||||
|
||||
|
||||
def add_master_pass(engine: Engine, master_pass: models.MasterPass) -> bool:
|
||||
"""Adds master password the database. Returns true on success,
|
||||
false otherwise"""
|
||||
try:
|
||||
with sqlmodel.Session(engine) as session:
|
||||
session.add(master_pass)
|
||||
session.commit()
|
||||
except IntegrityError:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
with sqlmodel.Session(engine) as session:
|
||||
result = _add_model(session, master_pass)
|
||||
session.commit()
|
||||
return result
|
||||
|
||||
|
||||
def add_accounts(
|
||||
engine: Engine,
|
||||
accounts: list[models.Account],
|
||||
) -> list[bool]:
|
||||
with sqlmodel.Session(engine) as session:
|
||||
result = [_add_model(session, account) for account in accounts]
|
||||
session.commit()
|
||||
return result
|
||||
|
Reference in New Issue
Block a user