Exemple #1
0
        public async Task <IActionResult> Get(
            int page        = 1,
            int size        = 10,
            string keywords = "",
            SortBy sort     = SortBy.LastReply,
            OrderBy order   = OrderBy.Desc)
        {
            // Get search settings
            var searchSettings = await _searchSettingsStore.GetAsync();

            // Set default sort column if auto is specified
            if (sort == SortBy.LastReply)
            {
                // Get search settings
                if (searchSettings != null)
                {
                    sort = searchSettings.SearchType == SearchTypes.Tsql
                        ? SortBy.LastReply
                        : SortBy.Rank;
                }
                else
                {
                    sort = SortBy.LastReply;
                }
            }

            // Get results
            var entities = await _searchService
                           .ConfigureDb(o =>
            {
                if (searchSettings != null)
                {
                    o.SearchType = searchSettings.SearchType;
                }
            })
                           .ConfigureQuery(async q =>
            {
                // Hide hidden?
                if (!await _authorizationService.AuthorizeAsync(HttpContext.User,
                                                                Permissions.SearchHidden))
                {
                    q.HidePrivate.True();
                }

                // Hide spam?
                if (!await _authorizationService.AuthorizeAsync(HttpContext.User,
                                                                Permissions.SearchSpam))
                {
                    q.HideSpam.True();
                }

                // Hide deleted?
                if (!await _authorizationService.AuthorizeAsync(HttpContext.User,
                                                                Permissions.SearchDeleted))
                {
                    q.HideDeleted.True();
                }

                // Hide private?
                if (!await _authorizationService.AuthorizeAsync(HttpContext.User,
                                                                Permissions.SearchPrivate))
                {
                    q.HidePrivate.True();
                }
            })
                           .GetResultsAsync(new EntityIndexOptions()
            {
                Search = keywords,
                Sort   = sort,
                Order  = order
            }, new PagerOptions()
            {
                Page = page,
                Size = size
            });

            IPagedResults <SearchApiResult> results = null;

            if (entities != null)
            {
                results = new PagedResults <SearchApiResult>
                {
                    Total = entities.Total
                };

                var baseUrl = await _contextFacade.GetBaseUrlAsync();

                foreach (var entity in entities.Data)
                {
                    var url = baseUrl + _contextFacade.GetRouteUrl(new RouteValueDictionary()
                    {
                        ["area"]       = entity.ModuleId,
                        ["controller"] = "Home",
                        ["action"]     = "Display",
                        ["opts.id"]    = entity.Id,
                        ["opts.alias"] = entity.Alias
                    });

                    results.Data.Add(new SearchApiResult()
                    {
                        Id        = entity.Id,
                        CreatedBy = new UserApiResult()
                        {
                            Id          = entity.CreatedBy.Id,
                            DisplayName = entity.CreatedBy.DisplayName,
                            UserName    = entity.CreatedBy.UserName,
                            Avatar      = entity.CreatedBy.Avatar,
                            Url         = baseUrl + _contextFacade.GetRouteUrl(new RouteValueDictionary()
                            {
                                ["area"]       = "Plato.Users",
                                ["controller"] = "Home",
                                ["action"]     = "Display",
                                ["opts.id"]    = entity.CreatedBy.Id,
                                ["opts.alias"] = entity.CreatedBy.Alias
                            })
                        },
                        ModifiedBy = new UserApiResult()
                        {
                            Id          = entity.ModifiedBy.Id,
                            DisplayName = entity.ModifiedBy.DisplayName,
                            UserName    = entity.ModifiedBy.UserName,
                            Avatar      = entity.ModifiedBy.Avatar,
                            Url         = baseUrl + _contextFacade.GetRouteUrl(new RouteValueDictionary()
                            {
                                ["area"]       = "Plato.Users",
                                ["controller"] = "Home",
                                ["action"]     = "Display",
                                ["opts.id"]    = entity.ModifiedBy.Id,
                                ["opts.alias"] = entity.ModifiedBy.Alias
                            })
                        },
                        LastReplyBy = new UserApiResult()
                        {
                            Id          = entity.LastReplyBy.Id,
                            DisplayName = entity.LastReplyBy.DisplayName,
                            UserName    = entity.LastReplyBy.UserName,
                            Avatar      = entity.LastReplyBy.Avatar,
                            Url         = baseUrl + _contextFacade.GetRouteUrl(new RouteValueDictionary()
                            {
                                ["area"]       = "Plato.Users",
                                ["controller"] = "Home",
                                ["action"]     = "Display",
                                ["opts.id"]    = entity.LastReplyBy.Id,
                                ["opts.alias"] = entity.LastReplyBy.Alias
                            })
                        },
                        Title       = entity.Title,
                        Excerpt     = entity.Abstract,
                        Url         = url,
                        CreatedDate = new FriendlyDate()
                        {
                            Text  = entity.CreatedDate.ToPrettyDate(),
                            Value = entity.CreatedDate
                        },
                        ModifiedDate = new FriendlyDate()
                        {
                            Text  = entity.ModifiedDate.ToPrettyDate(),
                            Value = entity.ModifiedDate
                        },
                        LastReplyDate = new FriendlyDate()
                        {
                            Text  = entity.LastReplyDate.ToPrettyDate(),
                            Value = entity.LastReplyDate
                        },
                        Relevance = entity.Relevance
                    });
                }
            }

            IPagedApiResults <SearchApiResult> output = null;

            if (results != null)
            {
                output = new PagedApiResults <SearchApiResult>()
                {
                    Page       = page,
                    Size       = size,
                    Total      = results.Total,
                    TotalPages = results.Total.ToSafeCeilingDivision(size),
                    Data       = results.Data
                };
            }

            return(output != null
                ? base.Result(output)
                : base.NoResults());
        }
