Exemple #1
0
def delete_question(event, context):
    try:
        token = validate_authorization_header_is_present(event['headers'])
        auth_utils.decode_auth_token(token)
        question_dao.delete(event['pathParameters']['question_id'])
        return build_response_without_body(204)

    except errors.ApiError as ae:
        return errors.build_response_from_api_error(ae)

    except Exception as e:
        return errors.build_response_from_api_error(errors.ApiError(errors.internal_server_error, e))
Exemple #2
0
def create_question(event, context):
    print('creating question')
    try:
        token = validate_authorization_header_is_present(event['headers'])

        auth_utils.decode_auth_token(token)

        question_request = QuestionModel.from_request(json.loads(event['body']))
        validate_quiz_exists(question_request.quiz_id)
        return build_response_with_body(201, question_dao.create(question_request).to_dict())

    except errors.ApiError as ae:
        return errors.build_response_from_api_error(ae)

    except Exception as e:
        return errors.build_response_from_api_error(errors.ApiError(errors.internal_server_error, e))
def get_by_flashcard_deck_id(event, context):
    try:
        token = validate_authorization_header_is_present(event['headers'])
        auth_utils.decode_auth_token(token)

        flashcard_deck = flashcard_deck_dao.get_by_id(
            event['pathParameters']['flashcard_deck_id'])

        return build_response_with_body(200, flashcard_deck.to_dict())

    except errors.ApiError as ae:
        return errors.build_response_from_api_error(ae)

    except Exception as e:
        return errors.build_response_from_api_error(
            errors.ApiError(errors.internal_server_error, e))
Exemple #4
0
def delete_quiz(event, context):
    try:
        token = validate_authorization_header_is_present(event['headers'])
        auth_utils.decode_auth_token(token)
        quiz_dao.delete(event['pathParameters']['quiz_id'])
        return build_response_without_body(204)

    except errors.ApiError as ae:
        return errors.build_response_from_api_error(ae)

    except KeyError as e:
        return errors.build_response_from_api_error(
            errors.Error('MISSING_REQUIRED_PARAMETER', str(e), 400))

    except Exception as e:
        return errors.build_response_from_api_error(
            errors.ApiError(errors.internal_server_error, e))
Exemple #5
0
def create_flashcard(event, context):
    try:
        token = validate_authorization_header_is_present(event['headers'])
        auth_utils.decode_auth_token(token)
        flashcard_request = FlashcardModel.from_request(
            json.loads(event['body']))
        validate_flashcard_deck_exists(flashcard_request.flashcard_deck_id)
        return build_response_with_body(
            201,
            flashcard_dao.create(flashcard_request).to_dict())

    except errors.ApiError as ae:
        return errors.build_response_from_api_error(ae)

    except Exception as e:
        return errors.build_response_from_api_error(
            errors.ApiError(errors.internal_server_error, e))
Exemple #6
0
def update_question(event, context):
    try:
        token = validate_authorization_header_is_present(event['headers'])
        auth_utils.decode_auth_token(token)
        question_model = QuestionModel.from_update_request(json.loads(event['body']))
        quiz = question_dao.update(question_model)

        logging.debug(quiz)
        logging.debug(quiz.to_dict())

        return build_response_with_body(200, quiz.to_dict())

    except errors.ApiError as ae:
        return errors.build_response_from_api_error(ae)

    except Exception as e:
        return errors.build_response_from_api_error(errors.ApiError(errors.internal_server_error, e))
Exemple #7
0
def get_by_user_id(event, context):
    try:
        logging.debug('event headers: ', event['headers'])
        token = validate_authorization_header_is_present(event['headers'])
        auth_utils.decode_auth_token(token)

        items = quiz_dao.get_by_user_id(event['pathParameters']['user_id'])

        quizzes = [item.to_dict() for item in items if items]

        return build_response_with_body(200, quizzes)

    except errors.ApiError as ae:
        return errors.build_response_from_api_error(ae)

    except Exception as e:
        return errors.build_response_from_api_error(
            errors.ApiError(errors.internal_server_error, e))
Exemple #8
0
def get_quiz_by_id(event, context):
    try:
        token = validate_authorization_header_is_present(event['headers'])
        auth_utils.decode_auth_token(token)
        quiz = quiz_dao.get_by_id(event['pathParameters']['quiz_id'])

        print('********* getting quiz')

        logging.debug(quiz)
        logging.debug(quiz.to_dict())

        return build_response_with_body(200, quiz.to_dict())

    except errors.ApiError as ae:
        return errors.build_response_from_api_error(ae)

    except Exception as e:
        return errors.build_response_from_api_error(
            errors.ApiError(errors.internal_server_error, e))
Exemple #9
0
def update_flashcard(event, context):
    try:
        token = validate_authorization_header_is_present(event['headers'])
        auth_utils.decode_auth_token(token)
        flashcard_request = FlashcardModel.from_update_request(
            json.loads(event['body']))
        flashcard = flashcard_dao.update(flashcard_request)

        logging.debug(flashcard)
        logging.debug(flashcard.to_dict())

        return build_response_with_body(200, flashcard.to_dict())

    except errors.ApiError as ae:
        return errors.build_response_from_api_error(ae)

    except Exception as e:
        return errors.build_response_from_api_error(
            errors.ApiError(errors.internal_server_error, e))
Exemple #10
0
def create_quiz(event, context):
    try:
        token = validate_authorization_header_is_present(event['headers'])
        auth_utils.decode_auth_token(token)
        quiz_request = QuizModel.from_request(json.loads(event['body']))
        logging.debug(quiz_request.user_id)
        validate_user_exists_by_id(quiz_request.user_id)
        quiz = quiz_dao.create(quiz_request)

        logging.debug(quiz)
        logging.debug(quiz.to_dict())

        return build_response_with_body(201, quiz.to_dict())

    except errors.ApiError as ae:
        return errors.build_response_from_api_error(ae)

    except Exception as e:
        return errors.build_response_from_api_error(
            errors.ApiError(errors.internal_server_error, e))