Ejemplo n.º 1
0
        public async Task <IActionResult> Deactivate([FromBody] int[] ids)
        {
            var bblog = new BLL.Blog(unitOfWork);

            await bblog.Deactivate(ids);

            return(Json("Success!"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Preview(EF.Blog blog, IFormCollection files)
        {
            var bllPhoto     = new BLL.Photo(unitOfWork);
            var bllBlogPhoto = new BLL.BlogPhoto(unitOfWork);

            if (!String.IsNullOrEmpty(Request.Cookies["preview_blog_photoId"]))
            {
                var id = Convert.ToInt32(Request.Cookies["preview_blog_photoId"]);

                // Delete photo if not in BlogPhotos
                if ((await bllBlogPhoto.Find(new EF.BlogPhoto {
                    PhotoId = id
                })).Count() <= 0)
                {
                    await bllPhoto.Delete(id, _environment);
                }
            }

            // Clear cookies
            Response.Cookies.Delete("preview_blog_title");
            Response.Cookies.Delete("preview_blog_body");
            Response.Cookies.Delete("preview_blog_photoId");

            // Add cookies
            Response.Cookies.Append("preview_blog_title", blog.Title);
            Response.Cookies.Append("preview_blog_body", blog.Body);

            foreach (var file in files.Files)
            {
                if (file.Name == "photo")
                {
                    int?photoId = null;
                    if (file != null && file.Length > 0)
                    {
                        photoId = await bllPhoto.Add(_environment, file);
                    }
                    else
                    {
                        if (blog.BlogId != 0)
                        {
                            var bllBlog = new BLL.Blog(unitOfWork);
                            var blo     = await bllBlog.Get(new EF.Blog {
                                BlogId = blog.BlogId
                            });

                            if (blo.BlogPhoto.Count() > 0)
                            {
                                photoId = blo.BlogPhoto.First().PhotoId;
                            }
                        }
                    }

                    Response.Cookies.Append("preview_blog_photoId", photoId.ToString());
                }
            }

            return(Json("Success!"));
        }
Ejemplo n.º 3
0
        private static BLL.Blog publish(string title, string body, BLL.User author)
        {
            BlogRepository repository = new BlogRepository(Helper.context);

            BLL.Blog blog = new BLL.Blog
            {
                Title  = title,
                Body   = body,
                Author = author
            };

            repository.Save(blog);
            //blog.Publish();
            return(blog);
        }
Ejemplo n.º 4
0
        private static BLL.Post comment(BLL.Blog onblog, string content, BLL.User author)
        {
            BLL.Post post = new BLL.Post
            {
                Blog    = onblog,
                BlogId  = onblog.Id,
                Content = content,
                Author  = author
            };
            //onblog.Publish();
            ////面向数据库:
            //new PostRepository(Helper.context).Save(post);

            //面向对象写法:
            onblog.Posts = onblog.Posts ?? new List <BLL.Post>();
            onblog.Posts.Add(post);
            new BlogRepository(Helper.context).Flush();

            return(post);
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Delete([FromBody] int[] ids)
        {
            var bblog = new BLL.Blog(unitOfWork);

            // Delete photos
            var blogphotos = await new BLL.BlogPhoto(unitOfWork).Get(ids);

            if (blogphotos.Count() > 0)
            {
                await new BLL.Photo(unitOfWork).Delete(blogphotos.Select(x => x.PhotoId).ToArray(), _environment);
            }

            // Delete attachments
            var blogattachments = await new BLL.BlogAttachment(unitOfWork).Get(ids);

            if (blogattachments.Count() > 0)
            {
                await new BLL.Attachment(unitOfWork).Delete(blogattachments.Select(x => x.AttachmentId).ToArray(), _environment);
            }

            await bblog.Delete(ids);

            return(Json("Success!"));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> New([Bind(Prefix = "Item1")] EF.Blog model, [Bind(Prefix = "Item2")] string tags, [Bind(Prefix = "Item3")] bool isactive, [Bind(Prefix = "Item4")] Dictionary <string, bool> categories, IFormCollection files)
        {
            ViewData["Title"] = "Blog/New";

            using (var txn = context.Database.BeginTransaction())
            {
                try
                {
                    model.VisibilityId = 1;
                    model.CreatedBy    = User.Identity.Name;

                    var bblog = new BLL.Blog(unitOfWork);

                    // Add Blog
                    if (!isactive)
                    {
                        model.DateInactive = DateTime.Now;
                    }
                    else
                    {
                        model.DateInactive = null;
                    }

                    var id = await bblog.Add(model);

                    // Add BlogCategory
                    foreach (var category in categories.Where(x => x.Value == true))
                    {
                        var c = await new BLL.Category(unitOfWork).Get(new EF.Category {
                            Name = category.Key
                        });
                        await new BLL.BlogCategory(unitOfWork).Add(new EF.BlogCategory {
                            BlogId = id, CategoryId = c.CategoryId
                        });
                    }

                    foreach (var file in files.Files)
                    {
                        if (file.Name == "photo")
                        {
                            // Add Photo
                            IFormFile uploadedImage = file;
                            if (uploadedImage != null && uploadedImage.ContentType.ToLower().StartsWith("image/"))
                            {
                                var pid = await new BLL.Photo(unitOfWork).Add(_environment, file);

                                var bp = new EF.BlogPhoto();
                                bp.BlogId  = id;
                                bp.PhotoId = pid;
                                await new BLL.BlogPhoto(unitOfWork).Add(bp);
                            }
                        }
                        else if (file.Name == "attachments")
                        {
                            // Add Attachment
                            if (file.Length > 0)
                            {
                                var aid = await new BLL.Attachment(unitOfWork).Add(_environment, file);

                                var ba = new EF.BlogAttachment();
                                ba.BlogId       = id;
                                ba.AttachmentId = aid;
                                await new BLL.BlogAttachment(unitOfWork).Add(ba);
                            }
                        }
                    }

                    // Add Tag
                    if (tags != null)
                    {
                        foreach (var tag in tags.Split(','))
                        {
                            var tid = await new BLL.Tag(unitOfWork).Add(new EF.Tag {
                                Name = tag
                            });
                            await new BLL.BlogTag(unitOfWork).Add(new EF.BlogTag {
                                BlogId = id, TagId = tid
                            });
                        }
                    }

                    txn.Commit();
                }
                catch (Exception ex)
                {
                    txn.Rollback();

                    logger.Error(ex);

                    TempData["notice"] = "Oops! Something went wrong.";

                    return(View(new Tuple <EF.Blog, string, bool, Dictionary <string, bool> >(model, tags, isactive, categories)));
                }
            }

            return(Redirect("~/Main/Blog"));
        }