示例#1
0
def search_customer(customer_id):
    """
        Searches customer by ID
        returns: dict; with name, lastname, email, phone
                 (or)
                 empty if no customer found
    """
    logging.info("searching customer ID %s...", customer_id)
    try:
        with DB.transaction():
            # query database for customer by ID
            searched_customer = Customer.get(
                Customer.customer_id == customer_id)
            logging.info("successfully found customer!")
            # return dict with name, lastname, email, phone number
            return {
                "name": searched_customer.name,
                "lastname": searched_customer.lastname,
                "email_address": searched_customer.email_address,
                "phone_number": searched_customer.phone_number
            }

    except DoesNotExist:
        LOGGER.error("DoesNotExisterror: customer ID %s not found!",
                     customer_id)
        # return empty dict
        return {}
示例#2
0
def add_customer(customer_id, name, lastname, home_address, phone_number,
                 email_address, status, credit_limit):
    """
        Adds a new customer to the database
        returns:
    """
    logging.info("attempting to add customer: %s %s...", name, lastname)
    try:
        with DB.transaction():
            # add new customer entry
            new_customer = Customer.create(customer_id=customer_id,
                                           name=name,
                                           lastname=lastname,
                                           home_address=home_address,
                                           phone_number=phone_number,
                                           email_address=email_address,
                                           status=status,
                                           credit_limit=credit_limit)

            # save new customer entry in database
            new_customer.save()
            LOGGER.info("successfully saved new customer %s %s!", name,
                        lastname)

    except IntegrityError:
        LOGGER.error("IntegrityError occurred while creating customer %s %s!",
                     name, lastname)
示例#3
0
def add_customer(customer_id: int, name: str, lastname: str, home_address: str,
                 phone_number: str, email_address: str, status: bool,
                 credit_limit: float):
    """
    Add a new customer to the database.

    :param customer_id: int, Customer ID
    :param name: str, First name
    :param lastname: str, Last name
    :param home_address: str, Home address
    :param phone_number: str, Phone number
    :param email_address: str, Email address
    :param status: bool, True for "active", False for "inactive"
    :param credit_limit: float, credit limit
    """

    # Create a new customer object
    with DB.transaction():
        try:
            cust = Customer.create(id=customer_id,
                                   name=name,
                                   last_name=lastname,
                                   address=home_address,
                                   phone=phone_number,
                                   email=email_address,
                                   status=status,
                                   credit_limit=credit_limit)
        except pw.IntegrityError as err:
            raise ValueError(err)
        else:
            cust.save()
            logging.info("Added customer with ID=%d", customer_id)
示例#4
0
def delete_customer(customer_id):
    """
        Deletes a customer from the database
        returns:
    """
    logging.info("attempting to delete customer ID %s...", customer_id)
    try:
        with DB.transaction():
            # query database for customer by ID
            to_delete = Customer.get(Customer.customer_id == customer_id)
            # delete the customer found
            to_delete.delete_instance()
            logging.info("successfully deleted customer!")

    except DoesNotExist:
        LOGGER.error("DoesNotExisterror: customer ID %s delete failed!",
                     customer_id)
def update_customer_credit(customer_id: int, credit_limit: float):
    """
    Updates an existing customer's credit limit. Raises a ValueError if no such
    customer exists.

    :param customer_id: int, customer ID
    :param credit_limit: float, new credit limit
    """

    try:
        res = _get_by_id(customer_id)
    except pw.DoesNotExist:
        raise ValueError(f"No customer with ID {customer_id}")
    else:
        with DB.transaction():
            res.credit_limit = credit_limit
            res.save()
示例#6
0
def update_customer_credit(customer_id, credit_limit):
    """
        Searches customer by ID and updates credit limit
        returns:
    """
    logging.info("attempting to update customer ID %s with limit: %s...",
                 customer_id, credit_limit)
    try:
        with DB.transaction():
            # query database for customer by ID
            update_customer = Customer.get(Customer.customer_id == customer_id)
            # update credit limit
            update_customer.credit_limit = credit_limit
            update_customer.save()
            logging.info("successfully updated credit limit!")
    except DoesNotExist:
        LOGGER.error("error: customer ID %s update failed!", customer_id)
        # if customer does not exist, raise ValueError
        raise ValueError
示例#7
0
def add_customer(customer):
    """
    This function will add new custome to Customer table
    in customer.db
    """
    try:
        with DB.transaction():
            logging.info('Adding \"%s\" as new customer',
                         (customer[NAME], customer[LASTNAME]))
            new_customer = Customer.create(name=customer[NAME],
                                           lastname=customer[LASTNAME],
                                           home_address=customer[ADDRESS],
                                           phone_number=customer[PHONE],
                                           email_address=customer[EMAIL],
                                           status=customer[STATUS],
                                           credit_limit=customer[LIMIT])
            logging.debug('New Customer : \"%s \" added to the database',\
                        (customer[NAME], customer[LASTNAME]))
            new_customer.save()
    except peewee.IntegrityError as exp:
        logging.debug('IntegrityError, Error adding customer %s', exp)