class DeleteController(Resource): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__orderservice = OrderService() @auth_required() @deleteNS.doc(security=["token"]) @deleteNS.param("user_slug", description="User slug", _in="path", required=True) @deleteNS.param("order_slug", description="Order slug", _in="path", required=True) @deleteNS.response(200, description="Deleted with success", mask=False) @deleteNS.response(400, "Bad Request", ERRORMODEL) @deleteNS.response(401, "Unauthorized", ERRORMODEL) @deleteNS.response(404, "Not Found", ERRORMODEL) @deleteNS.response(500, "Unexpected Error", ERRORMODEL) @deleteNS.response(504, "No response from gateway server", ERRORMODEL) def delete(self, user_slug, order_slug): """Order delete.""" try: self.__orderservice.delete(user_slug=user_slug, order_slug=order_slug) return {}, 200 except Exception as error: return ErrorHandler(error).handle_error()
class InsertController(Resource): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__orderservice = OrderService() @auth_required() @insertNS.doc(security=["token"]) @insertNS.expect(REQUESTMODEL) @insertNS.response(201, description="Created", mask=False) @insertNS.response(400, "Bad Request", ERRORMODEL) @insertNS.response(401, "Unauthorized", ERRORMODEL) @insertNS.response(500, "Unexpected Error", ERRORMODEL) @insertNS.response(502, "Error while accessing the gateway server", ERRORMODEL) @insertNS.response(504, "No response from gateway server", ERRORMODEL) def put(self): """Order insert.""" try: in_data = OrderInsertRequest.parse_json() url = app.config["WILLSTORES_WS"] headers = { "Authorization": "Bearer %s" % os.getenv("ACCESS_TOKEN") } req = post("%s/api/product/total" % (url), headers=headers, json={"item_list": in_data["item_list"]}) req.raise_for_status() self.__orderservice.insert(**in_data) return {}, 201 except Exception as error: return ErrorHandler(error).handle_error()
class SelectBySlugController(Resource): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__orderservice = OrderService() @auth_required() @selectBySlugNS.doc(security=["token"]) @selectBySlugNS.param("user_slug", description="User slug", _in="path", required=True) @selectBySlugNS.param("order_slug", description="Order slug", _in="path", required=True) @selectBySlugNS.response(200, "Success", RESPONSEMODEL) @selectBySlugNS.response(400, "Bad Request", ERRORMODEL) @selectBySlugNS.response(401, "Unauthorized", ERRORMODEL) @selectBySlugNS.response(404, "Not Found", ERRORMODEL) @selectBySlugNS.response(500, "Unexpected Error", ERRORMODEL) @selectBySlugNS.response(502, "Error while accessing the gateway server", ERRORMODEL) @selectBySlugNS.response(504, "No response from gateway server", ERRORMODEL) def get(self, user_slug, order_slug): """Order information.""" try: order = self.__orderservice.select_by_slug(user_slug=user_slug, order_slug=order_slug) items_list = [item.to_dict() for item in order.items] items_info = {"item_list": items_list} url = app.config["WILLSTORES_WS"] headers = { "Authorization": "Bearer %s" % os.getenv("ACCESS_TOKEN") } req = post("%s/api/product/list" % (url), headers=headers, json=items_info) req.raise_for_status() result = req.json() for item in items_list: product = next(p for p in result["products"] if p["id"] == item["item_id"]) product["amount"] = item["amount"] jsonsend = OrderResponse.marshall_json( dict(order.to_dict(), **result)) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
class SelectByUserController(Resource): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__orderservice = OrderService() @auth_required() @selectByUserNS.doc(security=["token"]) @selectByUserNS.param("user_slug", description="User Slug", _in="path", required=True) @selectByUserNS.param("payload", description="Optional", _in="body", required=False) @selectByUserNS.expect(REQUESTMODEL) @selectByUserNS.response(200, "Success", RESPONSEMODEL) @selectByUserNS.response(204, "No Content", ERRORMODEL) @selectByUserNS.response(400, "Bad Request", ERRORMODEL) @selectByUserNS.response(401, "Unauthorized", ERRORMODEL) @selectByUserNS.response(500, "Unexpected Error", ERRORMODEL) @selectByUserNS.response(502, "Error while accessing the gateway server", ERRORMODEL) @selectByUserNS.response(504, "No response from gateway server", ERRORMODEL) def post(self, user_slug): """Orders for a user.""" try: in_data = UserOrdersRequest.parse_json() user_info = self.__orderservice.select_by_user_slug( user_slug=user_slug, **in_data) url = app.config["WILLSTORES_WS"] with Session() as sess: sess.headers["Authorization"] = "Bearer %s" % os.getenv( "ACCESS_TOKEN") processed_user_info = asyncio.run( process_orders(sess, url, user_info)) jsonsend = UserOrdersResponse.marshall_json(processed_user_info) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def service(): service = OrderService() return service
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__orderservice = OrderService()