Added delete endpoint and tests.

This commit is contained in:
Nicholas Staples
2016-01-12 10:39:49 +00:00
parent 752a359d3d
commit 31f10c7457
8 changed files with 88 additions and 29 deletions

View File

@@ -1,7 +1,8 @@
from flask import (jsonify, request)
from sqlalchemy.exc import DataError
from sqlalchemy.orm.exc import NoResultFound
from app.dao.services_dao import (save_model_service, get_model_services)
from app.dao.services_dao import (
save_model_service, get_model_services, delete_model_service)
from app.dao.users_dao import get_model_users
from app.dao import DAOException
from .. import service
@@ -26,7 +27,7 @@ def create_service():
# TODO auth to be added
@service.route('/<int:service_id>', methods=['PUT'])
@service.route('/<int:service_id>', methods=['PUT', 'DELETE'])
def update_service(service_id):
try:
service = get_model_services(service_id=service_id)
@@ -34,19 +35,24 @@ def update_service(service_id):
return jsonify(result="error", message="Invalid service id"), 400
except NoResultFound:
return jsonify(result="error", message="Service not found"), 404
# TODO there has got to be a better way to do the next three lines
upd_serv, errors = service_schema.load(request.get_json())
if errors:
return jsonify(result="error", message=errors), 400
update_dict, errors = service_schema.dump(upd_serv)
# TODO FIX ME
# Remove update_service model which is added to db.session
db.session.rollback()
try:
save_model_service(service, update_dict=update_dict)
except DAOException as e:
return jsonify(result="error", message=str(e)), 400
return jsonify(data=service_schema.dump(service).data)
if request.method == 'DELETE':
status_code = 202
delete_model_service(service)
else:
status_code = 200
# TODO there has got to be a better way to do the next three lines
upd_serv, errors = service_schema.load(request.get_json())
if errors:
return jsonify(result="error", message=errors), 400
update_dict, errors = service_schema.dump(upd_serv)
# TODO FIX ME
# Remove update_service model which is added to db.session
db.session.rollback()
try:
save_model_service(service, update_dict=update_dict)
except DAOException as e:
return jsonify(result="error", message=str(e)), 400
return jsonify(data=service_schema.dump(service).data), status_code
# TODO auth to be added.