Beispiel #1
0
        public ActionResult GetRedirects(int page = 1, int limit = 20, string type = null, string text = null, Guid?rootNodeKey = null)
        {
            try {
                // Initialize the search options
                RedirectsSearchOptions options = new() {
                    Page        = page,
                    Limit       = limit,
                    Type        = EnumUtils.ParseEnum(type, RedirectTypeFilter.All),
                    Text        = text,
                    RootNodeKey = rootNodeKey
                };

                // Make the search for redirects via the redirects service
                RedirectsSearchResult result = _redirects.GetRedirects(options);

                // Map the result for the API
                return(new JsonResult(_backOffice.Map(result)));
            } catch (RedirectsException ex) {
                if (!ex.Is404)
                {
                    _logger.LogError(ex, ex.Message);
                }

                // Generate the error response
                return(Error(ex));
            }
        }
        //private bool TryCreateUrlHelper(out UrlHelper helper) {

        //    _linkGenerator.GetUmbracoApiService<RedirectsController>("Dummy").TrimEnd("Dummy");

        //    // Get the current HTTP context via the Umbraco context accessor
        //    HttpContextBase http = _httpContextAccessor.HttpContext.u
        //    if (http == null) {
        //        helper = null;
        //        return false;
        //    }

        //    // Initialize a new URL helper
        //    helper = new UrlHelper(http.Request.RequestContext);
        //    return true;

        //}

        /// <summary>
        /// Maps the specified <paramref name="result"/> to a corresponding object to be returned in the API.
        /// </summary>
        /// <param name="result">The search result to be mapped.</param>
        /// <returns>An instance of <see cref="object"/>.</returns>
        public virtual object Map(RedirectsSearchResult result)
        {
            Dictionary <Guid, RedirectRootNodeModel> rootNodeLookup = new Dictionary <Guid, RedirectRootNodeModel>();
            Dictionary <Guid, IContent> contentLookup = new Dictionary <Guid, IContent>();
            Dictionary <Guid, IMedia>   mediaLookup   = new Dictionary <Guid, IMedia>();

            IEnumerable <RedirectModel> items = result.Items
                                                .Select(redirect => Map(redirect, rootNodeLookup, contentLookup, mediaLookup));

            return(new {
                result.Pagination,
                items
            });
        }
Beispiel #3
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);
        }