Added transaction management in controllers.

This commit is contained in:
Alexis Lahouze 2013-01-24 14:58:28 +01:00
parent a7e26e7d96
commit f56e06223f
2 changed files with 79 additions and 49 deletions

View File

@ -46,15 +46,23 @@ def get_months(account_id):
@app.route("/api/accounts", methods=["PUT"])
def add_account():
with session.begin():
try:
account = Account(request.json['name'])
session.add(account)
session.commit()
return json.dumps("Account added.")
except:
session.rollback()
raise
@app.route("/api/accounts/<account_id>", methods=["PUT"])
def update_account(account_id):
with session.begin():
try:
account = session.query(Account).filter(Account.id == account_id).first()
account.name = request.json['name']
@ -63,13 +71,21 @@ def update_account(account_id):
session.commit()
return json.dumps("Account #%s updated." % account_id)
except:
session.rollback()
raise
@app.route("/api/accounts/<account_id>", methods=["DELETE"])
def delete_account(account_id):
with session.begin():
try:
account = session.query(Account).filter(Account.id == account_id).first()
session.delete(account)
session.commit()
return json.dumps("Account #%s deleted." % account_id)
except:
session.rollback()
raise

View File

@ -45,6 +45,8 @@ def get_entries(account_id, year, month):
@app.route("/api/entries", methods=["PUT"])
def add_entry():
with session.begin():
try:
entry = Entry(
value_date = request.json['value_date'],
operation_date = request.json['operation_date'],
@ -58,9 +60,14 @@ def add_entry():
session.commit()
return json.dumps("Entry added.")
except:
session.rollback()
raise
@app.route("/api/entries/<entry_id>", methods=["PUT"])
def update_entry(entry_id):
with session.begin():
try:
entry = session.query(Entry).filter(Entry.id == entry_id).first()
entry.id = entry_id
@ -75,14 +82,21 @@ def update_entry(entry_id):
session.commit()
return json.dumps("Entry #%s updated." % entry_id)
except:
session.rollback()
raise
@app.route("/api/entries/<entry_id>", methods=["DELETE"])
def delete_entry(entry_id):
with session.begin():
try:
entry = session.query(Entry).filter(Entry.id == entry_id).first()
session.delete(entry)
session.commit()
return json.dumps("Entry #%s deleted." % entry_id)
except:
session.rollback()
raise