Exemple #1
0
 public HttpResponseMessage Restore(TrashItem item)
 {
     try
     {
         repository.Restore(item.ObjectType, item.Id);
         return Request.CreateResponse(HttpStatusCode.OK);
     }
     catch (UnauthorizedAccessException)
     {
         return Request.CreateResponse(HttpStatusCode.Unauthorized);
     }
     catch (Exception)
     {
         return Request.CreateResponse(HttpStatusCode.InternalServerError);
     }
 }
 public HttpResponseMessage Purge(TrashItem item)
 {
     try
     {
         repository.Purge(item.ObjectType, item.Id);
         return Request.CreateResponse(HttpStatusCode.OK);
     }
     catch (UnauthorizedAccessException)
     {
         return Request.CreateResponse(HttpStatusCode.Unauthorized);
     }
     catch (Exception exception)
     {
         ErrorDatabaseManager.AddException(exception, GetType());
         return Request.CreateResponse(HttpStatusCode.InternalServerError);
     }
 }
        /// <summary>
        /// Get trash list
        /// </summary>
        /// <param name="trashType">Type (post, page, comment)</param>
        /// <param name="take">Take for a page</param>
        /// <param name="skip">Items to sckip</param>
        /// <param name="filter">Filter expression</param>
        /// <param name="order">Sort order</param>
        /// <returns></returns>
        public TrashVM GetTrash(TrashType trashType, int take = 10, int skip = 0, string filter = "1 == 1", string order = "DateCreated descending")
        {
            if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.AccessAdminPages))
                return new TrashVM();

            var trash = new TrashVM();
            var comments = new List<Comment>();
            var posts = new List<Post>();
            var pages = new List<Page>();
            var trashList = new List<TrashItem>();
            var trashPage = new List<TrashItem>();
            var query = trashList.AsQueryable().Where(filter);

            // comments
            if (trashType == TrashType.All || trashType == TrashType.Comment)
            {

                foreach (var p in Post.Posts)
                {
                    if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.EditOtherUsersPosts))
                        if (p.Author.ToLower() != Security.CurrentUser.Identity.Name.ToLower())
                            continue;

                    comments.AddRange(p.DeletedComments);
                }
            }

            if (comments.Count > 0)
            {
                foreach (var c in comments)
                {
                    TrashItem t1 = new TrashItem
                    {
                        Id = c.Id,
                        Title = c.Author + ": " + c.Teaser,
                        RelativeUrl = c.RelativeLink,
                        ObjectType = "Comment",
                        DateCreated = c.DateCreated.ToString("MM/dd/yyyy HH:mm")
                    };

                    trashList.Add(t1);
                }
            }

            // posts
            if (trashType == TrashType.All || trashType == TrashType.Post)
            {
                posts = (from x in Post.DeletedPosts orderby x.DateCreated descending select x).ToList();
            }

            if (posts.Count > 0)
            {
                foreach (var p in posts)
                {
                    if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.EditOtherUsersPosts))
                        if (p.Author.ToLower() != Security.CurrentUser.Identity.Name.ToLower())
                            continue;

                    TrashItem t2 = new TrashItem
                    {
                        Id = p.Id,
                        Title = System.Web.HttpContext.Current.Server.HtmlEncode(p.Title),
                        RelativeUrl = p.RelativeLink,
                        ObjectType = "Post",
                        DateCreated = p.DateCreated.ToString("MM/dd/yyyy HH:mm")
                    };

                    trashList.Add(t2);
                }
            }

            // pages
            if (trashType == TrashType.All || trashType == TrashType.Page)
            {
                pages = (from x in Page.DeletedPages orderby x.DateCreated descending select x).ToList();
            }

            if (pages.Count > 0)
            {
                foreach (var p in pages)
                {
                    TrashItem t3 = new TrashItem
                    {
                        Id = p.Id,
                        Title = System.Web.HttpContext.Current.Server.HtmlEncode(p.Title),
                        RelativeUrl = p.RelativeLink,
                        ObjectType = "Page",
                        DateCreated = p.DateCreated.ToString("MM/dd/yyyy HH:mm")
                    };

                    trashList.Add(t3);
                }
            }

            trash.TotalCount = trashList.Count;

            // if take passed in as 0, return all
            if (take == 0) take = trashList.Count;

            foreach (var item in query.OrderBy(order).Skip(skip).Take(take))
                trashPage.Add(item);
            
            trash.Items = trashPage;

            return trash;
        }
 public HttpResponseMessage Restore(TrashItem item)
 {
     repository.Restore(item.ObjectType, item.Id);
     return Request.CreateResponse(HttpStatusCode.OK);
 }