Exemplo n.º 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"));
     }
 }
        public ActionResult Index(SellNote model, SellerNotesAttachement mod, FormCollection fc, HttpPostedFileBase imgfile, HttpPostedFileBase pdffile)
        {
            var data = _context.NoteTypes.ToList();

            ViewBag.NoteType = new SelectList(data, "Name", "Name");
            var item = _context.Countries.ToList();

            ViewBag.Country = new SelectList(item, "Name", "Name");
            var item1 = _context.NoteCatgories.ToList();

            ViewBag.Category = new SelectList(item1, "Name", "Name");

            if (ModelState.IsValid)
            {
                var currenttime = DateTime.UtcNow;
                if (imgfile != null)
                {
                    model.Display_pic = new byte[imgfile.ContentLength];
                    imgfile.InputStream.Read(model.Display_pic, 0, imgfile.ContentLength);
                }
                if (pdffile != null)
                {
                    String FileExt = Path.GetExtension(pdffile.FileName).ToUpper();
                    if (FileExt == ".PDF")
                    {
                        Stream       str     = pdffile.InputStream;
                        BinaryReader Br      = new BinaryReader(str);
                        Byte[]       FileDet = Br.ReadBytes((Int32)str.Length);
                        mod.FileName       = model.FileName = pdffile.FileName;
                        mod.FileContent    = model.Upload_note = FileDet;
                        mod.AttachmentSize = pdffile.ContentLength / 1024;
                    }
                }
                if (fc["save"] == "SAVE")
                {
                    model.Status = "Draft";
                }
                if (fc["publish"] == "PUBLISH")
                {
                    model.Status = " Submitted for Review";
                }
                var res = _context.Registers.Where(x => x.EmailId == User.Identity.Name).Single().Id;
                model.Seller_Id  = res;
                mod.CreatedBy    = model.CreatedBy = User.Identity.Name;
                mod.CreatedDate  = model.CreatedDate = currenttime;
                mod.ModifiedBy   = model.ModifiedBy = User.Identity.Name;
                mod.ModifiedDate = model.ModifiedDate = currenttime;
                mod.NoteTitle    = model.title;
                _context.SellerNotesAttachements.Add(mod);
                _context.SellNotes.Add(model);
                _context.SaveChanges();
                return(RedirectToAction("ViewNotes"));
            }
            else
            {
                return(View());
            }
        }
        public ActionResult Approve(int?NoteID)
        {
            SellNote getSellnote = db.SellNotes.Where(x => x.ID == NoteID).FirstOrDefault();

            getSellnote.Status        = Convert.ToInt32(Enums.ReferenceNoteStatus.Approved);
            getSellnote.PublishedDate = DateTime.Now;
            getSellnote.ApprovedBy    = Convert.ToInt32(Session["ID"]);
            db.SaveChanges();
            return(RedirectToAction("NotesUnderReview", "NotesUnderReview"));
        }
        public ActionResult InReview(int?NoteID)
        {
            SellNote getSellnote = db.SellNotes.Where(x => x.ID == NoteID).FirstOrDefault();

            if (getSellnote.Status == Convert.ToInt32(Enums.ReferenceNoteStatus.SubmittedForReview))
            {
                getSellnote.Status = Convert.ToInt32(Enums.ReferenceNoteStatus.InReview);
            }
            db.SaveChanges();
            return(RedirectToAction("NotesUnderReview", "NotesUnderReview"));
        }
