Esempio n. 1
0
        /// <summary>
        /// Get a list of Actors by search string
        ///
        /// The search is a "contains" search on actor name
        /// If q is empty, all actors are returned
        /// </summary>
        /// <param name="actorQueryParameters">search parameters</param>
        /// <returns>List of Actors or an empty list</returns>
        public async Task <IEnumerable <Actor> > GetActorsAsync(ActorQueryParameters actorQueryParameters)
        {
            if (actorQueryParameters == null)
            {
                throw new ArgumentNullException(nameof(actorQueryParameters));
            }

            string key = actorQueryParameters.GetKey();

            if (App.Cache && cache.Contains(key) && cache.Get(key) is List <Actor> ac)
            {
                return(ac);
            }

            string       sql = App.SearchService.GetActorIds(actorQueryParameters);
            List <Actor> res;

            if (!string.IsNullOrWhiteSpace(sql))
            {
                res = (List <Actor>) await InternalCosmosDBSqlQuery <Actor>(sql).ConfigureAwait(false);
            }
            else
            {
                res = new List <Actor>();
            }

            // add to cache
            if (App.Cache)
            {
                cache.Add(new CacheItem(key, res), cachePolicy);
            }

            return(res);
        }
Esempio n. 2
0
        /// <summary>
        /// Get a list of Actors by search string
        ///
        /// The search is a "contains" search on actor name
        /// If q is empty, all actors are returned
        /// </summary>
        /// <param name="actorQueryParameters">search parameters</param>
        /// <returns>List of Actors or an empty list</returns>
        public async Task <IEnumerable <Actor> > GetActorsAsync(ActorQueryParameters actorQueryParameters)
        {
            if (actorQueryParameters == null)
            {
                throw new ArgumentNullException(nameof(actorQueryParameters));
            }

            string key = actorQueryParameters.GetKey();

            if (App.UseCache && cache.Contains(key) && cache.Get(key) is List <Actor> ac)
            {
                return(ac);
            }

            string sql = ActorSelect;

            int offset = actorQueryParameters.GetOffset();
            int limit  = actorQueryParameters.PageSize;

            string offsetLimit = string.Format(CultureInfo.InvariantCulture, ActorOffset, offset, limit);

            if (!string.IsNullOrEmpty(actorQueryParameters.Q))
            {
                // convert to lower and escape embedded '
                actorQueryParameters.Q = actorQueryParameters.Q.Trim().ToLowerInvariant().Replace("'", "''", System.StringComparison.OrdinalIgnoreCase);

                if (!string.IsNullOrEmpty(actorQueryParameters.Q))
                {
                    // get actors by a "like" search on name
                    sql += string.Format(CultureInfo.InvariantCulture, $" and contains(m.textSearch, @q) ");
                }
            }

            sql += ActorOrderBy + offsetLimit;

            QueryDefinition queryDefinition = new QueryDefinition(sql);

            if (!string.IsNullOrEmpty(actorQueryParameters.Q))
            {
                queryDefinition.WithParameter("@q", actorQueryParameters.Q);
            }

            List <Actor> res = (List <Actor>) await InternalCosmosDBSqlQuery <Actor>(queryDefinition).ConfigureAwait(false);

            cache.Add(new System.Runtime.Caching.CacheItem(key, res), cachePolicy);

            return(res);
        }