def add_customer(customer_id, first_name, last_name, home_address, phone_number, email_address, is_active, credit_limit): """ add new customer to customer database :param customer_id:customer id :param first_name: customer first name :param last_name: customer last name :param home_address: customer address :param phone_number: customer cell phone number :param email_address: customer email address :param is_active: whether the customer is active :param credit_limit: customer credit limit :return: add a new customer to table """ try: LOGGER.info('Successfully connected to the database') 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, is_active=is_active, credit_limit=credit_limit) new_customer.save() LOGGER.info("Customer added successfully") except IntegrityError as error: LOGGER.info(error) LOGGER.info('Error occurred')
def add_customer(customer_id, name, last_name, home_address, phone_number, email, status, credit_limit): """ This function will add a new customer to the sqlite3 database. :param customer_id: integer representing a unique value that identifies a customer :param name: string containing the customer's first name :param last_name: string containing the customer's last name :param home_address: string containing the customer's address :param phone_number: string containing the customer's phone number :param email: string containing the customer's email address :param status: boolean representing the customer's status :param credit_limit: float containing the customer's credit limit """ try: with DATABASE.transaction(): new_customer = Customer.create(customer_id=customer_id, name=name, last_name=last_name, home_address=home_address, phone_number=phone_number, email=email, status=status, credit_limit=credit_limit) new_customer.save() LOGGER.info("A new customer record, %s %s, was added", name, last_name) except IntegrityError as integrity_error: LOGGER.error("A IntegrityError occurred %s", integrity_error)
def update_customer_credit(customer_id, credit_limit): """ Update the credit limit for a customer via customer_id """ try: customer_to_update = Customer.get(Customer.customer_id == customer_id) with DATABASE.transaction(): logging.debug( f"Updating customer with customer_id: {customer_id}") customer_to_update.credit_limit = credit_limit customer_to_update.save() except DoesNotExist: logging.error(f"Unable to find customer with id: {customer_id}") raise DoesNotExist
def delete_customer(customer_id): """ Delete a customer from the databsse via customer_id """ try: customer_to_delete = Customer.get(Customer.customer_id == customer_id) with DATABASE.transaction(): logging.debug( f"Deleting customer with customer_id: {customer_id}") customer_to_delete.delete_instance() customer_to_delete.save() logging.debug("Successfully deleted customer") except DoesNotExist: logging.error(f"Unable to find customer with id: {customer_id}") raise DoesNotExist
def delete_customer(customer_id): """Deletes a customer from the database.""" init_database() try: with database.transaction(): customer = Customer.get_by_id(customer_id) customer.delete_instance() logging.info('Customer with ID %s successfully deleted.', customer_id) return True except peewee.DoesNotExist: logging.error('Customer delete with ID %s failed, not in database..', customer_id) return False finally: database.close()
def add_customer(name, lastname, home_address, phone_number, email_address, status, credit_limit): """create a new customer object in the database table""" try: with DATABASE.transaction(): new_customer = Customer.create(name=name, lastname=lastname, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) new_customer.save() LOGGER.info('Database add successful') return new_customer except pw.IntegrityError as exc: LOGGER.info("Error creating %s; Exception: %s", name, exc)
def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): """ Add a new customer to the database """ try: logging.debug(f"Adding new customer: {name} {lastname}") with DATABASE.transaction(): 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) logging.debug("Customer added successfully") except IntegrityError: logging.error(f"Error adding customer: {name} {lastname}") raise IntegrityError
def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): ''' This function will add a new customer to the sqlite3 database. ''' try: # id is automatically created and incremented by 1 with DATABASE.transaction(): new_customer = Customer.create( id=customer_id, name=name, last_name=lastname, home_address=home_address, phone_number=phone_number, email=email_address, status=status, credit_limit=credit_limit) new_customer.save() LOGGER.info(f'Database add successful for customerId {name}') except (OperationalError, DoesNotExist) as exc: LOGGER.info(f'Error creating = {name}') LOGGER.info(exc)
def update_customer_credit(customer_id, credit_limit): """ Update customer credit limit by searching customer id. :param customer_id: ID of the customer :param credit_limit: Credit limit to be set for the customer :return: None """ try: LOGGER.info('Update credit limit for customer.') with DATABASE.transaction(): a_customer = Customer.get(Customer.customer_id == customer_id) a_customer.credit_limit = int(credit_limit) a_customer.save() LOGGER.info('customer credit limit has updated') except Customer.DoesNotExist as error: LOGGER.info('Updated failed.') LOGGER.info('Customer with id %s not found.', customer_id) LOGGER.info(error) raise ValueError
def update_customer_credit(customer_id, credit_limit): """ Search an existing customer by customer_id and update their credit limit or raise a ValueError exception if the customer does not exist :param customer_id: Integer representing the customer id :param credit_limit: Float representing the credit limit dollar amount :raise ValueError: Raised if the customer id does not exist """ LOGGER.info("Updating credit limit for Customer ID %s", customer_id) try: result_customer = Customer.get_by_id(customer_id) with DATABASE.transaction(): result_customer.credit_limit = credit_limit result_customer.save() LOGGER.info("Updating customer, %s %s's credit limit to %s", result_customer.name, result_customer.last_name, credit_limit) except DoesNotExist: LOGGER.info( "Can not modify credit limit because customer ID %s doesn't exist.", customer_id) raise ValueError
def setUp(self): """Defines starting test database used for function testing.""" self.starting_db = [(1, 'Bob', 'Bobbo', '12 Green St', '1112223344', '*****@*****.**', False, 85000), (2, 'Jane', 'Janeo', '1550 Red Rd', '1118675309', '*****@*****.**', True, 150000), (5, 'Wilson', 'Volleyball', '1 Castaway Island', '0000000000', '*****@*****.**', True, 0) ] database.init(TEST_DATABASE) database.connect() database.execute_sql('PRAGMA foreign_keys = ON') database.create_tables([Customer]) with database.transaction(): Customer.delete().execute() Customer.insert_many(self.starting_db, fields=[Customer.customer_id, Customer.name, Customer.lastname, Customer.home_address, Customer.phone_number, Customer.email_address, Customer.active_status, Customer.credit_limit ]).execute() database.close()
def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): """Adds a new customer to the database.""" init_database() try: with database.transaction(): new_customer = Customer.create(customer_id=customer_id, name=name, lastname=lastname, home_address=home_address, phone_number=phone_number, email_address=email_address, active_status=status, credit_limit=credit_limit) new_customer.save() logging.info('New customer, ID %s, added successfully.', customer_id) return True except peewee.IntegrityError as exc: logging.error('Error creating new customer with ID %s: %s.', customer_id, exc) return False finally: database.close()