27 lines
791 B
Python
27 lines
791 B
Python
from banjo.urls import route_get, route_post
|
|
from banjo.http import NotFound
|
|
from app.models import Riddle
|
|
|
|
@route_get('all', args={})
|
|
def list_riddles(params):
|
|
riddles = Riddle.objects.all()
|
|
return {'riddles': [riddle.to_dict() for riddle in riddles]}
|
|
|
|
@route_post('new', args={'question': str, 'answer': str})
|
|
def create_riddle(params):
|
|
riddle = Riddle.from_dict(params)
|
|
riddle.save()
|
|
return riddle.to_dict()
|
|
|
|
@route_post('guess', args={'id': int, "answer": str})
|
|
def guess_answer(params):
|
|
try:
|
|
riddle = Riddle.objects.get(id=params['id'])
|
|
except Riddle.DoesNotExist:
|
|
raise NotFound("Riddle not found")
|
|
correct = riddle.check_guess(params['answer'])
|
|
return {
|
|
"guess": params['answer'],
|
|
"correct": correct,
|
|
}
|