public async Task <IActionResult> Preview() { var title = Request.Cookies["preview_blog_title"]; var body = Request.Cookies["preview_blog_body"]; var obj = new EF.Blog { Title = title, Body = body }; if (Request.Cookies["preview_blog_photoId"] != "") { var id = Convert.ToInt32(Request.Cookies["preview_blog_photoId"]); var photo = await new BLL.Photo(unitOfWork).Get(new EF.Photo { PhotoId = id }); obj.BlogPhoto.Add(new EF.BlogPhoto { Photo = photo }); } return(View(obj)); }
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!")); }
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")); }
public async Task <IActionResult> Index([Bind(Prefix = "Item1")] EF.Blog args) { ViewData["Title"] = "Blogs"; ViewBag.Data = await new BLL.Blog(unitOfWork).Find(args); return(View()); }
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")); }