Exemplo n.º 5
0
        public ActionResult Unpublish()
        {
            int      id          = Convert.ToInt32(Request.Form["hiddenId"]);
            SellNote getSellnote = db.SellNotes.Where(x => x.ID == id).FirstOrDefault();

            getSellnote.Status       = Convert.ToInt32(Enums.ReferenceNoteStatus.Removed);
            getSellnote.AdminRemarks = Request.Form["remarks"];
            getSellnote.ModifiedDate = DateTime.Now;
            getSellnote.RejectedBy   = Convert.ToInt32(Session["ID"]);
            getSellnote.ModifiedBy   = Convert.ToInt32(Session["ID"]);
            db.SaveChanges();
            return(RedirectToAction("PublishedNotes", "Admin"));
        }
        public ActionResult updateNote(SellNote model, FormCollection fc, HttpPostedFileBase imgfile, HttpPostedFileBase pdffile)
        {
            if (ModelState.IsValid)
            {
                var currenttime = DateTime.UtcNow;
                if (imgfile != null)
                {
                    model.Display_pic = new byte[imgfile.ContentLength];
                    imgfile.InputStream.Read(model.Display_pic, 0, imgfile.ContentLength);
                }
                if (pdffile != null)
                {
                    String FileExt = Path.GetExtension(pdffile.FileName).ToUpper();
                    if (FileExt == ".PDF")
                    {
                        Stream       str     = pdffile.InputStream;
                        BinaryReader Br      = new BinaryReader(str);
                        Byte[]       FileDet = Br.ReadBytes((Int32)str.Length);
                        model.FileName    = pdffile.FileName;
                        model.Upload_note = FileDet;
                    }
                }
                if (fc["save"] == "SAVE")
                {
                    model.Status = "Draft";
                }
                if (fc["publish"] == "PUBLISH")
                {
                    model.Status = " Submitted for Review";
                }
                var res = _context.Registers.Where(x => x.EmailId == User.Identity.Name).Single().Id;
                model.Seller_Id    = res;
                model.ModifiedDate = currenttime;

                model.Seller_Id             = _context.Registers.Where(x => x.EmailId == User.Identity.Name).Single().Id;
                _context.Entry(model).State = System.Data.Entity.EntityState.Modified;
                _context.SaveChanges();
                return(Redirect("ViewNotes"));
            }
            else
            {
                return(View());
            }
        }
        public ActionResult Index()
        {
            SellNote sn   = new SellNote();
            var      data = _context.NoteTypes.ToList();

            ViewBag.NoteType = new SelectList(data, "Name", "Name");
            var item = _context.Countries.ToList();

            ViewBag.Country = new SelectList(item, "Name", "Name");
            var item1 = _context.NoteCatgories.ToList();

            ViewBag.Category = new SelectList(item1, "Name", "Name");
            var result = _context.SellNotes.ToList();

            ViewBag.University_Name = new SelectList(result, "University_Name", "University_Name");
            ViewBag.Course          = new SelectList(result, "Course", "Course");

            return(View(sn));
        }
        public ActionResult DownloadNote(int?noteId)
        {
            SellNote noteISPaidOrNot = db.SellNotes.Where(x => x.ID == noteId).FirstOrDefault();
            bool     paidOrNot       = noteISPaidOrNot.IsPaid;
            int      DownloaderID    = Convert.ToInt32(Session["ID"]);
            var      noteAttachment  = db.SellNoteAttachments.Where(x => x.NoteID == noteISPaidOrNot.ID).ToList();

            var filePath = ConstantStrings.Notes_Upload_Path + noteISPaidOrNot.SellerID.ToString() + "/" + noteISPaidOrNot.ID.ToString() + ConstantStrings.Note_Attachment_Path;


            var outputStream = new MemoryStream();

            using (var zip = new ZipFile())
            {
                zip.AddDirectory(Server.MapPath(filePath));
                zip.Save(outputStream);
            }

            outputStream.Position = 0;
            return(File(outputStream.ToArray(), "application/zip", noteISPaidOrNot.Title + ".zip"));
        }
        public ActionResult updateNote(int id)
        {
            SellNote sn   = new SellNote();
            var      data = _context.NoteTypes.ToList();

            ViewBag.NoteType = new SelectList(data, "Name", "Name");
            var item = _context.Countries.ToList();

            ViewBag.Country = new SelectList(item, "Name", "Name");
            var item1 = _context.NoteCatgories.ToList();

            ViewBag.Category = new SelectList(item1, "Name", "Name");
            var result = _context.SellNotes.ToList();

            ViewBag.University_Name = new SelectList(result, "University_Name", "University_Name");
            ViewBag.Course          = new SelectList(result, "Course", "Course");
            var rs = _context.SellNotes.Where(x => x.Id == id).ToList();

            ViewData["data"] = rs;
            return(View(sn));
        }
Exemplo n.º 10
0
        public ActionResult DeleteNote(int?noteId)
        {
            SellNote           DeleteNote       = db.SellNotes.Where(x => x.ID == noteId).FirstOrDefault();
            SellNoteAttachment deleteAttachment = db.SellNoteAttachments.Where(x => x.NoteID == noteId).FirstOrDefault();

            if (DeleteNote != null)
            {
                db.SellNotes.Remove(DeleteNote);
            }
            if (deleteAttachment != null)
            {
                db.SellNoteAttachments.Remove(deleteAttachment);
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }

            return(RedirectToAction("Index", "Dashboard"));
        }
Exemplo n.º 11
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"));
        }
