Example #1
0
        public async Task <ActionResult> UpdateNews(UpdateNews newForUpdate)
        {
            var dbHelpre = DBHelper.Instance;

            dbHelpre.BeginTransaction();

            return(await Task.Run(() =>
            {
                try
                {
                    bool IsNotValid = false;
                    ViewBag.Message = "Update new news.";
                    if (ModelState.IsValid)
                    {
                        var news = new InterNews
                        {
                            NewsID = newForUpdate.NewsID,
                            Heading = newForUpdate.Heading.Trim(),
                            ShortDescription = newForUpdate.ShortDesc.Trim(),
                            NewsDescription = string.IsNullOrEmpty(newForUpdate.Description) ? string.Empty : newForUpdate.Description.Trim(),
                            IsActive = newForUpdate.IsActiveNews ? 1 : 0,
                            IsApproved = newForUpdate.IsApprovedNews ? 1 : 0,
                            IsTopNews = newForUpdate.IsTopTenNews ? 1 : 0
                        };

                        var image = new ImageDetail
                        {
                            ImageType = 2,
                            ImageUrl = newForUpdate.ImageUrl,
                            Caption = newForUpdate.Caption,
                            IsActive = 1,
                            IsFirst = 1
                        };

                        var fileName = string.Empty;
                        if (newForUpdate.Image != null && newForUpdate.Image.ContentLength > 0)
                        {
                            fileName = new Random().Next(1000000000).ToString() + Path.GetFileName(newForUpdate.Image.FileName);
                            image.ImageUrl = ImagePath.InternationalNewsImage + "/" + fileName;
                            image.IsActive = 1;
                            image.IsFirst = 1;
                        }

                        var user = (IUsers)this.Session[Constants.UserSessionKey];
                        InterNewsBL.Instance.UpdateNews(ref dbHelpre, news, image, user);

                        if (!string.IsNullOrEmpty(fileName))
                        {
                            if (SaveImage(ImagePath.InternationalNewsImage, fileName, newForUpdate.Image))
                            {
                                if (DeleteImage(ImagePath.InternationalNewsImage, newForUpdate.ImageUrl))
                                {
                                    ViewBag.Message = "Updated successfully.";
                                    ModelState.Clear();
                                }
                                else
                                {
                                    IsNotValid = true;
                                }
                            }
                            else
                            {
                                IsNotValid = true;
                            }
                        }

                        newForUpdate.ImageUrl = image.ImageUrl;
                    }
                    else
                    {
                        IsNotValid = true;
                    }

                    if (IsNotValid)
                    {
                        dbHelpre.RollbackTransaction();
                        ModelState.AddModelError("INSERT", "Oops ! There is some error.");
                        ViewBag.Message = "Oops ! There is some error.";
                    }
                    else
                    {
                        ViewBag.Message = "Updated successfully.";
                        ModelState.Clear();
                    }

                    dbHelpre.CommitTransaction();
                }
                catch (Exception ex)
                {
                    dbHelpre.RollbackTransaction();
                    throw ex;
                }
                finally
                {
                    dbHelpre.ClearAllParameters();
                    dbHelpre.CloseConnection();
                    dbHelpre.Dispose();
                }

                return View(newForUpdate);
            }));
        }
Example #2
0
        public async Task <ActionResult> InsertNews(CreateNews newForInsert)
        {
            var dbHelpre   = DBHelper.Instance;
            var BLInstance = InterNewsBL.Instance;

            dbHelpre.BeginTransaction();
            return(await Task.Run(() =>
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        var news = new InterNews
                        {
                            Heading = newForInsert.Heading.Trim(),
                            ShortDescription = newForInsert.ShortDesc.Trim(),
                            NewsDescription = string.IsNullOrEmpty(newForInsert.Description) ? string.Empty : newForInsert.Description.Trim(),
                            IsTopNews = newForInsert.IsTopTenNews ? 1 : 0,
                            IsActive = 0,
                            IsApproved = 0
                        };

                        var image = new ImageDetail
                        {
                            ImageType = (int)ImageType.TopNewsImage,
                            ImageUrl = " ",
                            Caption = newForInsert.ImageCaption,
                            CaptionLink = newForInsert.CaptionLink,
                            IsActive = 0,
                            IsFirst = 0
                        };

                        var fileName = string.Empty;
                        if (newForInsert.Image != null && newForInsert.Image.ContentLength > 0)
                        {
                            fileName = new Random().Next(1000000000).ToString() + Path.GetFileName(newForInsert.Image.FileName);
                            image.ImageUrl = ImagePath.InternationalNewsImage + "/" + fileName;
                            image.IsActive = 1;
                            image.IsFirst = 1;
                        }

                        string dirPath = ImagePath.InternationalNewsImage;
                        var user = (IUsers)this.Session[Constants.UserSessionKey];
                        BLInstance.Insert(ref dbHelpre, news, image, user);

                        if (SaveImage(ImagePath.InternationalNewsImage, fileName, newForInsert.Image))
                        {
                            newForInsert = new CreateNews();
                            ViewBag.Message = "Successfully Inserter.";
                            ModelState.Clear();
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("INSERT", "Oops ! There is some error.");
                        ViewBag.Message = "Oops ! There is some error.";
                    }

                    dbHelpre.CommitTransaction();
                    return View(newForInsert);
                }
                catch (Exception ex)
                {
                    dbHelpre.RollbackTransaction();
                    throw ex;
                }
                finally
                {
                    dbHelpre.Dispose();
                    BLInstance.Dispose();
                }
            }));
        }