Exemplo n.º 1
0
        /// <summary>
        /// Returns an array of redirects where the destination matches the specified <paramref name="nodeType"/> and <paramref name="nodeKey"/>.
        /// </summary>
        /// <param name="nodeType">The type of the destination node.</param>
        /// <param name="nodeKey">The key (GUID) of the destination node.</param>
        /// <returns>An array of <see cref="IRedirect"/>.</returns>
        protected virtual IRedirect[] GetRedirectsByNodeKey(string nodeType, Guid nodeKey)
        {
            // Create a new scope
            using IScope scope = _scopeProvider.CreateScope();

            // Generate the SQL for the query
            Sql <ISqlContext> sql = scope.SqlContext
                                    .Sql()
                                    .Select <RedirectDto>().From <RedirectDto>()
                                    .Where <RedirectDto>(x => x.DestinationType == nodeType && x.DestinationKey == nodeKey);

            // Make the call to the database
            var rows = scope.Database
                       .Fetch <RedirectDto>(sql)
                       .Select(x => (IRedirect)Redirect.CreateFromDto(x))
                       .ToArray();

            // Complete the scope
            scope.Complete();

            return(rows);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns a paginated list of redirects matching the specified <paramref name="options"/>.
        /// </summary>
        /// <param name="options">The options the returned redirects should match.</param>
        /// <returns>An instance of <see cref="RedirectsSearchResult"/>.</returns>
        public RedirectsSearchResult GetRedirects(RedirectsSearchOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            RedirectsSearchResult result;

            using (var scope = _scopeProvider.CreateScope()) {
                // Generate the SQL for the query
                var sql = scope.SqlContext.Sql().Select <RedirectDto>().From <RedirectDto>();

                // Search by the rootNodeId
                if (options.RootNodeKey != null)
                {
                    sql = sql.Where <RedirectDto>(x => x.RootKey == options.RootNodeKey.Value);
                }

                // Search by the type
                if (options.Type != RedirectTypeFilter.All)
                {
                    string type = options.Type.ToPascalCase();
                    sql = sql.Where <RedirectDto>(x => x.DestinationType == type);
                }

                // Search by the text
                if (string.IsNullOrWhiteSpace(options.Text) == false)
                {
                    string[] parts = options.Text.Split('?');

                    if (parts.Length == 1)
                    {
                        if (int.TryParse(options.Text, out int redirectId))
                        {
                            sql = sql.Where <RedirectDto>(x => x.Id == redirectId || x.Path.Contains(options.Text) || x.QueryString.Contains(options.Text));
                        }
                        else if (Guid.TryParse(options.Text, out Guid redirectKey))
                        {
                            sql = sql.Where <RedirectDto>(x => x.Key == redirectKey || x.Path.Contains(options.Text) || x.QueryString.Contains(options.Text));
                        }
                        else
                        {
                            sql = sql.Where <RedirectDto>(x => x.Path.Contains(options.Text) || x.QueryString.Contains(options.Text));
                        }
                    }
                    else
                    {
                        string url   = parts[0];
                        string query = parts[1];
                        sql = sql.Where <RedirectDto>(x => (
                                                          x.Path.Contains(options.Text)
                                                          ||
                                                          (x.Path.Contains(url) && x.QueryString.Contains(query))
                                                          ));
                    }
                }

                // Order the redirects
                sql = sql.OrderByDescending <RedirectDto>(x => x.Updated);

                // Make the call to the database
                RedirectDto[] all = scope.Database.Fetch <RedirectDto>(sql).ToArray();

                // Calculate variables used for the pagination
                int limit  = options.Limit;
                int pages  = (int)Math.Ceiling(all.Length / (double)limit);
                int page   = Math.Max(1, Math.Min(options.Page, pages));
                int offset = (page * limit) - limit;

                // Apply pagination and wrap the database rows
                IRedirect[] items = all
                                    .Skip(offset)
                                    .Take(limit)
                                    .Select(x => (IRedirect)Redirect.CreateFromDto(x))
                                    .ToArray();

                // Return the items (on the requested page)
                result = new RedirectsSearchResult(all.Length, limit, offset, page, pages, items);

                scope.Complete();
            }

            return(result);
        }