Exemple #2
0
        public async Task <IActionResult> Get(
            int page      = 1,
            int size      = 10,
            string sort   = "CreatedDate",
            OrderBy order = OrderBy.Desc)
        {
            // Ensure we are authenticated
            var user = await base.GetAuthenticatedUserAsync();

            if (user == null)
            {
                return(base.UnauthorizedException());
            }

            // Get notifications
            var userNotifications = await GetUserNotifications(
                page,
                size,
                user.Id,
                sort,
                order);

            IPagedResults <UserNotificationApiResult> results = null;

            if (userNotifications != null)
            {
                results = new PagedResults <UserNotificationApiResult>
                {
                    Total = userNotifications.Total
                };

                var baseUrl = await _contextFacade.GetBaseUrlAsync();

                foreach (var userNotification in userNotifications.Data)
                {
                    var toUrl = baseUrl + _contextFacade.GetRouteUrl(new RouteValueDictionary()
                    {
                        ["area"]       = "Plato.Users",
                        ["controller"] = "Home",
                        ["action"]     = "Display",
                        ["opts.id"]    = userNotification.To.Id,
                        ["opts.alias"] = userNotification.To.Alias
                    });

                    var fromUrl = baseUrl + _contextFacade.GetRouteUrl(new RouteValueDictionary()
                    {
                        ["area"]       = "Plato.Users",
                        ["controller"] = "Home",
                        ["action"]     = "Display",
                        ["opts.id"]    = userNotification.From.Id,
                        ["opts.alias"] = userNotification.From.Alias
                    });

                    var url = userNotification.Url;
                    if (url != null)
                    {
                        var noHttp      = url.IndexOf("http://", StringComparison.OrdinalIgnoreCase) == -1;
                        var noHttps     = url.IndexOf("https://", StringComparison.OrdinalIgnoreCase) == -1;
                        var relativeUrl = (noHttp && noHttps);
                        if (relativeUrl)
                        {
                            url = baseUrl + url;
                        }
                    }

                    results.Data.Add(new UserNotificationApiResult()
                    {
                        Id = userNotification.Id,
                        To = new UserApiResult()
                        {
                            Id          = userNotification.To.Id,
                            DisplayName = userNotification.To.DisplayName,
                            UserName    = userNotification.To.UserName,
                            Avatar      = userNotification.To.Avatar,
                            Url         = toUrl
                        },
                        From = new UserApiResult()
                        {
                            Id          = userNotification.From.Id,
                            DisplayName = userNotification.From.DisplayName,
                            UserName    = userNotification.From.UserName,
                            Avatar      = userNotification.From.Avatar,
                            Url         = fromUrl
                        },
                        Title   = userNotification.Title,
                        Message = userNotification.Message,
                        Url     = url,
                        Date    = new FriendlyDate()
                        {
                            Text  = userNotification.CreatedDate.ToPrettyDate(),
                            Value = userNotification.CreatedDate
                        }
                    });
                }
            }

            IPagedApiResults <UserNotificationApiResult> output = null;

            if (results != null)
            {
                output = new PagedApiResults <UserNotificationApiResult>()
                {
                    Page       = page,
                    Size       = size,
                    Total      = results.Total,
                    TotalPages = results.Total.ToSafeCeilingDivision(size),
                    Data       = results.Data
                };
            }

            return(output != null
                ? base.Result(output)
                : base.NoResults());
        }