Exemplo n.º 12
0
        public ActionResult DownloadNote(int?noteId)
        {
            int      DownloaderID    = Convert.ToInt32(Session["ID"]);
            SellNote noteISPaidOrNot = db.SellNotes.Where(x => x.ID == noteId).FirstOrDefault();
            bool     paidOrNot       = noteISPaidOrNot.IsPaid;
            var      noteAttachment  = db.SellNoteAttachments.Where(x => x.NoteID == noteISPaidOrNot.ID).ToList();
            var      filePath        = ConstantStrings.Notes_Upload_Path + noteISPaidOrNot.SellerID.ToString() + "/" + noteISPaidOrNot.ID.ToString() + ConstantStrings.Note_Attachment_Path;/*+ noteAttachment.FileName;*/

            if (noteISPaidOrNot.SellerID != DownloaderID)
            {
                var DownloadEntryExistForPaidAndFreeBoth = db.Downloads.Where(x => x.Downloader == DownloaderID && x.NoteID == noteId &&
                                                                              x.IsSellerHasAllowedDownloads == true).FirstOrDefault();
                if (DownloadEntryExistForPaidAndFreeBoth == null)
                {
                    if (paidOrNot == true)
                    {
                        // Buyer Request has one entry for this
                        Download data = new Download()
                        {
                            NoteID     = noteISPaidOrNot.ID,
                            Seller     = noteISPaidOrNot.SellerID,
                            Downloader = DownloaderID,
                            IsSellerHasAllowedDownloads = false,
                            AttachmentPath         = filePath,
                            IsAttachmentDownloaded = false,
                            IsPaid         = true,
                            PurchasedPrice = noteISPaidOrNot.SellingPrice,
                            NoteTitle      = noteISPaidOrNot.Title,
                            NoteCategory   = noteISPaidOrNot.NoteCategory.Name,
                            CreatedDate    = DateTime.Now,
                        };
                        db.Downloads.Add(data);
                        db.SaveChanges();
                        data.CreatedBy = DownloaderID;
                        db.SaveChanges();

                        User SendMail = db.Users.Where(x => x.ID == DownloaderID).FirstOrDefault();

                        // Email sending
                        var sender   = new MailAddress(ConstantStrings.supportEmail, ConstantStrings.supportName);
                        var receiver = new MailAddress(noteISPaidOrNot.User.EmailID, noteISPaidOrNot.User.FirstName);
                        var password = ConstantStrings.supportPassword;
                        var body     = string.Empty;
                        var subject  = SendMail.FirstName + " wants to purchase your notes";

                        using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplates/DownloadPaidNotesNotification.html")))
                        {
                            body = reader.ReadToEnd();
                        }
                        body = body.Replace("{SellerName}", noteISPaidOrNot.User.FirstName);
                        body = body.Replace("{BuyerName}", SendMail.FirstName);


                        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"));
                    }
                    else
                    {
                        var outputStream = new MemoryStream();

                        using (var zip = new ZipFile())
                        {
                            zip.AddDirectory(Server.MapPath(filePath));
                            zip.Save(outputStream);
                        }

                        outputStream.Position = 0;

                        //Entry in Downloads table for Downloader
                        Download data = new Download()
                        {
                            NoteID     = noteISPaidOrNot.ID,
                            Seller     = noteISPaidOrNot.SellerID,
                            Downloader = DownloaderID,
                            IsSellerHasAllowedDownloads = true,
                            AttachmentDownloadDate      = DateTime.Now,
                            AttachmentPath         = filePath,
                            IsAttachmentDownloaded = true,
                            IsPaid         = false,
                            PurchasedPrice = 0,
                            NoteTitle      = noteISPaidOrNot.Title,
                            NoteCategory   = noteISPaidOrNot.NoteCategory.Name,
                            CreatedDate    = DateTime.Now,
                        };
                        db.Downloads.Add(data);
                        db.SaveChanges();
                        data.CreatedBy = DownloaderID;
                        db.SaveChanges();

                        return(File(outputStream.ToArray(), "application/zip", noteISPaidOrNot.Title + ".zip"));
                    }
                }
                else
                {
                    var outputStream = new MemoryStream();

                    using (var zip = new ZipFile())
                    {
                        zip.AddDirectory(Server.MapPath(filePath));
                        zip.Save(outputStream);
                    }

                    outputStream.Position = 0;
                    DownloadEntryExistForPaidAndFreeBoth.IsAttachmentDownloaded = true;
                    DownloadEntryExistForPaidAndFreeBoth.AttachmentDownloadDate = DateTime.Now;
                    db.SaveChanges();
                    return(File(outputStream.ToArray(), "application/zip", noteISPaidOrNot.Title + ".zip"));
                }
            }
            else
            {
                var outputStream = new MemoryStream();

                using (var zip = new ZipFile())
                {
                    zip.AddDirectory(Server.MapPath(filePath));
                    zip.Save(outputStream);
                }

                outputStream.Position = 0;
                return(File(outputStream.ToArray(), "application/zip", noteISPaidOrNot.Title + ".zip"));
            }
        }