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 get(self): products = Product.find_all() if products is None: error = NotFoundError("Results") display = ErrorView.display(error) return get_response(display, 404) product_name = request.args.get('name') if product_name is None: feed = Product.get_feed(products) display = ProductView.display_list(feed) return get_response(display, 200) product = Product.find_by_name(products, product_name) if product is None: error = NotFoundError("Product") display = ErrorView.display(error) return get_response(display, 404) discount_rate = product["discount_rate"] product["discount"] = Product.get_percentage(discount_rate) original_price = product["original_price"] price = Product.get_price(discount_rate, original_price) product["price"] = price display = ProductView.display_details(product) return get_response(display, 200)
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 get(self, customer_id): cart = model.find_by_customer(customer_id) if 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)
def get(self, customer_id, order_id): order = model.find_by(order_id) if order is not None: display = view.display_details(order) return get_response(display, 200) if order is None: error = NotFoundError("Order Number") display = ErrorView.display(error) return get_response(display, 404)
def get(self, customer_id): orders = model.find_by_customer(customer_id) if orders is not None: display = view.display_list(orders) return get_response(display, 200) if orders is None: error = NotFoundError("Results") display = ErrorView.display(error) return get_response(display, 404)
def get(self, _id): customer = model.find_by(_id) if customer is not None: customer["middle_initial"] = model.get_middle_initial(customer) customer["full_name"] = model.get_full_name(customer) display = view.display(customer) return get_response(display, 200) if customer is None: display = ErrorView.display(NotFoundError("Customer")) return get_response(display, 404)
def get(self, _id): products = Product.find_all() if products is None: error = NotFoundError("Results") display = ErrorView.display(error) return get_response(display, 404) product = Product.find_by(products, _id) if product is None: error = NotFoundError("Product") display = ErrorView.display(error) return get_response(display, 404) discount_rate = product["discount_rate"] product["discount"] = Product.get_percentage(discount_rate) original_price = product["original_price"] product["price"] = Product.get_price(discount_rate, original_price) display = ProductView.display_details(product) return get_response(display, 200)
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 get(self, _id): products = model.find_all() if products: product = model.find_by(products, _id) if product: original_price = product["original_price"] rate = product["discount_rate"] product["price"] = model.get_price(rate, original_price) display = view.display_details(product) return get_response(display, 200) if product is None: error = NotFoundError("Product") display = ErrorView.display(error) return get_response(display, 404) if products is None or not products: error = NotFoundError("Results") display = ErrorView.display(error) return get_response(display, 404)
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)
def get(self): products = model.find_all() if products: for product in products: original_price = product["original_price"] discount_rate = product["discount_rate"] product["price"] = model.get_price(discount_rate, original_price) display = view.display_list(products) return get_response(display, 200) if products is None or not products: error = NotFoundError("Results") display = ErrorView.display(error) return get_response(display, 404)
def post(self, customer_id): cart = Cart.find_by_customer(customer_id) if cart is not None: error = ResourceAlreadyExistError("Cart") display = ErrorView.display(error) return get_response(display, 409) new_cart = CartModel(customer_id, []).__dict__ state = Cart.add(new_cart) if state: new_cart["sub_total"] = Cart.get_sub_total(new_cart) new_cart["number_of_items"] = Cart.count_all_items(new_cart) new_cart["tax_amount"] = 0 new_cart["total"] = 0 return get_response(new_cart, 201)
from api.v1.common import get_response from api.v1.models.Product import Product from api.v1.views.ProductView import ProductView from error.model import NotFoundError from error.model import BadRequestError from error.model import UnprocessableEntityError from error.model import ResourceAlreadyExistError from error.view import ErrorView from flask import request from flask_restful import Resource model = Product() view = ProductView() ErrorView = ErrorView() class ProductController(Resource): def get(self): products = model.find_all() if products: for product in products: original_price = product["original_price"] discount_rate = product["discount_rate"] product["price"] = model.get_price(discount_rate, original_price) display = view.display_list(products) return get_response(display, 200)
def handle_unauthorized_error(e): view = ErrorView.display(UnauthorizedError()) return get_response(view, 403)
def handle_access_denied_error(e): view = ErrorView.display(AccessDeniedError()) return get_response(view, 401)
def handle_internal_server_error(e): view = ErrorView.display(InternalServerError()) return get_response(view, 500)
def handle_page_not_found(e): view = ErrorView.display(NotFoundError("Page")) return get_response(view, 404)