Exemple #3
0
        public async Task <IActionResult> Get(
            int page        = 1,
            int size        = 10,
            string keywords = "",
            string sort     = "LastReplyDate",
            OrderBy order   = OrderBy.Desc)
        {
            var entities = await GetEntities(
                page,
                size,
                keywords,
                sort,
                order);

            IPagedResults <EntityApiResult> results = null;

            if (entities != null)
            {
                results = new PagedResults <EntityApiResult>
                {
                    Total = entities.Total
                };

                var baseUrl = await _contextFacade.GetBaseUrlAsync();

                foreach (var entity in entities.Data)
                {
                    var createdByUrl = baseUrl + _contextFacade.GetRouteUrl(new RouteValueDictionary()
                    {
                        ["area"]       = "Plato.Users",
                        ["controller"] = "Home",
                        ["action"]     = "Display",
                        ["opts.id"]    = entity.CreatedBy.Id,
                        ["opts.alias"] = entity.CreatedBy.Alias
                    });

                    var modifiedByUrl = baseUrl + _contextFacade.GetRouteUrl(new RouteValueDictionary()
                    {
                        ["area"]       = "Plato.Users",
                        ["controller"] = "Home",
                        ["action"]     = "Display",
                        ["opts.id"]    = entity.ModifiedBy.Id,
                        ["opts.alias"] = entity.ModifiedBy.Alias
                    });

                    var url = _contextFacade.GetRouteUrl(new RouteValueDictionary()
                    {
                        ["area"]       = entity.ModuleId,
                        ["controller"] = "Home",
                        ["action"]     = "Display",
                        ["opts.id"]    = entity.Id,
                        ["opts.alias"] = entity.Alias
                    });

                    results.Data.Add(new EntityApiResult()
                    {
                        Id        = entity.Id,
                        CreatedBy = new UserApiResult()
                        {
                            Id          = entity.CreatedBy.Id,
                            DisplayName = entity.CreatedBy.DisplayName,
                            UserName    = entity.CreatedBy.UserName,
                            Url         = createdByUrl
                        },
                        ModifiedBy = new UserApiResult()
                        {
                            Id          = entity.ModifiedBy.Id,
                            DisplayName = entity.ModifiedBy.DisplayName,
                            UserName    = entity.ModifiedBy.UserName,
                            Url         = modifiedByUrl
                        },
                        LastReplyBy = new UserApiResult()
                        {
                            Id          = entity.ModifiedBy.Id,
                            DisplayName = entity.ModifiedBy.DisplayName,
                            UserName    = entity.ModifiedBy.UserName,
                            Url         = modifiedByUrl
                        },
                        Title       = entity.Title,
                        Message     = entity.Message,
                        Url         = url,
                        CreatedDate = new FriendlyDate()
                        {
                            Text  = entity.CreatedDate.ToPrettyDate(),
                            Value = entity.CreatedDate
                        }
                    });
                }
            }

            IPagedApiResults <EntityApiResult> output = null;

            if (results != null)
            {
                output = new PagedApiResults <EntityApiResult>()
                {
                    Page       = page,
                    Size       = size,
                    Total      = results.Total,
                    TotalPages = results.Total.ToSafeCeilingDivision(size),
                    Data       = results.Data
                };
            }

            return(output != null
                ? base.Result(output)
                : base.NoResults());
        }
