def add_customer(customer_id, first_name, last_name, home_address,
                 phone_number, email_address, status, credit_limit):
    """funcion that adds a customer to the database with the given info"""
    try:
        with database.transaction():
            new_customer = Customer.create(customer_id=customer_id,
                                           first_name=first_name,
                                           last_name=last_name,
                                           home_address=home_address,
                                           phone_number=phone_number,
                                           email_address=email_address,
                                           status=status,
                                           credit_limit=credit_limit)
            new_customer.save()
    except peewee.IntegrityError:
        LOGGER.info('Error adding {customer_id} : {first_name} {last_name}')
        LOGGER.info(f'{customer_id} already exists in database')
        raise ValueError
    finally:
        database.close()
Beispiel #2
0
def update_customer_credit(customer_id, credit_limit):
    """Given a customer id, updates the customer's credit limit"""
    try:
        with database.transaction():
            selected_customer = Customer.get(Customer.customer_id == customer_id)
            selected_customer.credit_limit = float(credit_limit)
            selected_customer.save()
            LOGGER.info(f'Customers credit limit updated, {customer_id}')
    except peewee.DoesNotExist:
        LOGGER.info(f'Error occured updating credit limit for {customer_id}')
        LOGGER.info(f'{customer_id} not found in database')
        raise ValueError
Beispiel #3
0
 def tearDown(self):
     """Remves tables created by tests"""
     database.drop_tables([Customer])
Beispiel #4
0
 def setUp(self):
     """sets up the tests"""
     database.create_tables([Customer])
Beispiel #5
0
    """Given a customer ID, deletes customer from database"""
    try:
        selected_customer = Customer.get(Customer.customer_id == customer_id)
        selected_customer.delete_instance()
        LOGGER.info(f'Customer deleted from database, {customer_id}')
    except peewee.DoesNotExist:
        LOGGER.info(f'Error occured deleting {customer_id}')
        LOGGER.info(f'{customer_id} not found in database')
        raise ValueError

def update_customer_credit(customer_id, credit_limit):
    """Given a customer id, updates the customer's credit limit"""
    try:
        with database.transaction():
            selected_customer = Customer.get(Customer.customer_id == customer_id)
            selected_customer.credit_limit = float(credit_limit)
            selected_customer.save()
            LOGGER.info(f'Customers credit limit updated, {customer_id}')
    except peewee.DoesNotExist:
        LOGGER.info(f'Error occured updating credit limit for {customer_id}')
        LOGGER.info(f'{customer_id} not found in database')
        raise ValueError

def list_active_customers():
    """returns the count of the number of active customers"""
    return Customer.select().where(Customer.status == 'Active').count()

if __name__ == "__main__":
    database.init('customers.db')
    database.create_tables([Customer])