61 lines
1.5 KiB
Python
61 lines
1.5 KiB
Python
|
from flask import Blueprint, jsonify
|
||
|
|
||
|
from .models import User
|
||
|
|
||
|
users_blueprint = Blueprint('users', __name__)
|
||
|
|
||
|
|
||
|
@users_blueprint.route('/users', methods=['GET'])
|
||
|
def get_users():
|
||
|
"""Get all users"""
|
||
|
users = User.query.all()
|
||
|
users_list = []
|
||
|
for user in users:
|
||
|
user_object = {
|
||
|
'id': user.id,
|
||
|
'username': user.username,
|
||
|
'email': user.email,
|
||
|
'created_at': user.created_at
|
||
|
}
|
||
|
users_list.append(user_object)
|
||
|
response_object = {
|
||
|
'status': 'success',
|
||
|
'data': {
|
||
|
'users': users_list
|
||
|
}
|
||
|
}
|
||
|
return jsonify(response_object), 200
|
||
|
|
||
|
|
||
|
@users_blueprint.route('/users/<user_id>', methods=['GET'])
|
||
|
def get_single_user(user_id):
|
||
|
"""Get single user details"""
|
||
|
response_object = {
|
||
|
'status': 'fail',
|
||
|
'message': 'User does not exist'
|
||
|
}
|
||
|
try:
|
||
|
user = User.query.filter_by(id=int(user_id)).first()
|
||
|
if not user:
|
||
|
return jsonify(response_object), 404
|
||
|
else:
|
||
|
response_object = {
|
||
|
'status': 'success',
|
||
|
'data': {
|
||
|
'username': user.username,
|
||
|
'email': user.email,
|
||
|
'created_at': user.created_at
|
||
|
}
|
||
|
}
|
||
|
return jsonify(response_object), 200
|
||
|
except ValueError:
|
||
|
return jsonify(response_object), 404
|
||
|
|
||
|
|
||
|
@users_blueprint.route('/ping', methods=['GET'])
|
||
|
def ping_pong():
|
||
|
return jsonify({
|
||
|
'status': 'success',
|
||
|
'message': 'pong!'
|
||
|
})
|