Exemple #4
0
        public async Task <IActionResult> Get(
            int page        = 1,
            int size        = 10,
            string keywords = "",
            string sort     = "LastLoginDate",
            OrderBy order   = OrderBy.Desc)
        {
            var users = await GetUsers(
                page,
                size,
                keywords,
                sort,
                order);

            IPagedResults <UserApiResult> results = null;

            if (users != null)
            {
                results = new PagedResults <UserApiResult>
                {
                    Total = users.Total
                };

                var baseUrl = await _contextFacade.GetBaseUrlAsync();

                foreach (var user in users.Data)
                {
                    var profileUrl = baseUrl + _contextFacade.GetRouteUrl(new RouteValueDictionary()
                    {
                        ["area"]       = "Plato.Users",
                        ["controller"] = "Home",
                        ["action"]     = "Display",
                        ["opts.id"]    = user.Id,
                        ["opts.alias"] = user.Alias
                    });

                    results.Data.Add(new UserApiResult()
                    {
                        Id          = user.Id,
                        DisplayName = user.DisplayName,
                        UserName    = user.UserName,
                        Url         = profileUrl,
                        Avatar      = user.Avatar
                    });
                }
            }

            IPagedApiResults <UserApiResult> output = null;

            if (results != null)
            {
                output = new PagedApiResults <UserApiResult>()
                {
                    Page       = page,
                    Size       = size,
                    Total      = results.Total,
                    TotalPages = results.Total.ToSafeCeilingDivision(size),
                    Data       = results.Data
                };
            }

            return(output != null
                ? base.Result(output)
                : base.NoResults());
        }
Exemple #5
0
        public async Task <IActionResult> Get(
            int page          = 1,
            int size          = 10,
            int entityId      = 0,
            int entityReplyId = 0,
            string sort       = "CreatedDate",
            OrderBy order     = OrderBy.Desc)
        {
            // Get histories
            var histories = await GetEntityHistory(
                page,
                size,
                entityId,
                entityReplyId,
                sort,
                order);

            IPagedResults <EntityHistoryApiResult> results = null;

            if (histories != null)
            {
                results = new PagedResults <EntityHistoryApiResult>
                {
                    Total = histories.Total
                };

                var baseUrl = await _contextFacade.GetBaseUrlAsync();

                foreach (var history in histories.Data)
                {
                    var createdByUrl = baseUrl + _contextFacade.GetRouteUrl(new RouteValueDictionary()
                    {
                        ["area"]       = "Plato.Users",
                        ["controller"] = "Home",
                        ["action"]     = "Display",
                        ["opts.id"]    = history.CreatedBy.Id,
                        ["opts.alias"] = history.CreatedBy.Alias
                    });

                    var sb = new StringBuilder();
                    sb.Append(history.CreatedBy.DisplayName)
                    .Append(" ")
                    .Append(history.MajorVersion == 1 && history.MinorVersion == 0
                            ? T["created"].Value
                            : T["edited"].Value)
                    .Append(" ")
                    .Append(history.CreatedDate.ToPrettyDate());

                    results.Data.Add(new EntityHistoryApiResult()
                    {
                        Id        = history.Id,
                        Text      = sb.ToString(),
                        Version   = history.Version,
                        CreatedBy = new UserApiResult()
                        {
                            Id          = history.CreatedBy.Id,
                            DisplayName = history.CreatedBy.DisplayName,
                            UserName    = history.CreatedBy.UserName,
                            Avatar      = history.CreatedBy.Avatar,
                            Url         = createdByUrl
                        },
                        Date = new FriendlyDate()
                        {
                            Text  = history.CreatedDate.ToPrettyDate(),
                            Value = history.CreatedDate
                        }
                    });
                }
            }

            IPagedApiResults <EntityHistoryApiResult> output = null;

            if (results != null)
            {
                output = new PagedApiResults <EntityHistoryApiResult>()
                {
                    Page       = page,
                    Size       = size,
                    Total      = results.Total,
                    TotalPages = results.Total.ToSafeCeilingDivision(size),
                    Data       = results.Data
                };
            }

            return(output != null
                ? base.Result(output)
                : base.NoResults());
        }
