예제 #1
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!"));
        }
예제 #2
0
        public async Task <IActionResult> Edit([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/Edit";

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

                    // Update Category
                    // Delete all
                    var bblogcategory = new BLL.BlogCategory(unitOfWork);
                    var cats          = await bblogcategory.Find(new EF.BlogCategory {
                        BlogId = model.BlogId
                    });

                    await bblogcategory.Delete(cats.ToList());

                    // Add
                    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 = model.BlogId, CategoryId = c.CategoryId
                        });
                    }

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

                    await new BLL.Blog(unitOfWork).Edit(model);

                    foreach (var file in files.Files)
                    {
                        if (file.Name == "photo")
                        {
                            // Update Photo
                            IFormFile uploadedImage = file;
                            if (uploadedImage != null && uploadedImage.ContentType.ToLower().StartsWith("image/"))
                            {
                                var bblogphoto = new BLL.BlogPhoto(unitOfWork);
                                var blogphotos = await bblogphoto.Find(new EF.BlogPhoto {
                                    BlogId = model.BlogId
                                });

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

                                var pid = await new BLL.Photo(unitOfWork).Add(_environment, file);

                                var bp = new EF.BlogPhoto();
                                bp.BlogId  = model.BlogId;
                                bp.PhotoId = pid;
                                await bblogphoto.Edit(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       = model.BlogId;
                                ba.AttachmentId = aid;
                                await new BLL.BlogAttachment(unitOfWork).Add(ba);
                            }
                        }
                    }

                    // Update 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).Edit(new EF.BlogTag {
                                BlogId = model.BlogId, TagId = tid
                            });
                        }
                    }

                    // Remove unwanted Tag
                    string[] tagsArr;
                    if (tags == null)
                    {
                        tagsArr = new string[] { string.Empty }
                    }
                    ;
                    else
                    {
                        tagsArr = tags.Split(',');
                    }

                    await new BLL.BlogTag(unitOfWork).Clean(tagsArr);

                    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"));
        }