browser-go-api/api/rooms/api_rooms.py

48 lines
1.4 KiB
Python
Raw Normal View History

2019-10-09 22:42:38 +00:00
from flask import Blueprint, request, jsonify, session
2019-10-09 00:18:40 +00:00
from models.User import User, user_schema, users_schema
2019-10-09 23:27:41 +00:00
from models.GameRoom import GameRoom, rooms_schema, room_schema
2019-10-09 22:42:38 +00:00
from database import db
2019-10-09 05:53:37 +00:00
from ..decorators import jwt_required
2019-10-09 00:18:40 +00:00
api_rooms = Blueprint('api_rooms', __name__, url_prefix='/api/rooms')
2019-10-09 07:08:37 +00:00
@api_rooms.route('/<room_id>', methods=['GET'])
2019-10-09 00:18:40 +00:00
def get_room():
pass
@api_rooms.route('/', methods=['GET'])
def get_rooms():
2019-10-09 23:27:41 +00:00
rooms = GameRoom.query.all()
response = rooms_schema.dumps(rooms)
2019-10-09 00:18:40 +00:00
return jsonify(response)
# protected route
@api_rooms.route('/', methods=['POST'])
2019-10-09 05:53:37 +00:00
@jwt_required()
2019-10-09 00:18:40 +00:00
def post_room():
2019-10-09 22:42:38 +00:00
print('Hey it\'s a POST request')
2019-10-09 05:53:37 +00:00
data = request.get_json()
2019-10-09 22:42:38 +00:00
print(data)
2019-10-09 05:53:37 +00:00
try:
room = GameRoom(
name = data['name'],
description = data['description'],
2019-10-09 22:42:38 +00:00
# TODO add support for private rooms and multiple languages
# private = data['private'],
# language = data['language']
2019-10-09 05:53:37 +00:00
)
db.session.add(room)
db.session.commit()
response = {
'status': 'success',
'message': 'Succesfully registered.',
'gameRoom': room.id
}
return jsonify(response), 201
except Exception as e:
print(e.__dict__)
response = {
'status': 'fail',
'message': 'There was an error. Please try again.'
}
return jsonify(response), 401