def get_product(productId): dynamodb = get_db_resource() table = dynamodb.Table(table_name) response = table.get_item(Key={'productId': productId}) # logger.info("Logger Response: ") # logger.info(response) if 'Item' not in response: return json.dumps({'error': 'Product does not exist'}) item = response['Item'] product = { 'productId': item['productId'], # 'productName': item['productName'], 'productDescription': item['productDescription'], # 'supplier': item['supplier'], 'imageUrl': item['imageUrl'], 'currency': item['currency'], # 'price': item['price'], # 'category': item['category'], 'rate': item['rate'], 'interestPaymentPeriod': item['interestPaymentPeriod'], 'interestPaymentDate': item['interestPaymentDate'], 'createdDate': item['createdDate'], 'updatedDate': item['updatedDate'], } return json.dumps({'product': product})
def get_customer(customerId): dynamodb = get_db_resource() table = dynamodb.Table(table_name) response = table.get_item(Key={'customerId': customerId}, ConsistentRead=True) # logger.info("Logger Response: ") # logger.info(response) if 'Item' not in response: raise Exception("CustomerNotFound") item = response['Item'] customer = { 'customerId': item['customerId'], 'firstName': item['firstName'], 'lastName': item['lastName'], 'email': item['email'], 'userName': item['userName'], 'birthDate': item['birthDate'], 'gender': item['gender'], 'custNumber': item['custNumber'], 'cardNumber': item['cardNumber'], 'custAccountNo': item['custAccountNo'], 'phoneNumber': item['phoneNumber'], 'createdDate': item['createdDate'], 'updatedDate': item['updatedDate'], 'profilePhotoUrl': item['profilePhotoUrl'], } return json.dumps({'customer': customer})
def get_all_products(): dynamodb = get_db_resource() table = dynamodb.Table(table_name) response = table.scan(Select='ALL_ATTRIBUTES') # logger.info("Logger Response: ") # logger.info(response) product_list = defaultdict(list) for item in response["Items"]: product = { 'productId': item['productId'], # 'productName': item['productName'], 'productDescription': item['productDescription'], # 'supplier': item['supplier'], 'imageUrl': item['imageUrl'], 'currency': item['currency'], # 'price': item['price'], # 'category': item['category'], 'rate': item['rate'], 'interestPaymentPeriod': item['interestPaymentPeriod'], 'interestPaymentDate': item['interestPaymentDate'], 'createdDate': item['createdDate'], 'updatedDate': item['updatedDate'], } product_list["products"].append(product) return json.dumps(product_list)
def create_customer(customer_dict): customerId = str(customer_dict['userName']) # str(uuid.uuid4()) firstName = str(customer_dict['firstName']) lastName = str(customer_dict['lastName']) email = str(customer_dict['email']) userName = str(customer_dict['userName']) birthDate = str(customer_dict['birthDate']) gender = str(customer_dict['gender']) custNumber = customer_number_generator() cardNumber = card_number_generator() custAccountNo = "0000000000000000000000" phoneNumber = str(customer_dict['phoneNumber']) createdDate = str(datetime.datetime.now().isoformat()) updatedDate = "1900-01-01T00:00:00.000000" profilePhotoUrl = str(customer_dict['profilePhotoUrl']) unique = is_unique(customerId, email, userName, custNumber, cardNumber) if unique: dynamodb = get_db_resource() table = dynamodb.Table(table_name) response = table.put_item(TableName=table_name, Item={ 'customerId': customerId, 'firstName': firstName, 'lastName': lastName, 'email': email, 'userName': userName, 'birthDate': birthDate, 'gender': gender, 'custNumber': custNumber, 'cardNumber': cardNumber, 'custAccountNo': custAccountNo, 'phoneNumber': phoneNumber, 'createdDate': createdDate, 'updatedDate': updatedDate, 'profilePhotoUrl': profilePhotoUrl }) # logger.info("Logger Response: ") # logger.info(response) customer = { 'customerId': customerId, 'firstName': firstName, 'lastName': lastName, 'email': email, 'userName': userName, 'birthDate': birthDate, 'gender': gender, 'custNumber': custNumber, 'cardNumber': cardNumber, 'custAccountNo': custAccountNo, 'phoneNumber': phoneNumber, 'createdDate': createdDate, 'updatedDate': updatedDate, 'profilePhotoUrl': profilePhotoUrl, } return json.dumps({'customer': customer}) else: raise Exception('CustomerExists')
def get_all_customers(): dynamodb = get_db_resource() table = dynamodb.Table(table_name) response = table.scan(Select='ALL_ATTRIBUTES') logger.info("Logger Response: ") logger.info(response) customer_list = defaultdict(list) for item in response["Items"]: customer = { 'customerId': item['customerId'], 'firstName': item['firstName'], 'lastName': item['lastName'], 'email': item['email'], 'userName': item['userName'], 'birthDate': item['birthDate'], 'gender': item['gender'], 'custNumber': item['custNumber'], 'cardNumber': item['cardNumber'], 'custAccountNo': item['custAccountNo'], 'phoneNumber': item['phoneNumber'], 'createdDate': item['createdDate'], 'updatedDate': item['updatedDate'], 'profilePhotoUrl': item['profilePhotoUrl'], } customer_list["customers"].append(customer) return json.dumps(customer_list)
def create_product(product_dict): productId = product_id_generator() # str(uuid.uuid4()) productName = str(product_dict['productName']) productDescription = str(product_dict['productDescription']) # supplier str(product_dict['supplier']) imageUrl = str(product_dict['imageUrl']) currency = str(product_dict['currency']) # price = str(product_dict['price']) # category = str(product_dict['category']) rate = str(product_dict['rate']) interestPaymentPeriod = str(product_dict['interestPaymentPeriod']) interestPaymentDate = str(product_dict['interestPaymentDate']) createdDate = str(datetime.datetime.now().isoformat()) updatedDate = "1900-01-01T00:00:00.000000" dynamodb = get_db_resource() table = dynamodb.Table(table_name) response = table.put_item( TableName=table_name, Item={ 'productId': productId, # 'productName': productName, 'productDescription': productDescription, # 'supplier': supplier, 'imageUrl': imageUrl, 'currency': currency, # 'price': price, # 'category': category, 'rate': rate, 'interestPaymentPeriod': interestPaymentPeriod, 'interestPaymentDate': interestPaymentDate, 'createdDate': createdDate, 'updatedDate': updatedDate }) # logger.info("Logger Response: ") # logger.info(response) product = { 'productId': productId, # 'productName': productName, 'productDescription': productDescription, # 'supplier': supplier, 'imageUrl': imageUrl, 'currency': currency, # 'price': price, # 'category': category, 'rate': rate, 'interestPaymentPeriod': interestPaymentPeriod, 'interestPaymentDate': interestPaymentDate, 'createdDate': createdDate, 'updatedDate': updatedDate, } return json.dumps({'product': product})
def delete_product(productId): dynamodb = get_db_resource() table = dynamodb.Table(table_name) response = table.delete_item(TableName=table_name, Key={'productId': productId}) # logger.info("Logger Response: ") # logger.info(response) if 'Item' not in response: return json.dumps({'error': 'product does not exist'}) product = { 'productId': productId, } return json.dumps({'product': product})
def get_max_value(attribute): """Will scan the table for the maximum possible value given an attribute""" maximum = None dynamodb = get_db_resource() table = dynamodb.Table(table_name) response = table.scan( Select='SPECIFIC_ATTRIBUTES', AttributesToGet=[attribute], ConsistentRead=True, ) if response['Items'] == []: pass else: maximum = max([int(m[attribute]) for m in response['Items']]) return maximum
def delete_customer(customerId): dynamodb = get_db_resource() table = dynamodb.Table(table_name) response = table.delete_item(TableName=table_name, Key={'customerId': customerId}) logger.info("Logger Response: ") logger.info(response) if 'ConsumedCapacity' in response: raise Exception("CustomerNotFound") customer = { 'customerId': customerId, } return json.dumps({'customer': customer})
def is_unique(customerId, email, userName): """ Checks if email, userName, custNumber, cardNumber are unique Will return a list do duplicate fields """ dynamodb = get_db_resource() table = dynamodb.Table(table_name) filter_expression = Attr('customerId').eq(customerId) \ | Attr('email').eq(email) \ | Attr('userName').eq(userName) response = table.scan( Select='ALL_ATTRIBUTES', FilterExpression=filter_expression, ConsistentRead=True, ) return len(response['Items']) == 0
def get_customer(customerId): dynamodb = get_db_resource() table = dynamodb.Table(table_name) response = table.get_item(Key={'customerId': customerId}, ConsistentRead=True) # logger.info("Logger Response: ") # logger.info(response) if 'Item' not in response: raise Exception("CustomerNotFound") item = response['Item'] address = {} value = item.get('address') if bool(value): address = { 'address_1': item['address']['address_1'], 'address_2': item['address']['address_2'], 'city': item['address']['city'], 'state': item['address']['state'], 'country': item['address']['country'], 'zipcode': item['address']['zipcode'], } customer = { 'customerId': item['customerId'], 'firstName': item['firstName'], 'lastName': item['lastName'], 'email': item['email'], 'userName': item['userName'], 'birthDate': item['birthDate'], 'gender': item['gender'], 'phoneNumber': item['phoneNumber'], 'createdDate': item['createdDate'], 'updatedDate': item['updatedDate'], 'profilePhotoUrl': item['profilePhotoUrl'], 'address': address } return json.dumps({'customer': customer})
def get_all_customers(): dynamodb = get_db_resource() table = dynamodb.Table(table_name) response = table.scan(Select='ALL_ATTRIBUTES') customer_list = defaultdict(list) for item in response["Items"]: # if 'address' in item.keys(): address = {} value = item.get('address') if bool(value): address = { 'address_1': item['address']['address_1'], 'address_2': item['address']['address_2'], 'city': item['address']['city'], 'state': item['address']['state'], 'country': item['address']['country'], 'zipcode': item['address']['zipcode'], } customer = { 'customerId': item['customerId'], 'firstName': item['firstName'], 'lastName': item['lastName'], 'email': item['email'], 'userName': item['userName'], 'birthDate': item['birthDate'], 'gender': item['gender'], 'phoneNumber': item['phoneNumber'], 'createdDate': item['createdDate'], 'updatedDate': item['updatedDate'], 'profilePhotoUrl': item['profilePhotoUrl'], 'address': address } customer_list["customers"].append(customer) return json.dumps(customer_list)
def update_product(productId, product_dict): # productName = str(product_dict['productName']) productDescription = str(product_dict['productDescription']) # supplier = str(product_dict['supplier']) imageUrl = str(product_dict['imageUrl']) currency = str(product_dict['currency']) # price = str(product_dict['price']) # category = str(product_dict['category']) rate = str(product_dict['rate']) interestPaymentPeriod = str(product_dict['interestPaymentPeriod']) interestPaymentDate = str(product_dict['interestPaymentDate']) updatedDate = str(datetime.datetime.now().isoformat()) dynamodb = get_db_resource() table = dynamodb.Table(table_name) response = table.update_item( Key={'productId': productId}, UpdateExpression="""SET productDescription = :p_productDescription, imageUrl = :p_imageUrl, currency = :p_currency, rate = :p_rate, interestPaymentPeriod = :p_interestPaymentPeriod, interestPaymentDate = :p_interestPaymentDate, updatedDate = :p_updatedDate """, ExpressionAttributeValues={ # p_productName': productName, ':p_productDescription': productDescription, # p_supplier': supplier, ':p_imageUrl': imageUrl, ':p_currency': currency, # ':p_price': price, # ':p_category': category, ':p_rate': rate, ':p_interestPaymentPeriod': interestPaymentPeriod, ':p_interestPaymentDate': interestPaymentDate, ':p_updatedDate': updatedDate }, ReturnValues="ALL_NEW") # logger.info("Logger Response: ") # logger.info(response) if 'Item' not in response: return json.dumps({'error': 'Product does not exist'}) updated = response['Attributes'] product = { 'productId': updated['productId'], # 'productName': updated['productName'], 'productDescription': updated['productDescription'], # 'supplier': updated['supplier'], 'imageUrl': updated['imageUrl'], 'currency': updated['currency'], # 'price': updated['price'], # 'category': updated['category'], 'rate': updated['rate'], 'interestPaymentPeriod': updated['interestPaymentPeriod'], 'interestPaymentDate': updated['interestPaymentDate'], 'createdDate': updated['createdDate'], 'updatedDate': updated['updatedDate'], } return json.dumps({'product': product})
def update_customer(customerId, customer_dict): firstName = str(customer_dict['firstName']) lastName = str(customer_dict['lastName']) email = str(customer_dict['email']) userName = str(customer_dict['userName']) birthDate = str(customer_dict['birthDate']) gender = str(customer_dict['gender']) custAccountNo = str(customer_dict['custAccountNo']) phoneNumber = str(customer_dict['phoneNumber']) updatedDate = str(datetime.datetime.now().isoformat()) profilePhotoUrl = str(customer_dict['profilePhotoUrl']) dynamodb = get_db_resource() table = dynamodb.Table(table_name) try: response = table.update_item( Key={'customerId': customerId}, UpdateExpression="""SET firstName = :p_firstName, lastName = :p_lastName, email = :p_email, userName = :p_userName, birthDate = :p_birthDate, gender = :p_gender, custAccountNo = :p_custAccountNo, phoneNumber = :p_phoneNumber, updatedDate = :p_updatedDate, profilePhotoUrl = :p_profilePhotoUrl """, ConditionExpression="customerId = :p_customerId", ExpressionAttributeValues={ ':p_firstName': firstName, ':p_lastName': lastName, ':p_email': email, ':p_userName': userName, ':p_birthDate': birthDate, ':p_gender': gender, ':p_custAccountNo': custAccountNo, ':p_phoneNumber': phoneNumber, ':p_updatedDate': updatedDate, ':p_profilePhotoUrl': profilePhotoUrl, ':p_customerId': customerId }, ReturnValues="ALL_NEW") except Exception as e: raise Exception("CustomerNotFound") logger.info("Logger Response: ") logger.info(response) updated = response['Attributes'] customer = { 'customerId': updated['customerId'], 'firstName': updated['firstName'], 'lastName': updated['lastName'], 'email': updated['email'], 'userName': updated['userName'], 'birthDate': updated['birthDate'], 'gender': updated['gender'], 'custNumber': updated['custNumber'], 'cardNumber': updated['cardNumber'], 'custAccountNo': updated['custAccountNo'], 'phoneNumber': updated['phoneNumber'], 'createdDate': updated['createdDate'], 'updatedDate': updated['updatedDate'], 'profilePhotoUrl': updated['profilePhotoUrl'], } return json.dumps({'customer': customer})
def update_customer(customerId, customer_dict): """ logger.info("Customer Dict Response: ") logger.info(customer_dict) """ firstName = str(customer_dict['firstName']) lastName = str(customer_dict['lastName']) email = str(customer_dict['email']) userName = str(customer_dict['userName']) birthDate = str(customer_dict['birthDate']) gender = str(customer_dict['gender']) phoneNumber = str(customer_dict['phoneNumber']) updatedDate = str(datetime.datetime.now().isoformat()) profilePhotoUrl = str(customer_dict['profilePhotoUrl']) address_1 = str(customer_dict['address1']) address_2 = str(customer_dict['address2']) city = str(customer_dict['city']) state = str(customer_dict['region']) country = str(customer_dict['country']) zipcode = str(customer_dict['zipCode']) dynamodb = get_db_resource() table = dynamodb.Table(table_name) try: response = table.update_item( Key={'customerId': customerId}, UpdateExpression="""set firstName = :p_firstName, lastName = :p_lastName, email = :p_email, userName = :p_userName, birthDate = :p_birthDate, gender = :p_gender, phoneNumber = :p_phoneNumber, updatedDate = :p_updatedDate, profilePhotoUrl = :p_profilePhotoUrl, #attrName = :attrValue""", ExpressionAttributeNames={"#attrName": "address"}, ExpressionAttributeValues={ ':p_firstName': firstName, ':p_lastName': lastName, ':p_email': email, ':p_userName': userName, ':p_birthDate': birthDate, ':p_gender': gender, ':p_phoneNumber': phoneNumber, ':p_updatedDate': updatedDate, ':p_profilePhotoUrl': profilePhotoUrl, ':attrValue': { 'address_1': address_1, 'address_2': address_2, 'city': city, 'state': state, 'country': country, 'zipcode': zipcode } }, ReturnValues="ALL_NEW") except Exception as e: raise Exception("CustomerNotFound") """ logger.info("Logger Response: ") logger.info(response) """ updated_customer = response['Attributes'] """ customer = { 'customerId': updated['customerId'], 'firstName': updated['firstName'], 'lastName': updated['lastName'], 'email': updated['email'], 'userName': updated['userName'], 'birthDate': updated['birthDate'], 'gender': updated['gender'], 'phoneNumber': updated['phoneNumber'], 'createdDate': updated['createdDate'], 'updatedDate': updated['updatedDate'], 'profilePhotoUrl': updated['profilePhotoUrl'], } """ return json.dumps({'customer': updated_customer})