def put(self, customer_id, item_id): data = ItemController.parser.parse_args() cart = model.find_by_customer(customer_id) if cart: item = model.edit_item(cart, item_id, data) if item: cart["sub_total"] = model.get_sub_total(cart) cart["number_of_items"] = model.count_all_items(cart) cart["tax_amount"] = model.get_tax_amount(cart) cart["total"] = model.get_total(cart) display = view.display_cart(cart) return get_response(display, 200) if cart is None: error = NotFoundError("Cart") display = ErrorView.display(error) return get_response(display, 404) error = UnprocessableEntityError() display = ErrorView.display(error) return get_response(display, 422)
def post(self): data = SignupController.parser.parse_args() valid_email = model.is_email_valid(data["email"]) if valid_email: customer = model.find_by_email(data["email"]) if customer is not None: error = ResourceAlreadyExistError("User") display = ErrorView.display(error) return get_response(display, 409) new_customer = Customer(data["email"], data["password"], data["first_name"], data["middle_name"], data["last_name"]).__dict__ state = model.add(new_customer) if state is not None: display = view.display_credentials(new_customer) return get_response(display, 201) error = UnprocessableEntityError() display = ErrorView.display(error) return get_response(display, 422)
def post(self, customer_id): data = OrderController.parser.parse_args() new_order = Order(data["customer_name"], data["phone_number"], data["shipping_address"], data["billing_address"], data["delivery_date"], data["payment_method"], data["payment_date"], data["shipping_fee"], data["tax_rate"], data["items"], customer_id).__dict__ state = model.add(new_order) if state is not None: new_order["sub_total"] = model.get_sub_total(new_order) new_order["tax_amount"] = model.get_tax_amount(new_order) new_order["number_of_items"] = model.count_all_items(new_order) new_order["total"] = model.get_total(new_order) display = view.display_details(new_order) return get_response(display, 201) if state is None: error = UnprocessableEntityError() display = ErrorView.display(error) return get_response(display, 422)
def put(self, customer_id, order_id): data = request.get_json() orders = model.find_all() order = model.edit(orders, order_id, data) if order is not None: display = view.display_details(order) return get_response(display, 200) if not order: error = NotFoundError("Order Number") display = ErrorView.display(error) return get_response(display, 404) if order is None: error = UnprocessableEntityError() display = ErrorView.display(error) return get_response(display, 422)
def post(self): data = LoginController.parser.parse_args() customer = model.find_by_email(data["email"]) if customer is not None: input_password = data["password"] password = customer['password'] valid_password = model.is_password_valid(input_password, password) if valid_password: display = view.display_credentials(customer) return get_response(display, 200) if customer is None or not valid_password: error = BadRequestError("Login failed", "Invalid email/password") display = ErrorView.display(error) return get_response(display, 400) error = UnprocessableEntityError() display = ErrorView.display(error) get_response(display, error)
def post(self, customer_id): cart = model.find_by_customer(customer_id) if cart is None or not cart: new_cart = Cart(customer_id, []).__dict__ state = model.add(new_cart) if state: new_cart["sub_total"] = model.get_sub_total(new_cart) new_cart["number_of_items"] = model.count_all_items(new_cart) new_cart["tax_amount"] = 0 new_cart["total"] = 0 return get_response(new_cart, 201) if cart is not None: error = ResourceAlreadyExistError("Cart") display = ErrorView.display(error) return get_response(display, 409) error = UnprocessableEntityError() display = ErrorView.display(error) return get_response(display, 422)
def delete(self, customer_id, item_id): cart = model.find_by_customer(customer_id) if cart is not None: item = model.remove_item(cart, item_id) if item is not None: cart["sub_total"] = model.get_sub_total(cart) cart["number_of_items"] = model.count_all_items(cart) cart["tax_amount"] = model.get_tax_amount(cart) cart["total"] = model.get_total(cart) display = view.display_cart(cart) return get_response(display, 200) if cart is None: error = NotFoundError("Cart") display = ErrorView.display(error) return get_response(display, 404) error = UnprocessableEntityError() display = ErrorView.display(error), return get_response(display, 422)