Ejemplo n.º 1
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"));
        }
Ejemplo n.º 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"));
        }