def MakePurchase(self, request, context): print("Making Purchase") # Check the credit card information total_cost = 0.0 for i in range(len(request.item_ids)): item_id = request.item_ids[i] quantity = request.quantities[i] item_found = False for i_product in range(len(self.database.products)): if self.database.products[i_product].item_id == item_id: # Making the purchase item_found = True if self.database.products[i_product].quantity >= quantity: sale_price = self.database.products[i_product].sale_price total_cost += (sale_price * quantity) self.database.remove_item(i_product, quantity) # self.database.products[i_product].quantity -= quantity # Update the sellers info for i_seller in range(len(self.database.sellers)): if self.database.sellers[i_seller].seller_id == self.database.products[i_product].seller_id: self.database.make_sale(i_seller, quantity) # self.database.sellers[i_seller].num_items_sold += quantity break break else: error = f"Insufficent Quantity of item {self.database.products[i_product].item_id}. In-stock: {self.database.products[i_product].quantity}, Req: {quantity}" return product_pb2.Confirmation(status=False, error=error) if not item_found: error = f"Unable to locate item_id: {item_id}" return product_pb2.Confirmation(status=False, error=error) return product_pb2.Confirmation(status=True, error="")
def LeaveFeedback(self, request, context): print("Leaving Feedback") # Find seller id seller_id = None for p in self.database.products: if p.item_id == request.item_id: seller_id = p.seller_id if seller_id is not None: # Add feedback to seller for i in range(len(self.database.sellers)): if self.database.sellers[i].seller_id == seller_id: if request.feedback_type not in list(self.database.sellers[i].feedback.keys()): error = "Improper feedback request format" return product_pb2.Confirmation(status=False, error=error) self.database.leave_feedback(i, request.feedback_type) # self.database.sellers[i].feedback[request.feedback_type] += 1 return product_pb2.Confirmation(status=True, error="") return product_pb2.Confirmation(status=False, error="Could not locate seller id") else: return product_pb2.Confirmation(status=False, error="Item not found")
def ChangePrice(self, request, context): print("Changing Price") matched = False for i in range(len(self.database.products)): if self.database.products[i].item_id == request.item_id: self.database.change_price(i, request.sale_price) matched = True if matched: error = "" updated = True else: error = "No items matched" updated = False return product_pb2.Confirmation(status=updated, error=error)
def DeleteItem(self, request, context): print("Removing Items") matched = False for i in range(len(self.database.products)): if self.database.products[i].item_id == request.item_id: # Delete item matched = True self.database.remove_item(i, request.quantity) break if matched: error = "" updated = True else: error = "No items matched" updated = False return product_pb2.Confirmation(status=updated, error=error)
def ChangeLogin(self, request, context): user = request.username found = False error = "" for i in range(len(self.database.sellers)): # print(f"{s.name} : {s.password} : {s.logged_in}") if self.database.sellers[i].username == user : if request.logging_in and self.database.sellers[i].password == request.password: self.database.change_login(i, True) found = True print(f"Logging in {user}") elif not request.logging_in: self.database.change_login(i, False) found = True print(f"Logging out {user}") else: error = "Wrong Password!" print(error) if not found: error = "User not found or password incorrect" return product_pb2.Confirmation(status=found, error=error)
def CreateUser(self, request, context): print("Creating User") seller_id = len(self.database.sellers) self.database.add_seller( request.name, seller_id, request.username, request.password ) print("Returning") return product_pb2.Confirmation(status=True, error="")