Exemple #6
0
        public async Task <IActionResult> Index([FromBody] TagApiParams parameters)
        {
            // Get tags
            var tags = await GetTags(parameters);

            // Build results
            IPagedResults <TagApiResult> results = null;

            if (tags != null)
            {
                // Get feature for tags
                IShellFeature feature = null;
                if (parameters.FeatureId > 0)
                {
                    feature = await _shellFeatureStore.GetByIdAsync(parameters.FeatureId);
                }

                results = new PagedResults <TagApiResult>
                {
                    Total = tags.Total
                };

                var baseUrl = await _contextFacade.GetBaseUrlAsync();

                foreach (var tag in tags.Data)
                {
                    var url = _contextFacade.GetRouteUrl(new RouteValueDictionary()
                    {
                        ["area"]       = feature?.ModuleId ?? "Plato.Tags",
                        ["controller"] = "Home",
                        ["action"]     = "Tag",
                        ["opts.id"]    = tag.Id,
                        ["opts.alias"] = tag.Alias
                    });

                    results.Data.Add(new TagApiResult()
                    {
                        Id       = tag.Id,
                        Name     = tag.Name,
                        Entities = tag.TotalEntities.ToPrettyInt(),
                        Follows  = tag.TotalFollows.ToPrettyInt(),
                        Url      = url
                    });
                }
            }

            IPagedApiResults <TagApiResult> output = null;

            if (results != null)
            {
                output = new PagedApiResults <TagApiResult>()
                {
                    Page       = parameters.Page,
                    Size       = parameters.Size,
                    Total      = results.Total,
                    TotalPages = results.Total.ToSafeCeilingDivision(parameters.Size),
                    Data       = results.Data
                };
            }

            return(output != null
                ? base.Result(output)
                : base.NoResults());
        }
Exemple #7
0
        public async Task <IActionResult> Get(
            int page        = 1,
            int size        = 10,
            string keywords = "",
            string sort     = "TotalEntities",
            OrderBy order   = OrderBy.Desc)
        {
            var tags = await GetTags(
                page,
                size,
                keywords,
                sort,
                order);

            IPagedResults <TagApiResult> results = null;

            if (tags != null)
            {
                results = new PagedResults <TagApiResult>
                {
                    Total = tags.Total
                };

                var baseUrl = await _contextFacade.GetBaseUrlAsync();

                foreach (var tag in tags.Data)
                {
                    var url = _contextFacade.GetRouteUrl(new RouteValueDictionary()
                    {
                        ["area"]       = "Plato.Tags",
                        ["controller"] = "Home",
                        ["action"]     = "Tag",
                        ["opts.id"]    = tag.Id,
                        ["opts.alias"] = tag.Alias
                    });

                    results.Data.Add(new TagApiResult()
                    {
                        Id   = tag.Id,
                        Name = tag.Name,
                        Url  = url
                    });
                }
            }

            IPagedApiResults <TagApiResult> output = null;

            if (results != null)
            {
                output = new PagedApiResults <TagApiResult>()
                {
                    Page       = page,
                    Size       = size,
                    Total      = results.Total,
                    TotalPages = results.Total.ToSafeCeilingDivision(size),
                    Data       = results.Data
                };
            }

            return(output != null
                ? base.Result(output)
                : base.NoResults());
        }