Admin: add and delete a sport (WIP)
This commit is contained in:
@ -58,6 +58,45 @@ def get_sport(auth_user_id, sport_id):
|
||||
return jsonify(response_object), code
|
||||
|
||||
|
||||
@activities_blueprint.route('/sports', methods=['POST'])
|
||||
@authenticate
|
||||
def post_sport(auth_user_id):
|
||||
"""Post a sport"""
|
||||
sport_data = request.get_json()
|
||||
if not sport_data or sport_data.get('label') is None:
|
||||
response_object = {
|
||||
'status': 'error',
|
||||
'message': 'Invalid payload.'
|
||||
}
|
||||
return jsonify(response_object), 400
|
||||
|
||||
sports_list = []
|
||||
try:
|
||||
new_sport = Sport(label=sport_data.get('label'))
|
||||
db.session.add(new_sport)
|
||||
db.session.commit()
|
||||
sports_list.append({
|
||||
'id': new_sport.id,
|
||||
'label': new_sport.label
|
||||
})
|
||||
response_object = {
|
||||
'status': 'created',
|
||||
'data': {
|
||||
'sports': sports_list
|
||||
}
|
||||
}
|
||||
code = 201
|
||||
except (exc.IntegrityError, exc.OperationalError, ValueError) as e:
|
||||
db.session.rollback()
|
||||
appLog.error(e)
|
||||
response_object = {
|
||||
'status': 'error',
|
||||
'message': 'Error. Please try again or contact the administrator.'
|
||||
}
|
||||
code = 500
|
||||
return jsonify(response_object), code
|
||||
|
||||
|
||||
@activities_blueprint.route('/sports/<int:sport_id>', methods=['PATCH'])
|
||||
@authenticate
|
||||
def update_sport(auth_user_id, sport_id):
|
||||
@ -106,6 +145,44 @@ def update_sport(auth_user_id, sport_id):
|
||||
return jsonify(response_object), code
|
||||
|
||||
|
||||
@activities_blueprint.route('/sports/<int:sport_id>', methods=['DELETE'])
|
||||
@authenticate
|
||||
def delete_sport(auth_user_id, sport_id):
|
||||
"""Delete a sport"""
|
||||
sports_list = []
|
||||
try:
|
||||
sport = Sport.query.filter_by(id=sport_id).first()
|
||||
if sport:
|
||||
db.session.query(Sport).filter_by(id=sport_id).delete()
|
||||
db.session.commit()
|
||||
response_object = {
|
||||
'status': 'no content',
|
||||
'data': {
|
||||
'sports': sports_list
|
||||
}
|
||||
}
|
||||
code = 204
|
||||
print('OK')
|
||||
print(response_object)
|
||||
else:
|
||||
response_object = {
|
||||
'status': 'not found',
|
||||
'data': {
|
||||
'sports': sports_list
|
||||
}
|
||||
}
|
||||
code = 404
|
||||
except (exc.IntegrityError, exc.OperationalError, ValueError) as e:
|
||||
db.session.rollback()
|
||||
appLog.error(e)
|
||||
response_object = {
|
||||
'status': 'error',
|
||||
'message': 'Error. Please try again or contact the administrator.'
|
||||
}
|
||||
code = 500
|
||||
return jsonify(response_object), code
|
||||
|
||||
|
||||
@activities_blueprint.route('/activities', methods=['GET'])
|
||||
@authenticate
|
||||
def get_activities(auth_user_id):
|
||||
|
@ -117,6 +117,39 @@ def test_get_a_sport(app):
|
||||
assert 'cycling' in data['data']['sports'][0]['label']
|
||||
|
||||
|
||||
def test_add_a_sport(app):
|
||||
add_admin()
|
||||
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(
|
||||
email='admin@example.com',
|
||||
password='12345678'
|
||||
)),
|
||||
content_type='application/json'
|
||||
)
|
||||
response = client.post(
|
||||
'/api/sports',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(
|
||||
label='surfing'
|
||||
)),
|
||||
headers=dict(
|
||||
Authorization='Bearer ' + json.loads(
|
||||
resp_login.data.decode()
|
||||
)['auth_token']
|
||||
)
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 201
|
||||
assert 'created' in data['status']
|
||||
|
||||
assert len(data['data']['sports']) == 1
|
||||
assert 'surfing' in data['data']['sports'][0]['label']
|
||||
|
||||
|
||||
def test_update_a_sport(app):
|
||||
add_admin()
|
||||
add_sport('cycling')
|
||||
@ -149,3 +182,28 @@ def test_update_a_sport(app):
|
||||
|
||||
assert len(data['data']['sports']) == 1
|
||||
assert 'cycling updated' in data['data']['sports'][0]['label']
|
||||
|
||||
|
||||
def test_delete_a_sport(app):
|
||||
add_admin()
|
||||
add_sport('cycling')
|
||||
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(
|
||||
email='admin@example.com',
|
||||
password='12345678'
|
||||
)),
|
||||
content_type='application/json'
|
||||
)
|
||||
response = client.delete(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
headers=dict(
|
||||
Authorization='Bearer ' + json.loads(
|
||||
resp_login.data.decode()
|
||||
)['auth_token']
|
||||
)
|
||||
)
|
||||
assert response.status_code == 204
|
||||
|
Reference in New Issue
Block a user