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-10 21:57:29 +00:00
|
|
|
from models.Game import Game, games_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-11 06:06:22 +00:00
|
|
|
from websockets.socket import new_room_notice, join_room_notice
|
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-10 21:57:29 +00:00
|
|
|
def get_room(room_id):
|
|
|
|
print(room_id)
|
|
|
|
games = Game.query.filter_by(game_room=room_id).all()
|
|
|
|
response = games_schema.dumps(games)
|
2019-10-11 06:06:22 +00:00
|
|
|
join_room_notice(room_id)
|
2019-10-10 21:57:29 +00:00
|
|
|
return jsonify(response)
|
2019-10-09 00:18:40 +00:00
|
|
|
|
|
|
|
@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(
|
2019-10-10 21:57:29 +00:00
|
|
|
name = data['name'],
|
|
|
|
description = data['description'],
|
|
|
|
# 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.',
|
|
|
|
}
|
2019-10-11 06:06:22 +00:00
|
|
|
new_room_notice(room_schema.dumps(room))
|
2019-10-09 05:53:37 +00:00
|
|
|
return jsonify(response), 201
|
|
|
|
except Exception as e:
|
2019-10-10 21:57:29 +00:00
|
|
|
print(e)
|
2019-10-09 05:53:37 +00:00
|
|
|
print(e.__dict__)
|
|
|
|
response = {
|
|
|
|
'status': 'fail',
|
|
|
|
'message': 'There was an error. Please try again.'
|
|
|
|
}
|
|
|
|
return jsonify(response), 401
|