Added transaction management in controllers.
This commit is contained in:
parent
a7e26e7d96
commit
f56e06223f
@ -46,15 +46,23 @@ def get_months(account_id):
|
|||||||
|
|
||||||
@app.route("/api/accounts", methods=["PUT"])
|
@app.route("/api/accounts", methods=["PUT"])
|
||||||
def add_account():
|
def add_account():
|
||||||
|
with session.begin():
|
||||||
|
try:
|
||||||
account = Account(request.json['name'])
|
account = Account(request.json['name'])
|
||||||
|
|
||||||
session.add(account)
|
session.add(account)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
return json.dumps("Account added.")
|
return json.dumps("Account added.")
|
||||||
|
except:
|
||||||
|
session.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/accounts/<account_id>", methods=["PUT"])
|
@app.route("/api/accounts/<account_id>", methods=["PUT"])
|
||||||
def update_account(account_id):
|
def update_account(account_id):
|
||||||
|
with session.begin():
|
||||||
|
try:
|
||||||
account = session.query(Account).filter(Account.id == account_id).first()
|
account = session.query(Account).filter(Account.id == account_id).first()
|
||||||
|
|
||||||
account.name = request.json['name']
|
account.name = request.json['name']
|
||||||
@ -63,13 +71,21 @@ def update_account(account_id):
|
|||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
return json.dumps("Account #%s updated." % account_id)
|
return json.dumps("Account #%s updated." % account_id)
|
||||||
|
except:
|
||||||
|
session.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
@app.route("/api/accounts/<account_id>", methods=["DELETE"])
|
@app.route("/api/accounts/<account_id>", methods=["DELETE"])
|
||||||
def delete_account(account_id):
|
def delete_account(account_id):
|
||||||
|
with session.begin():
|
||||||
|
try:
|
||||||
account = session.query(Account).filter(Account.id == account_id).first()
|
account = session.query(Account).filter(Account.id == account_id).first()
|
||||||
|
|
||||||
session.delete(account)
|
session.delete(account)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
return json.dumps("Account #%s deleted." % account_id)
|
return json.dumps("Account #%s deleted." % account_id)
|
||||||
|
except:
|
||||||
|
session.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
@ -45,6 +45,8 @@ def get_entries(account_id, year, month):
|
|||||||
|
|
||||||
@app.route("/api/entries", methods=["PUT"])
|
@app.route("/api/entries", methods=["PUT"])
|
||||||
def add_entry():
|
def add_entry():
|
||||||
|
with session.begin():
|
||||||
|
try:
|
||||||
entry = Entry(
|
entry = Entry(
|
||||||
value_date = request.json['value_date'],
|
value_date = request.json['value_date'],
|
||||||
operation_date = request.json['operation_date'],
|
operation_date = request.json['operation_date'],
|
||||||
@ -58,9 +60,14 @@ def add_entry():
|
|||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
return json.dumps("Entry added.")
|
return json.dumps("Entry added.")
|
||||||
|
except:
|
||||||
|
session.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
@app.route("/api/entries/<entry_id>", methods=["PUT"])
|
@app.route("/api/entries/<entry_id>", methods=["PUT"])
|
||||||
def update_entry(entry_id):
|
def update_entry(entry_id):
|
||||||
|
with session.begin():
|
||||||
|
try:
|
||||||
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
||||||
|
|
||||||
entry.id = entry_id
|
entry.id = entry_id
|
||||||
@ -75,14 +82,21 @@ def update_entry(entry_id):
|
|||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
return json.dumps("Entry #%s updated." % entry_id)
|
return json.dumps("Entry #%s updated." % entry_id)
|
||||||
|
except:
|
||||||
|
session.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
@app.route("/api/entries/<entry_id>", methods=["DELETE"])
|
@app.route("/api/entries/<entry_id>", methods=["DELETE"])
|
||||||
def delete_entry(entry_id):
|
def delete_entry(entry_id):
|
||||||
|
with session.begin():
|
||||||
|
try:
|
||||||
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
||||||
|
|
||||||
session.delete(entry)
|
session.delete(entry)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
return json.dumps("Entry #%s deleted." % entry_id)
|
return json.dumps("Entry #%s deleted." % entry_id)
|
||||||
|
except:
|
||||||
|
session.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user