コード例 #1
0
 public ActionResult AddNotes(int?noteId)
 {
     if (Session["EmailID"] != null && Session["ID"] != null)
     {
         var categories = db.NoteCategories.Where(x => x.IsActive == true).ToList();
         var countries  = db.Countries.Where(x => x.IsActive == true).ToList();
         var noteTypes  = db.NoteTypes.Where(x => x.IsActive == true).ToList();
         var viewModel  = new SellNotesAllDropdownList
         {
             NoteCategories = categories,
             Countries      = countries,
             NoteTypes      = noteTypes,
         };
         if (noteId != null)
         {
             SellNote note = db.SellNotes.Where(x => x.ID == noteId).FirstOrDefault();
             viewModel.SellNote    = note;
             viewModel.IsPaidOrNot = note.IsPaid.ToString();
             var attachment = db.SellNoteAttachments.Where(x => x.NoteID == noteId).ToList();
             ViewBag.UploadedFileData = attachment.Count();
         }
         return(View(viewModel));
     }
     else
     {
         return(RedirectToAction("Login", "Account"));
     }
 }
コード例 #2
0
        public ActionResult AddNotes(SellNotesAllDropdownList user,
                                     HttpPostedFileBase DisplayPictureFile,
                                     HttpPostedFileBase[] UploadedPdfFile,
                                     HttpPostedFileBase PreviewFile)
        {
            SellNote note = db.SellNotes.Where(x => x.ID == user.SellNote.ID).FirstOrDefault();

            if (note != null)
            {
                var SubmitValue = Request.Form["SavePublish"];
                if (SubmitValue == "save")
                {
                    string fileExtension;
                    string DisplayPictureFileName;
                    string PreviewFileName;

                    // check paid status
                    SellNotesEntity PaidOrNot  = new SellNotesEntity();
                    bool            PaidStatus = PaidOrNot.CheckNoteStatusPaidOrNot(user.IsPaidOrNot);
                    if (user.SellNote.SellingPrice != null)
                    {
                        note.SellingPrice = user.SellNote.SellingPrice;
                    }
                    else
                    {
                        note.SellingPrice = 0;
                    }

                    note.Course        = user.SellNote.Course;
                    note.CourseCode    = user.SellNote.CourseCode;
                    note.Discription   = user.SellNote.Discription;
                    note.NumberOfPages = user.SellNote.NumberOfPages;
                    note.Professor     = user.SellNote.Professor;

                    note.Title          = user.SellNote.Title;
                    note.UniversityName = user.SellNote.UniversityName;
                    note.IsPaid         = PaidStatus;
                    note.Category       = user.SellNote.Category;
                    note.Country        = user.SellNote.Country;
                    note.NoteType       = user.SellNote.NoteType;
                    note.Status         = Convert.ToInt32(Enums.ReferenceNoteStatus.Draft);
                    note.ActionedBy     = 3;
                    note.SellerID       = Convert.ToInt32(Session["ID"]);
                    note.IsActive       = true;
                    note.CreatedDate    = DateTime.Now;

                    db.SaveChanges();

                    if (DisplayPictureFile != null /*&& note.DisplayPicture != null*/)
                    {
                        fileExtension = Path.GetExtension(DisplayPictureFile.FileName);
                        fileExtension = fileExtension.ToLower();
                        if (fileExtension == ".jpg" || fileExtension == ".jpeg" || fileExtension == ".png" ||
                            fileExtension == ".JPG" || fileExtension == ".JPEG" || fileExtension == ".PNG")
                        {
                            DisplayPictureFileName = "DP_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension;
                            var checkFile = CheckifPathExistForCurrentUser(note.ID) + note.DisplayPicture;
                            if (System.IO.File.Exists(checkFile))
                            {
                                System.IO.File.Delete(checkFile);
                            }
                            note.DisplayPicture = DisplayPictureFileName;

                            DisplayPictureFileName = CheckifPathExistForCurrentUser(note.ID) + DisplayPictureFileName;

                            DisplayPictureFile.SaveAs(DisplayPictureFileName);
                            db.SaveChanges();
                        }
                        else
                        {
                            ModelState.AddModelError("DisplayPictureFile", "Please upload image file of jpg, jpeg, png only");
                            user.NoteCategories = db.NoteCategories.Where(x => x.IsActive == true).ToList();
                            user.Countries      = db.Countries.Where(x => x.IsActive == true).ToList();
                            user.NoteTypes      = db.NoteTypes.Where(x => x.IsActive == true).ToList();
                            return(View(user));
                        }
                    }



                    if (PreviewFile != null /*&& note.NotesPreview != null*/)
                    {
                        fileExtension = Path.GetExtension(PreviewFile.FileName);
                        fileExtension = fileExtension.ToLower();
                        if (fileExtension == ".pdf")
                        {
                            PreviewFileName = "PreviewFile_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension;
                            var checkFile1 = CheckifPathExistForCurrentUser(note.ID) + note.NotesPreview;
                            if (System.IO.File.Exists(checkFile1))
                            {
                                System.IO.File.Delete(checkFile1);
                            }
                            note.NotesPreview = PreviewFileName;
                            PreviewFileName   = CheckifPathExistForCurrentUser(note.ID) + PreviewFileName;

                            PreviewFile.SaveAs(PreviewFileName);
                            db.SaveChanges();
                        }
                        else
                        {
                            ModelState.AddModelError("inputAddNotePreview", "Please upload file of pdf only");
                            user.NoteCategories = db.NoteCategories.Where(x => x.IsActive == true).ToList();
                            user.Countries      = db.Countries.Where(x => x.IsActive == true).ToList();
                            user.NoteTypes      = db.NoteTypes.Where(x => x.IsActive == true).ToList();
                            return(View(user));
                        }
                    }



                    // logic is remain
                    if (UploadedPdfFile != null)
                    {
                        var FilePathForUploadedPdf = checkifPathExistForNoteAttachment(note.ID);
                        var attchedNote            = db.SellNoteAttachments.Where(x => x.NoteID == note.ID).ToList();

                        DirectoryInfo di = new DirectoryInfo(FilePathForUploadedPdf);
                        foreach (FileInfo files in di.GetFiles())
                        {
                            files.Delete();
                        }
                        foreach (var file in attchedNote)
                        {
                            db.SellNoteAttachments.Remove(file);
                            db.SaveChanges();
                        }

                        long Length = 0;
                        foreach (HttpPostedFileBase file in UploadedPdfFile)
                        {
                            fileExtension = Path.GetExtension(file.FileName);
                            fileExtension = fileExtension.ToLower();
                            //Checking file is available to save.
                            if (file != null && fileExtension == ".pdf")
                            {
                                var InputFileName = Path.GetFileNameWithoutExtension(file.FileName);
                                var attachment    = new SellNoteAttachment()
                                {
                                    NoteID   = note.ID,
                                    FileName = InputFileName,
                                    FilePath = FilePathForUploadedPdf,
                                    IsActive = true
                                };
                                db.SellNoteAttachments.Add(attachment);
                                db.SaveChanges();
                                int AttachmentID = attachment.ID;

                                var UploadedPdfFileName = AttachmentID.ToString() + "_" + InputFileName + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension;
                                attachment.FileName = UploadedPdfFileName;
                                var destinationFileLocation = Path.Combine(FilePathForUploadedPdf, UploadedPdfFileName);
                                file.SaveAs(destinationFileLocation);
                                Length = Length + file.ContentLength;
                                db.SaveChanges();
                            }
                            else
                            {
                                ModelState.AddModelError("inputAdNoteUploadNotes", "Please upload file of pdf only");
                                user.NoteCategories = db.NoteCategories.Where(x => x.IsActive == true).ToList();
                                user.Countries      = db.Countries.Where(x => x.IsActive == true).ToList();
                                user.NoteTypes      = db.NoteTypes.Where(x => x.IsActive == true).ToList();
                                return(View(user));
                            }
                        }

                        note.AttachmentSize = FileSizeFormatter.FormatSize(Length);
                        db.SaveChanges();
                    }

                    return(RedirectToAction("AddNotes", "Dashboard"));
                }

                else if (SubmitValue == "publish")
                {
                    note.Status        = Convert.ToInt32(Enums.ReferenceNoteStatus.SubmittedForReview);
                    note.PublishedDate = DateTime.Now;
                    db.SaveChanges();

                    // Mail Sending Code
                    int  id       = Convert.ToInt32(Session["ID"]);
                    User SendMail = db.Users.Where(x => x.ID == id).FirstOrDefault();

                    var sender   = new MailAddress(ConstantStrings.supportEmail, ConstantStrings.supportName);
                    var receiver = new MailAddress("*****@*****.**", "Admin");
                    var password = ConstantStrings.supportPassword;
                    var body     = string.Empty;
                    var subject  = SendMail.FirstName + " sent his note for review";

                    using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplates/PublishNoteNotification.html")))
                    {
                        body = reader.ReadToEnd();
                    }
                    body = body.Replace("{SellerName}", SendMail.FirstName);
                    body = body.Replace("{NoteTitle}", note.Title);

                    var smtp = new SmtpClient
                    {
                        Host                  = ConfigurationManager.AppSettings["Host"],
                        Port                  = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]),
                        EnableSsl             = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]),
                        DeliveryMethod        = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = Convert.ToBoolean(ConfigurationManager.AppSettings["UseDefaultCredentials"]),
                        Credentials           = new NetworkCredential(sender.Address, password)
                    };

                    using (var messege = new MailMessage(sender, receiver)
                    {
                        Body = body,
                        Subject = subject,
                        IsBodyHtml = true
                    })
                    {
                        smtp.Send(messege);
                    }
                }
            }


            // Below Code is For New Note Added

            else
            {
                var    SubmitValue = Request.Form["SavePublish"];
                string fileExtension;
                string DisplayPictureFileName;
                string PreviewFileName;
                if (SubmitValue == "save")
                {
                    // check paid status
                    SellNotesEntity PaidOrNot  = new SellNotesEntity();
                    bool            PaidStatus = PaidOrNot.CheckNoteStatusPaidOrNot(user.IsPaidOrNot);



                    var addSellNoteDetail = new SellNote
                    {
                        Course        = user.SellNote.Course,
                        CourseCode    = user.SellNote.CourseCode,
                        Discription   = user.SellNote.Discription,
                        NumberOfPages = user.SellNote.NumberOfPages,
                        Professor     = user.SellNote.Professor,

                        Title          = user.SellNote.Title,
                        UniversityName = user.SellNote.UniversityName,
                        IsPaid         = PaidStatus,
                        Category       = user.SellNote.Category,
                        Country        = user.SellNote.Country,
                        NoteType       = user.SellNote.NoteType,
                        Status         = Convert.ToInt32(Enums.ReferenceNoteStatus.Draft),
                        ActionedBy     = 3,
                        SellerID       = Convert.ToInt32(Session["ID"]),
                        IsActive       = true,
                        CreatedDate    = DateTime.Now
                    };

                    try
                    {
                        if (user.SellNote.SellingPrice != null)
                        {
                            addSellNoteDetail.SellingPrice = user.SellNote.SellingPrice;
                        }
                        else
                        {
                            addSellNoteDetail.SellingPrice = 0;
                        }
                        db.SellNotes.Add(addSellNoteDetail);
                        db.SaveChanges();
                        int NotesID = addSellNoteDetail.ID;


                        if (DisplayPictureFile != null)
                        {
                            fileExtension = Path.GetExtension(DisplayPictureFile.FileName);
                            fileExtension = fileExtension.ToLower();
                            if (fileExtension == ".jpg" || fileExtension == ".jpeg" || fileExtension == ".png" ||
                                fileExtension == ".JPG" || fileExtension == ".JPEG" || fileExtension == ".PNG")
                            {
                                DisplayPictureFileName           = "DP_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension;
                                addSellNoteDetail.DisplayPicture = DisplayPictureFileName;
                                DisplayPictureFileName           = CheckifPathExistForCurrentUser(NotesID) + DisplayPictureFileName;

                                DisplayPictureFile.SaveAs(DisplayPictureFileName);
                                db.SaveChanges();
                            }
                            else
                            {
                                ModelState.AddModelError("DisplayPictureFile", "Please upload image file of jpg, jpeg, png only");
                                user.NoteCategories = db.NoteCategories.Where(x => x.IsActive == true).ToList();
                                user.Countries      = db.Countries.Where(x => x.IsActive == true).ToList();
                                user.NoteTypes      = db.NoteTypes.Where(x => x.IsActive == true).ToList();
                                return(View(user));
                            }
                        }

                        if (PreviewFile != null)
                        {
                            fileExtension = Path.GetExtension(PreviewFile.FileName);
                            fileExtension = fileExtension.ToLower();
                            if (fileExtension == ".pdf")
                            {
                                PreviewFileName = "PreviewFile_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension;
                                addSellNoteDetail.NotesPreview = PreviewFileName;
                                PreviewFileName = CheckifPathExistForCurrentUser(NotesID) + PreviewFileName;

                                PreviewFile.SaveAs(PreviewFileName);
                                db.SaveChanges();
                            }
                            else
                            {
                                ModelState.AddModelError("inputAddNotePreview", "Please upload file of pdf only");
                                user.NoteCategories = db.NoteCategories.Where(x => x.IsActive == true).ToList();
                                user.Countries      = db.Countries.Where(x => x.IsActive == true).ToList();
                                user.NoteTypes      = db.NoteTypes.Where(x => x.IsActive == true).ToList();
                                return(View(user));
                            }
                        }

                        var  FilePathForUploadedPdf = checkifPathExistForNoteAttachment(NotesID);
                        long Length = 0;
                        foreach (HttpPostedFileBase file in UploadedPdfFile)
                        {
                            //Checking file is available to save.
                            if (file != null)
                            {
                                fileExtension = Path.GetExtension(file.FileName);
                                fileExtension = fileExtension.ToLower();
                                if (fileExtension == ".pdf")
                                {
                                    var InputFileName = Path.GetFileNameWithoutExtension(file.FileName);
                                    var attachment    = new SellNoteAttachment()
                                    {
                                        NoteID   = NotesID,
                                        FileName = InputFileName,
                                        FilePath = FilePathForUploadedPdf,
                                        IsActive = true
                                    };
                                    db.SellNoteAttachments.Add(attachment);
                                    db.SaveChanges();
                                    int AttachmentID = attachment.ID;

                                    var UploadedPdfFileName = AttachmentID.ToString() + "_" + InputFileName + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension;
                                    attachment.FileName = UploadedPdfFileName;
                                    var destinationFileLocation = Path.Combine(FilePathForUploadedPdf, UploadedPdfFileName);
                                    file.SaveAs(destinationFileLocation);
                                    Length = Length + file.ContentLength;
                                    db.SaveChanges();
                                }
                                else
                                {
                                    ModelState.AddModelError("inputAdNoteUploadNotes", "Please upload file of pdf only");
                                    user.NoteCategories = db.NoteCategories.Where(x => x.IsActive == true).ToList();
                                    user.Countries      = db.Countries.Where(x => x.IsActive == true).ToList();
                                    user.NoteTypes      = db.NoteTypes.Where(x => x.IsActive == true).ToList();
                                    return(View(user));
                                }
                            }
                        }

                        addSellNoteDetail.AttachmentSize = FileSizeFormatter.FormatSize(Length);
                        db.SaveChanges();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }

                else if (SubmitValue == "publish")
                {
                    // check paid status
                    SellNotesEntity PaidOrNot  = new SellNotesEntity();
                    bool            PaidStatus = PaidOrNot.CheckNoteStatusPaidOrNot(user.IsPaidOrNot);

                    var addSellNoteDetail = new SellNote
                    {
                        Course        = user.SellNote.Course,
                        CourseCode    = user.SellNote.CourseCode,
                        Discription   = user.SellNote.Discription,
                        NumberOfPages = user.SellNote.NumberOfPages,
                        Professor     = user.SellNote.Professor,

                        Title          = user.SellNote.Title,
                        UniversityName = user.SellNote.UniversityName,
                        IsPaid         = PaidStatus,
                        Category       = user.SellNote.Category,
                        Country        = user.SellNote.Country,
                        NoteType       = user.SellNote.NoteType,
                        Status         = 4,
                        ActionedBy     = 3,
                        SellerID       = Convert.ToInt32(Session["ID"]),
                        IsActive       = true,
                        CreatedDate    = DateTime.Now,
                        PublishedDate  = DateTime.Now
                    };

                    try
                    {
                        if (user.SellNote.SellingPrice != null)
                        {
                            addSellNoteDetail.SellingPrice = user.SellNote.SellingPrice;
                        }
                        else
                        {
                            addSellNoteDetail.SellingPrice = 0;
                        }
                        db.SellNotes.Add(addSellNoteDetail);
                        db.SaveChanges();
                        int NotesID = addSellNoteDetail.ID;


                        if (DisplayPictureFile != null)
                        {
                            fileExtension = Path.GetExtension(DisplayPictureFile.FileName);
                            fileExtension = fileExtension.ToLower();
                            if (fileExtension == ".jpg" || fileExtension == ".jpeg" || fileExtension == ".png" ||
                                fileExtension == ".JPG" || fileExtension == ".JPEG" || fileExtension == ".PNG")
                            {
                                DisplayPictureFileName           = "DP_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension;
                                addSellNoteDetail.DisplayPicture = DisplayPictureFileName;
                                DisplayPictureFileName           = CheckifPathExistForCurrentUser(NotesID) + DisplayPictureFileName;

                                DisplayPictureFile.SaveAs(DisplayPictureFileName);
                                db.SaveChanges();
                            }
                            else
                            {
                                ModelState.AddModelError("DisplayPictureFile", "Please upload image file of jpg, jpeg, png only");
                                user.NoteCategories = db.NoteCategories.Where(x => x.IsActive == true).ToList();
                                user.Countries      = db.Countries.Where(x => x.IsActive == true).ToList();
                                user.NoteTypes      = db.NoteTypes.Where(x => x.IsActive == true).ToList();
                                return(View(user));
                            }
                        }

                        if (PreviewFile != null)
                        {
                            fileExtension = Path.GetExtension(PreviewFile.FileName);
                            fileExtension = fileExtension.ToLower();
                            if (fileExtension == ".pdf")
                            {
                                PreviewFileName = "PreviewFile_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension;
                                addSellNoteDetail.NotesPreview = PreviewFileName;
                                PreviewFileName = CheckifPathExistForCurrentUser(NotesID) + PreviewFileName;

                                PreviewFile.SaveAs(PreviewFileName);
                                db.SaveChanges();
                            }
                            else
                            {
                                ModelState.AddModelError("inputAddNotePreview", "Please upload file of pdf only");
                                user.NoteCategories = db.NoteCategories.Where(x => x.IsActive == true).ToList();
                                user.Countries      = db.Countries.Where(x => x.IsActive == true).ToList();
                                user.NoteTypes      = db.NoteTypes.Where(x => x.IsActive == true).ToList();
                                return(View(user));
                            }
                        }

                        var  FilePathForUploadedPdf = checkifPathExistForNoteAttachment(NotesID);
                        long Length = 0;
                        foreach (HttpPostedFileBase file in UploadedPdfFile)
                        {
                            //Checking file is available to save.
                            if (file != null)
                            {
                                fileExtension = Path.GetExtension(file.FileName);
                                fileExtension = fileExtension.ToLower();
                                if (fileExtension == ".pdf")
                                {
                                    var InputFileName = Path.GetFileNameWithoutExtension(file.FileName);
                                    var attachment    = new SellNoteAttachment()
                                    {
                                        NoteID   = NotesID,
                                        FileName = InputFileName,
                                        FilePath = FilePathForUploadedPdf,
                                        IsActive = true
                                    };
                                    db.SellNoteAttachments.Add(attachment);
                                    db.SaveChanges();
                                    int AttachmentID = attachment.ID;

                                    var UploadedPdfFileName = AttachmentID.ToString() + "_" + InputFileName + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension;
                                    attachment.FileName = UploadedPdfFileName;
                                    var destinationFileLocation = Path.Combine(FilePathForUploadedPdf, UploadedPdfFileName);
                                    file.SaveAs(destinationFileLocation);
                                    Length = Length + file.ContentLength;
                                    db.SaveChanges();
                                }
                                else
                                {
                                    ModelState.AddModelError("inputAdNoteUploadNotes", "Please upload file of pdf only");
                                    user.NoteCategories = db.NoteCategories.Where(x => x.IsActive == true).ToList();
                                    user.Countries      = db.Countries.Where(x => x.IsActive == true).ToList();
                                    user.NoteTypes      = db.NoteTypes.Where(x => x.IsActive == true).ToList();
                                    return(View(user));
                                }
                            }
                        }
                        addSellNoteDetail.AttachmentSize = FileSizeFormatter.FormatSize(Length);
                        db.SaveChanges();
                    }
                    catch (Exception)
                    {
                        throw;
                    }


                    db.SaveChanges();
                    int  id       = Convert.ToInt32(Session["ID"]);
                    User SendMail = db.Users.Where(x => x.ID == id).FirstOrDefault();

                    // Mail Sending Code
                    var sender   = new MailAddress(ConstantStrings.supportEmail, ConstantStrings.supportName);
                    var receiver = new MailAddress("*****@*****.**", "Admin");
                    var password = ConstantStrings.supportPassword;
                    var body     = string.Empty;
                    var subject  = SendMail.FirstName + " sent his note for review";

                    using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplates/PublishNoteNotification.html")))
                    {
                        body = reader.ReadToEnd();
                    }
                    body = body.Replace("{SellerName}", SendMail.FirstName);
                    body = body.Replace("{NoteTitle}", addSellNoteDetail.Title);

                    var smtp = new SmtpClient
                    {
                        Host                  = ConfigurationManager.AppSettings["Host"],
                        Port                  = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]),
                        EnableSsl             = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]),
                        DeliveryMethod        = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = Convert.ToBoolean(ConfigurationManager.AppSettings["UseDefaultCredentials"]),
                        Credentials           = new NetworkCredential(sender.Address, password)
                    };

                    using (var messege = new MailMessage(sender, receiver)
                    {
                        Body = body,
                        Subject = subject,
                        IsBodyHtml = true
                    })
                    {
                        smtp.Send(messege);
                    }
                }
            }
            return(RedirectToAction("Index", "Dashboard"));
        }