def setMembership():
'''This function is used to store the new Membership in the database
Example : POST /api/v1/memberships HTTP/1.1
{ "m_type": "general", "discount" : 10, "description" : "some description"}
Result : {
"meta": {
"code": 200,
"message": "Created Successfully"
}
}
'''
with SessionManager(Session) as session:
try:
m_type = request.json['m_type']
discount = request.json['discount']
if not 0 <= int(discount) < 100:
return jsonify(error_envelop(400, 'DataError', 'Enter the valid discount amount (0 to 100)'))
description = request.json.get('description', 'No Description Available')
membership = Membership(m_type=m_type, discount=discount, description=description)
session.add(membership)
session.commit()
return jsonify(post_envelop(200, data = request.json))
except DataError: #this excepyion might probably occur if the value key has a value of non integer
return jsonify(error_envelop(400, 'DataError', 'Use the correct value'))
except IntegrityError:
return jsonify(error_envelop(400, 'IntegrityError','Value : {0} already exists'.format(m_type)))
except:
return jsonify(error_envelop(400,'UnknownError','Error need to be identified'))
评论列表
文章目录