class SessionController(Resource): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__productservice = ProductService() self.__sessionservice = SessionService() @auth_required() @sessionNS.doc(security=["token"]) @sessionNS.param("sessionid", description="The desired session ID", _in="path", required=True) @sessionNS.param("payload", description="Optional", _in="body", required=False) @sessionNS.expect(REQUESTMODEL) @sessionNS.response(200, "Success", RESPONSEMODEL) @sessionNS.response(204, "No products found", ERRORMODEL) @sessionNS.response(400, "Bad Request", ERRORMODEL) @sessionNS.response(401, "Unauthorized", ERRORMODEL) @sessionNS.response(404, "Not Found", {}) @sessionNS.response(500, "Unexpected Error", ERRORMODEL) @sessionNS.response(504, "Error while accessing the gateway server", ERRORMODEL) def post(self, sessionid): """Session information.""" try: in_data = SearchRequest.parse_json() session = self.__sessionservice.select_by_id(sessionid) sessions = self.__sessionservice.select(gender=session.gender) total = self.__productservice.get_total(sessionid=sessionid, **in_data) brands = self.__productservice.select_brands(sessionid=sessionid, **in_data) kinds = self.__productservice.select_kinds(sessionid=sessionid, **in_data) pricerange = self.__productservice.select_pricerange( sessionid=sessionid) jsonsend = SessionResultsResponse.marshall_json({ "sessions": sessions, "total": total, "brands": brands, "kinds": kinds, "pricerange": pricerange }) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
class ProductTotalController(Resource): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__productservice = ProductService() @auth_required() @productTotalNS.doc(security=["token"]) @productTotalNS.param("payload", description="Required", _in="body", required=True) @productTotalNS.expect(REQUESTMODEL) @productTotalNS.response(200, "Success", RESPONSEMODEL) @productTotalNS.response(401, "Unauthorized", ERRORMODEL) @productTotalNS.response(400, "Bad Request", ERRORMODEL) @productTotalNS.response(500, "Unexpected Error", ERRORMODEL) @productTotalNS.response(504, "Error while accessing the gateway server", ERRORMODEL) def post(self): """Product list total price.""" try: in_data = ProductListRequest.parse_json() _, total = self.__productservice.select_by_item_list(**in_data) jsonsend = ProductTotalResponse.marshall_json({"total": total}) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
class KindProductsController(Resource): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__productservice = ProductService() @auth_required() @kindProductsNS.doc(security=["token"]) @kindProductsNS.param("kind", description="The desired kind", _in="path", required=True) @kindProductsNS.param("page", description="The search page.", _in="path", required=True) @kindProductsNS.param("payload", description="Optional", _in="body", required=False) @kindProductsNS.expect(REQUESTMODEL) @kindProductsNS.response(200, "Success", RESPONSEMODEL) @kindProductsNS.response(204, "No content", ERRORMODEL) @kindProductsNS.response(400, "Bad Request", ERRORMODEL) @kindProductsNS.response(401, "Unauthorized", ERRORMODEL) @kindProductsNS.response(500, "Unexpected Error", ERRORMODEL) @kindProductsNS.response(504, "Error while accessing the gateway server", ERRORMODEL) def post(self, kind, page): """Kind products paginated""" try: in_data = SearchProductsRequest.parse_json() products = self.__productservice.select(kind=kind, page=page, **in_data) jsonsend = SearchProductsResultsResponse.marshall_json( { "products": [p.get_dict_min() for p in products] } ) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
class ProductController(Resource): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__productservice = ProductService() @auth_required() @productNS.doc(security=["token"]) @productNS.param("productid", description="The desired product ID", _in="path", required=True) @productNS.response(200, "Success", RESPONSEMODEL) @productNS.response(401, "Unauthorized", ERRORMODEL) @productNS.response(404, "Not Found", {}) @productNS.response(500, "Unexpected Error", ERRORMODEL) @productNS.response(504, "Error while accessing the gateway server", ERRORMODEL) def get(self, productid): """Product information.""" try: product = self.__productservice.select_by_id(productid) jsonsend = ProductResultsResponse.marshall_json(product.get_dict()) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
class GenderController(Resource): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__productservice = ProductService() self.__sessionservice = SessionService() @auth_required() @genderNS.doc(security=["token"]) @genderNS.param("gender", description="The desired gender", _in="path", required=True) @genderNS.param("payload", description="Optional", _in="body", required=False) @genderNS.expect(REQUESTMODEL) @genderNS.response(200, "Success", RESPONSEMODEL) @genderNS.response(204, "No Content", {}) @genderNS.response(400, "Bad Request", ERRORMODEL) @genderNS.response(401, "Unauthorized", ERRORMODEL) @genderNS.response(500, "Unexpected Error", ERRORMODEL) @genderNS.response(504, "Error while accessing the gateway server", ERRORMODEL) def post(self, gender="Men"): """Gender information.""" try: in_data = GenderRequest.parse_json() discounts = self.__productservice.super_discounts(gender=gender, **in_data) sessions = self.__sessionservice.select(gender=gender) brands = self.__productservice.select_brands(gender=gender) kinds = self.__productservice.select_kinds(gender=gender) jsonsend = GenderResultsResponse.marshall_json({ "discounts": [d.get_dict_min() for d in discounts], "sessions": sessions, "brands": brands, "kinds": kinds }) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
class StartController(Resource): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__productservice = ProductService() @auth_required() @startNS.doc(security=["token"]) @startNS.response(200, "Success", RESPONSEMODEL) @startNS.response(401, "Unauthorized", ERRORMODEL) @startNS.response(500, "Unexpected Error", ERRORMODEL) @startNS.response(504, "Error while accessing the gateway server", ERRORMODEL) def get(self): """Total products registered.""" try: numproducts = self.__productservice.products_count() jsonsend = ProductsCountResponse.marshall_json({"count": numproducts}) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__productservice = ProductService()
def service(): service = ProductService() return service