def rounds_endpoint(): """GET end point to return rounds information""" auth = Auth(app) if not auth.authenticate_request(): return auth.unauthorized_response() round_model = Round() query_response = round_model.get_all_rounds() if query_response: json = [] for row in query_response: race_model = Race() race_ids = race_model.get_round_race_ids(row.id) json.append({ "id": row.id, "name": row.name, "start_date": row.start_date, "end_date": row.end_date, "races": [race_id.id for race_id in race_ids] }) return json return { 'status': 'Failed', 'message': 'Rounds not found' }, status.HTTP_404_NOT_FOUND
def single_race_endpoint(id): """GET end point to return a single race's information""" auth = Auth(app) if not auth.authenticate_request(): return auth.unauthorized_response() race = Race() single_race_query = race.get_race(id) single_race_participants = RaceParticipants( ).get_race_participants_race_id(id) if single_race_query: json = [] snails_id_list = [] for row in single_race_participants: snails_id_list.append(row.id_snail) json.append({ "id": single_race_query.id, "date": single_race_query.date, "status": single_race_query.status, "id_round": single_race_query.id_round, "id_snails": snails_id_list }) return json return { 'status': 'Failed', 'message': 'Race not found' }, status.HTTP_404_NOT_FOUND
def races_endpoint(): """GET end point to return races information""" auth = Auth(app) if not auth.authenticate_request(): return auth.unauthorized_response() race = Race() race_query = race.get_all_races() all_race_participants = RaceParticipants() if race_query: json = [] for each_race in race_query: race_participants = all_race_participants.get_race_participants_race_id( each_race.id) snails_id_list = [] for row in race_participants: snails_id_list.append(row.id_snail) json.append({ "id": each_race.id, "date": each_race.date, "status": each_race.status, "id_round": each_race.id_round, "id_snails": snails_id_list }) return json return { 'status': 'Failed', 'message': 'Races not found' }, status.HTTP_404_NOT_FOUND
def single_snail_endpoint(id): """GET end point to return single snail information""" auth = Auth(app) if not auth.authenticate_request(): return auth.unauthorized_response() snail = Snail() query_response = snail.get_snail(id) if query_response is not None: trainer = Trainer() query_response_trainer = trainer.get_trainer(query_response.trainer_id) return { "id": query_response.id, "name": query_response.name, "trainer": { "id": query_response_trainer.id, "name": query_response_trainer.name } } return { 'status': 'Failed', 'message': 'Snail not found' }, status.HTTP_404_NOT_FOUND
def snails_endpoint(): """GET end point to return snails information""" auth = Auth(app) if not auth.authenticate_request(): return auth.unauthorized_response() snail = Snail() query_response = snail.get_all_snails() if query_response: trainer = Trainer() json = [] for row in query_response: query_response_trainer = trainer.get_trainer(row.trainer_id) json.append({ "id": row.id, "name": row.name, "trainer": { "id": query_response_trainer.id, "name": query_response_trainer.name } }) return json return { 'status': 'Failed', 'message': 'Snails not found' }, status.HTTP_404_NOT_FOUND
def single_result_endpoint(id): """GET end point to return single result information""" auth = Auth(app) if not auth.authenticate_request(): return auth.unauthorized_response() return single_result_json(id)
def results_endpoint(): """GET end point to return results information""" auth = Auth(app) if not auth.authenticate_request(): return auth.unauthorized_response() return results_json()
def snails_endpoint(id): """Endpoint to get information for a round with specified id. Retrieves the round information in json format for the specified id. Args: id (int): The id of the requested round. Returns: JSON: The json representation of the round with specified id. """ auth = Auth(app) if not auth.authenticate_request(): return auth.unauthorized_response() round_model = Round() query_response = round_model.get_round(id) if query_response: race_model = Race() race_ids = race_model.get_round_race_ids(query_response.id) return { "id": query_response.id, "name": query_response.name, "start_date": query_response.start_date, "end_date": query_response.end_date, "races": [race_id.id for race_id in race_ids] } return { 'status': 'Failed', 'message': 'Round id: %s not found' % id }, status.HTTP_404_NOT_FOUND