Exemplo n.º 1
0
def delete_customer(customer_id):
    """Delete a customer from the customers database."""

    try:
        connect_customer_db()
        Customer.get_by_id(customer_id).delete_instance()
        logger.info(f"{customer_id} deleted from the database.")

    except DoesNotExist:
        logger.info(f"{customer_id} does not exist in the database.")
        raise DoesNotExist

    finally:
        db.close()
Exemplo n.º 2
0
    def test_update_customer_credit(self):
        """Test updating customer credit."""

        reset_db()
        add_customer("test_id1", "Rhianna", "doesntneedalastname",
                     "543 Sample Address, Winnebago, AK 99302", "3241232455",
                     "*****@*****.**", True, 333.33)

        # Make sure the current credit limit is 333.33
        self.assertEqual(
            Customer.get_by_id("test_id1").credit_limit, Decimal('333.33'))

        # Then update the record and make sure it changed
        update_customer_credit("test_id1", Decimal('1000.00'))
        self.assertEqual(
            Customer.get_by_id("test_id1").credit_limit, Decimal('1000.00'))
Exemplo n.º 3
0
def add_customer(customer_id, first_name, last_name, home_address,
                 phone_number, email_address, active_customer, credit_limit):
    """Add customer to customer_database"""

    try:
        connect_customer_db()
        with db.atomic():
            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,
                                           active_customer=active_customer,
                                           credit_limit=credit_limit)
            new_customer.save()
            logger.info(f"Added {customer_id} to the database.")

    except TypeError as err:
        logger.info(f"Could not add {customer_id} to the database.")
        logger.info(err)
        raise TypeError

    finally:
        db.close()
def delete_customer(customer_id):
    """use customer ID to find and delete a customer from the database"""
    try:
        remove_customer = Customer.get(Customer.customer_id == customer_id)
        remove_customer.delete_instance()
        logging.info(f"Customer {customer_id} was found, deleting from the database")
    except DoesNotExist:
        logging.info(f"Customer with id {customer_id} does not exist")
        logging.info(DoesNotExist)
        raise DoesNotExist
def update_customer_credit(customer_id, credit_limit):
    """Use customer ID to update the customer credit limit record"""
    try:
        update_customer = Customer.get(Customer.customer_id == customer_id)
        update_customer.credit_limit = credit_limit
        update_customer.save()
        logging.info(f"Customer with id of {customer_id} has had their credit limit changed to {credit_limit}")
    except DoesNotExist:
        logging.info(f"Customer with id {customer_id} does not exist")
        logging.info(DoesNotExist)
        raise DoesNotExist
Exemplo n.º 6
0
def list_active_customers():
    """Return the number of customers with an active_status of True."""

    try:
        connect_customer_db()
        active_count = Customer.select().where(
            Customer.active_customer).count()
        logger.info(f"There are currently {active_count} active customers.")
        return active_count

    finally:
        db.close()
def search_customer(customer_id):
    """use customer ID to return a dictionary object with the first name,
     last name, email address and phone number of that customer"""
    try:
        customer_found = Customer.get(Customer.customer_id == customer_id)
        logging.info(f"Customer found with id of {customer_id}")
        return {"first_name": customer_found.first_name,
                "last_name": customer_found.last_name,
                "email_address": customer_found.email_address,
                "phone_number": customer_found.phone_number}
    except DoesNotExist:
        logging.info(f"Customer id {customer_id} does not exist")
        logging.info(DoesNotExist)
        return {}
def add_customer(customer_id, first_name, last_name, mailing_address, phone_number,
                 email_address, active_status, credit_limit):
    """Add a new customer to the database"""
    try:
        new_customer = Customer.create(customer_id=customer_id, first_name=first_name,
                                       last_name=last_name, mailing_address=mailing_address,
                                       phone_number=phone_number, email_address=email_address,
                                       active_status=active_status, credit_limit=credit_limit)
        new_customer.save()
        logging.info(f"Add new customer with id {customer_id} -OK")
    except TypeError as oops:
        logging.info(f"Add new customer with id of {customer_id} failed")
        logging.info(oops)
        raise oops
Exemplo n.º 9
0
    def test_delete_customer(self):
        """Test deleting a record from db."""

        reset_db()
        add_customer("test_id1", "Rhianna", "doesntneedalastname",
                     "543 Sample Address, Winnebago, AK 99302", "3241232455",
                     "*****@*****.**", True, 333.33)

        # Make sure the record exists
        self.assertEqual(Customer.get_by_id("test_id1").first_name, "Rhianna")

        # Then delete the record and make sure it's gone
        delete_customer("test_id1")
        self.assertEqual(search_customer("test_id1"), {})
Exemplo n.º 10
0
    def test_add_customer(self):
        """Test adding new customer to the database."""

        reset_db()
        add_customer("test_id1", "Rhianna", "doesntneedalastname",
                     "543 Sample Address, Winnebago, AK 99302", "3241232455",
                     "*****@*****.**", True, 333.33)

        test_customer = Customer.get_by_id("test_id1")

        self.assertEqual(test_customer.first_name, "Rhianna")
        self.assertEqual(test_customer.phone_number, "3241232455")

        test_customer.delete_instance()
Exemplo n.º 11
0
    def test_list_credit_limits(self):
        """Test listing customers and their credit limits."""

        reset_db()
        add_customer("test_id1", "Rhianna", "doesntneedalastname",
                     "543 Sample Address, Winnebago, AK 99302", "3241232455",
                     "*****@*****.**", True, 333.33)

        # Make sure the current credit limit is 333.33
        self.assertEqual(
            Customer.get_by_id("test_id1").credit_limit, Decimal('333.33'))

        expected_result = "doesntneedalastname, Rhianna: 333.33"
        self.assertEqual(list_credit_limits()[0], expected_result)
Exemplo n.º 12
0
def update_customer_credit(customer_id, credit_limit):
    """Update a customer's credit limit."""

    try:
        connect_customer_db()
        customer = Customer.get_by_id(customer_id)
        customer.credit_limit = credit_limit
        customer.save()
        logger.info(
            f"Credit limit for {customer_id} updated to {credit_limit}.")

    except DoesNotExist as err:
        logger.info(f"{customer_id} does not exist in the database.")
        raise DoesNotExist

    finally:
        db.close()
Exemplo n.º 13
0
def list_credit_limits():
    """Return a list of active customers along with their credit limits."""

    try:
        connect_customer_db()

        # Creates a list of all active customers
        active_customers = Customer.select().where(Customer.active_customer)

        # Lists credit limits for those customers
        logger.info(f"Listing credit limits for all active customers.")
        return [
            f"{cust.last_name}, {cust.first_name}: {cust.credit_limit}"
            for cust in active_customers
        ]

    finally:
        db.close()
Exemplo n.º 14
0
def search_customer(customer_id):
    """
    Return dictionary object with a customer's name and contact info
    if found, otherwise return an empty dictionary object.
    """

    try:
        connect_customer_db()
        customer = Customer.get_by_id(customer_id)
        logger.info(f"{customer_id} found. Returning name and contact info.")

        return {
            'first_name': customer.first_name,
            'last_name': customer.last_name,
            'email_address': customer.email_address,
            'phone_number': customer.phone_number
        }

    except DoesNotExist:
        logger.info(f"{customer_id} does not exist in the database.")
        return {}

    finally:
        db.close()
def list_active_customers():
    """Give a count of only active status customers in the database"""
    active_customers = Customer.select().where(Customer.active_status).count()
    logging.info(f"The number of active customers is {active_customers}")
    return active_customers