def show(user_id): log.info(f"Getting user {user_id}") if not validate_uuid(user_id): raise A55ApiError.invalid_uuid_parameter() user = UserRepository.get_by_external_id(user_id) if user is None: raise A55ApiError.user_not_found() log.info(f"Returning {user.__str__()}") return user
def show(ticket): log.info(f"Getting ticket {ticket}") if not validate_uuid(ticket): raise A55ApiError.invalid_uuid_parameter() credit_request = CreditRequestRepository.get_by_ticket(ticket) if credit_request is None: raise A55ApiError.ticket_not_found() log.info(f"Returning {credit_request.__str__()}") return credit_request
def delete(user_id): log.info(f"Deleting user {user_id}") if not validate_uuid(user_id): raise A55ApiError.invalid_uuid_parameter() user = UserRepository.get_by_external_id(user_id) if user is None: raise A55ApiError.user_not_found() user.delete() log.info(f"Successfully deleted user {user_id}") return ""
def create(): log.info("Creating user") if not request.json: raise A55ApiError.invalid_body_format() try: create_user_schema.load(request.json) except ValidationError as err: log.error(f"Validation errors: {err.messages}") raise A55ApiError.invalid_body_input() user_data = request.json user = create_new_user(user_data) log.info(f"Returning {user.__str__()}") return user
def create(): log.info("Requesting credit") if not request.json: raise A55ApiError.invalid_body_format() try: create_credit_request_schema.load(request.json) except ValidationError as err: log.error(f"Validation errors: {err.messages}") raise A55ApiError.invalid_body_input() credit_request_data = request.json credit_request = request_credit(credit_request_data) log.info(f"Returning {credit_request.__str__()}") return credit_request
def update(user_id): log.info(f"Updating user {user_id}") if not validate_uuid(user_id): raise A55ApiError.invalid_uuid_parameter() if not request.json: raise A55ApiError.invalid_body_format() try: update_user_schema.load(request.json) except ValidationError as err: log.error(f"Validation errors: {err.messages}") raise A55ApiError.invalid_body_input() user_data = request.json user = update_user(user_data, user_id) log.info(f"Returning {user.__str__()}") return user
def update_user(user_data, user_id): payload = { "name": user_data["name"], "birth_date": datetime.fromisoformat(user_data["birth_date"]) } user = UserRepository.get_by_external_id(user_id) if user is None: raise A55ApiError.user_not_found() for key, value in payload.items(): setattr(user, key, value) user.save() return user
def request_credit(credit_request_data): user_external_id = credit_request_data["user_id"] user = UserRepository.get_by_external_id(user_external_id) if user is None: raise A55ApiError.user_not_found() payload = { "amount_required": credit_request_data["amount_required"], "user_id": user.id } credit_request = CreditRequest(**payload) credit_request.save() validator_data = { "amount": credit_request.amount_required, "ticket": credit_request.ticket, "age": user.age } validate_credit_request.delay(**validator_data) return credit_request