async def add_collection(request: Request) -> JSONResponse: """Add new collection. :param collection: Collection key to add to Redis. :type collection: str :return: {"success": bool}. :rtype: JSONResponse """ collection = request.path_params["collection"] username = request.cookies.get("username") if not username: err = 'username not saved in cookies' logger.exception(err) raise ValueError(err) try: result_redis = redis_helper.add_collection_to_redis( REDIS, collection, username) result_mongo = mongo_helper.add_collection_to_mongo( MONGO, collection, username) result = result_redis["success"] and result_mongo["success"] return JSONResponse({ "success": result, "message": result_mongo["message"] }) except (ConnectionError, TimeoutError) as err: logger.exception("cannot connect to Redis. %s", err)
async def login(request: Request) -> JSONResponse: """Login user and save him into a session. :return: {"success": bool}. :rtype: JSONResponse """ try: params = await request.json() auth = Authenticator(MONGO) try: user_info = auth.login_user(params["login"], params["password"]) if user_info["success"]: response = JSONResponse(user_info) response.set_cookie("user_token", str(user_info["token"])) response.set_cookie("user_id", str(user_info["id"])) response.set_cookie("username", str(user_info["username"])) return response return JSONResponse({ "success": False, "username": user_info["username"], "userID": user_info["id"], }) except ValueError as err: logger.exception(err) except JSONDecodeError as err: logger.exception(err)
async def collections(request: Request) -> JSONResponse: """Return all collections. :return: List of collections. :rtype: JSONResponse """ username = request.cookies.get('username') try: data = redis_helper.get_all_collections(REDIS, username) data_decoded = [byte.decode("utf-8") for byte in data] result = redis_helper.format_set_dropdown(data_decoded) return JSONResponse(result) except (ConnectionError, TimeoutError) as err: logger.exception("cannot connect to Redis. %s", err)
async def editions(request: Request) -> JSONResponse: """Return all editions. :return: List of all editions. :rtype: JSONResponse """ try: data = redis_helper.get_all_editions(REDIS) data_decoded = [ byte.decode("utf-8").removeprefix("edition:") for byte in data ] result = redis_helper.format_dropdown(data_decoded) if result is None: logger.warning("'/editions' returned 0 values") return JSONResponse(result) except (ConnectionError, TimeoutError) as err: logger.exception("cannot connect to Redis. %s", err)
async def collection(request: Request) -> JSONResponse: """Return all cards from collection by its name. :param name: Collection key in Redis. :type name: str :return: List of card objects. :rtype: JSONResponse """ username = request.cookies.get('username') collection = request.path_params["name"] key = f"{username}:{collection}" try: data = redis_helper.get_collection(REDIS, key) data_decoded = [json.loads(byte.decode("utf-8")) for byte in data] result = [] # Add index, so there is a better value to set as key in Vue loops. for i, item in enumerate(data_decoded): item["id"] = i result.append(item) return JSONResponse(result) except (ConnectionError, TimeoutError) as err: logger.exception("cannot connect to Redis. %s", err)
async def suggest(request: Request) -> JSONResponse: """Return auto suggested cards. :param text: Text which cards need to contain. :type text: str :return: List of cards. :rtype: JSONResponse """ text = request.path_params["text"] try: data = redis_helper.get_suggestions(REDIS, text, 20) result = redis_helper.format_cards(data) return JSONResponse(result) except (ConnectionError, TimeoutError) as err: logger.exception(err) except IndexError as err: logger.exception(err) except TypeError as err: logger.exception(err)
async def add_card(request: Request) -> JSONResponse: """Add card to collection. :param collection: Collection key in Redis, where card should be saved. :type collection: str :param card: Card part of key in Redis. :type card: str :param edition: Edition part of card key in Redis. :type edition: str :param units: Number of units to save. :type units: int :return: {"success": bool}. :rtype: JSONResponse """ collection = request.path_params["collection"] card = request.path_params["card"] edition = request.path_params["edition"] units = request.path_params["units"] username = request.cookies.get('username') if not username: err = 'username not saved in cookie' logger.exception(err) raise ValueError(err) try: result_redis = redis_helper.add_card_to_redis(REDIS, username, collection, card, edition, units) result_mongo = mongo_helper.add_card_to_mongo(MONGO, username, collection, card, edition, units) result = result_redis['success'] and result_mongo['success'] return JSONResponse({'success': result}) except ValueError as err: logger.exception(err) except (ConnectionError, TimeoutError) as err: logger.exception("cannot connect to Redis. %s", err)