示例#1
0
        public ActionResult Delete(string[] ids)
        {
            try
            {
                if (ids.IsAny())
                {
                    var posts         = new List <Post>();
                    var galleryImages = new List <GalleryImage>();
                    var orderItems    = new List <OrderItem>();
                    IEnumerable <LocalizedProperty> localizedProperties = null;

                    for (var i = 0; i < ids.Length; i++)
                    {
                        var id   = int.Parse(ids[i]);
                        var post = _postService.Get(x => x.Id == id);

                        if (post.GalleryImages.IsAny())
                        {
                            galleryImages.AddRange(post.GalleryImages.ToList());
                        }

                        post.AttributeValues.ToList().ForEach(att => post.AttributeValues.Remove(att));
                        posts.Add(post);

                        var orderItem = _orderItemService.GetByPostId(id);
                        if (orderItem != null)
                        {
                            orderItems.Add(orderItem);
                        }

                        localizedProperties = _localizedPropertyService.GetByEntityId(id);
                    }

                    _galleryService.BatchDelete(galleryImages);
                    _orderItemService.BatchDelete(orderItems);
                    _localizedPropertyService.BatchDelete(localizedProperties);
                    _postService.BatchDelete(posts);
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                LogText.Log(string.Concat("Post.Delete: ", ex.Message));
            }

            return(RedirectToAction("Index"));
        }