Exemplo n.º 1
0
        public ActionResult AllowDownload(int id)
        {
            NotesMarketPlace.Context.Users user = dobj.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault();

            Downloads download = dobj.Downloads.Find(id);

            if (user.ID == download.Seller)
            {
                NotesAttachments attachement = dobj.NotesAttachments.Where(x => x.NoteID == download.NoteID && x.IsActive == true).FirstOrDefault();


                dobj.Downloads.Attach(download);
                download.IsSellerHasAllowedDownload = true;
                download.AttachmentPath             = attachement.FilePath;
                download.ModifiedBy   = user.ID;
                download.ModifiedDate = DateTime.Now;
                dobj.SaveChanges();

                AllowDownloadTemplate(download, user);

                return(RedirectToAction("BuyerRequest"));
            }
            else
            {
                return(RedirectToAction("BuyerRequest"));
            }
        }
        public ActionResult EditNotes(int id)
        {
            Context.Users    user        = dobj.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault();
            NoteDetail       note        = dobj.NoteDetail.Where(x => x.ID == id && x.IsActive == true && x.SellerID == user.ID).FirstOrDefault();
            NotesAttachments attachement = dobj.NotesAttachments.Where(x => x.NoteID == id).FirstOrDefault();

            if (note != null)
            {
                EditNotes viewModel = new EditNotes
                {
                    ID             = note.ID,
                    NoteID         = note.ID,
                    Title          = note.Title,
                    Category       = note.Category,
                    Picture        = note.DisplayPicture,
                    Note           = attachement.FileName,
                    NumberofPages  = note.NumberofPages,
                    Description    = note.Description,
                    NoteType       = note.NoteType,
                    UniversityName = note.UniversityName,
                    Course         = note.Course,
                    CourseCode     = note.CourseCode,
                    Country        = note.Country,
                    Professor      = note.Professor,
                    IsPaid         = note.IsPaid,
                    SellingPrice   = note.SellingPrice,
                    Preview        = note.NotesPreview,

                    NoteCategoryList = dobj.NoteCategories.Where(x => x.IsActive == true).ToList(),
                    NoteTypeList     = dobj.NoteTypes.Where(x => x.IsActive == true).ToList(),
                    CountryList      = dobj.Countries.Where(x => x.IsActive == true).ToList(),
                };

                return(View(viewModel));
            }
            else
            {
                return(HttpNotFound());
            }
        }
        public ActionResult DeleteDraft(int id)
        {
            NoteDetail note = dobj.NoteDetail.Where(x => x.ID == id && x.IsActive == true).FirstOrDefault();

            if (note == null)
            {
                return(HttpNotFound());
            }

            IEnumerable <NotesAttachments> noteattachement = dobj.NotesAttachments.Where(x => x.NoteID == id && x.IsActive == true).ToList();

            if (noteattachement.Count() == 0)
            {
                return(HttpNotFound());
            }

            string notefolderpath           = Server.MapPath("~/Members/" + note.SellerID + "/" + note.ID);
            string noteattachmentfolderpath = Server.MapPath("~/Members/" + note.SellerID + "/" + note.ID + "/Attachements");

            DirectoryInfo notefolder            = new DirectoryInfo(notefolderpath);
            DirectoryInfo attachementnotefolder = new DirectoryInfo(noteattachmentfolderpath);

            EmptyFolder(attachementnotefolder);
            EmptyFolder(notefolder);

            Directory.Delete(notefolderpath);

            dobj.NoteDetail.Remove(note);

            foreach (var item in noteattachement)
            {
                NotesAttachments attachement = dobj.NotesAttachments.Where(x => x.ID == item.ID).FirstOrDefault();
                dobj.NotesAttachments.Remove(attachement);
            }
            dobj.SaveChanges();

            return(RedirectToAction("Dashboard", "SellNotes"));
        }
        public ActionResult CloneNote(int noteid)
        {
            // logged in user
            var user = dobj.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault();

            //get id of rejected notes
            var rejectednote = dobj.NoteDetail.Find(noteid);

            // creare clone of note
            NoteDetail clonenote = new NoteDetail();

            clonenote.SellerID       = rejectednote.SellerID;
            clonenote.Status         = 6;
            clonenote.Title          = rejectednote.Title;
            clonenote.Category       = rejectednote.Category;
            clonenote.NoteType       = rejectednote.NoteType;
            clonenote.NumberofPages  = rejectednote.NumberofPages;
            clonenote.Description    = rejectednote.Description;
            clonenote.UniversityName = rejectednote.UniversityName;
            clonenote.Country        = rejectednote.Country;
            clonenote.Course         = rejectednote.Course;
            clonenote.CourseCode     = rejectednote.CourseCode;
            clonenote.Professor      = rejectednote.Professor;
            clonenote.IsPaid         = rejectednote.IsPaid;
            clonenote.SellingPrice   = rejectednote.SellingPrice;
            clonenote.CreatedBy      = user.ID;
            clonenote.CreatedDate    = DateTime.Now;
            clonenote.IsActive       = true;

            // save note in database
            dobj.NoteDetail.Add(clonenote);
            dobj.SaveChanges();

            // get clonenote
            clonenote = dobj.NoteDetail.Find(clonenote.ID);

            // if display picture is not null then copy file from rejected note's folder to clone note's new folder
            if (rejectednote.DisplayPicture != null)
            {
                var rejectednotefilepath = Server.MapPath(rejectednote.DisplayPicture);
                var clonenotefilepath    = "~/Members/" + user.ID + "/" + clonenote.ID + "/";

                var filepath = Path.Combine(Server.MapPath(clonenotefilepath));

                FileInfo file = new FileInfo(rejectednotefilepath);

                Directory.CreateDirectory(filepath);
                if (file.Exists)
                {
                    System.IO.File.Copy(rejectednotefilepath, Path.Combine(filepath, Path.GetFileName(rejectednotefilepath)));
                }

                clonenote.DisplayPicture = Path.Combine(clonenotefilepath, Path.GetFileName(rejectednotefilepath));
                dobj.SaveChanges();
            }

            // if note preview is not null then copy file from rejected note's folder to clone note's new folder
            if (rejectednote.NotesPreview != null)
            {
                var rejectednotefilepath = Server.MapPath(rejectednote.NotesPreview);
                var clonenotefilepath    = "~/Members/" + user.ID + "/" + clonenote.ID + "/";

                var filepath = Path.Combine(Server.MapPath(clonenotefilepath));

                FileInfo file = new FileInfo(rejectednotefilepath);

                Directory.CreateDirectory(filepath);

                if (file.Exists)
                {
                    System.IO.File.Copy(rejectednotefilepath, Path.Combine(filepath, Path.GetFileName(rejectednotefilepath)));
                }

                clonenote.NotesPreview = Path.Combine(clonenotefilepath, Path.GetFileName(rejectednotefilepath));
                dobj.SaveChanges();
            }

            // attachment path of rejected note and clone note
            var rejectednoteattachement = Server.MapPath("~/Members/" + user.ID + "/" + rejectednote.ID + "/Attachements/");
            var clonenoteattachement    = "~/Members/" + user.ID + "/" + clonenote.ID + "/Attachements/";

            var attachementfilepath = Path.Combine(Server.MapPath(clonenoteattachement));

            // create directory for attachement folder
            Directory.CreateDirectory(attachementfilepath);

            // get attachements files from rejected note and copy to clone note
            foreach (var files in Directory.GetFiles(rejectednoteattachement))
            {
                FileInfo file = new FileInfo(files);

                if (file.Exists)
                {
                    System.IO.File.Copy(file.ToString(), Path.Combine(attachementfilepath, Path.GetFileName(file.ToString())));
                }

                // save attachment in database
                NotesAttachments attachement = new NotesAttachments();
                attachement.NoteID      = clonenote.ID;
                attachement.FileName    = Path.GetFileName(file.ToString());
                attachement.FilePath    = clonenoteattachement;
                attachement.CreatedDate = DateTime.Now;
                attachement.CreatedBy   = user.ID;
                attachement.IsActive    = true;

                dobj.NotesAttachments.Add(attachement);
                dobj.SaveChanges();
            }
            return(RedirectToAction("Dashboard", "SellNotes"));
        }
        public ActionResult EditNotes(int id, EditNotes notes)
        {
            var user = dobj.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault();

            var sellnote = dobj.NoteDetail.Where(x => x.ID == id && x.IsActive == true && x.SellerID == user.ID).FirstOrDefault();

            if (sellnote == null)
            {
                return(HttpNotFound());
            }

            //if nnote is paid note
            if (notes.IsPaid == true && notes.NotesPreview == null && sellnote.NotesPreview == null)
            {
                ModelState.AddModelError("NotesPreview", "This field is required if selling type is paid");
                notes.NoteCategoryList = dobj.NoteCategories.Where(x => x.IsActive == true).ToList();
                notes.NoteTypeList     = dobj.NoteTypes.Where(x => x.IsActive == true).ToList();
                notes.CountryList      = dobj.Countries.Where(x => x.IsActive == true).ToList();
                return(View(notes));
            }

            if (ModelState.IsValid)
            {
                var notesattachement = dobj.NotesAttachments.Where(x => x.NoteID == notes.NoteID && x.IsActive == true).ToList();


                dobj.NoteDetail.Attach(sellnote);
                sellnote.Title          = notes.Title;
                sellnote.Category       = notes.Category;
                sellnote.NoteType       = notes.NoteType;
                sellnote.NumberofPages  = notes.NumberofPages;
                sellnote.Description    = notes.Description;
                sellnote.Country        = notes.Country;
                sellnote.UniversityName = notes.UniversityName;
                sellnote.Course         = notes.Course;
                sellnote.CourseCode     = notes.CourseCode;
                sellnote.Professor      = notes.Professor;
                if (notes.IsPaid == true)
                {
                    sellnote.IsPaid       = true;
                    sellnote.SellingPrice = notes.SellingPrice;
                }
                else
                {
                    sellnote.IsPaid       = false;
                    sellnote.SellingPrice = 0;
                }
                sellnote.ModifiedDate = DateTime.Now;
                sellnote.ModifiedBy   = user.ID;
                dobj.SaveChanges();

                if (notes.DisplayPicture != null)
                {
                    if (sellnote.DisplayPicture != null)
                    {
                        string   path = Server.MapPath(sellnote.DisplayPicture);
                        FileInfo file = new FileInfo(path);
                        if (file.Exists)
                        {
                            file.Delete();
                        }
                    }

                    string displaypicturefilename = System.IO.Path.GetFileName(notes.DisplayPicture.FileName);
                    string displaypicturepath     = "~/Members/" + user.ID + "/" + sellnote.ID + "/";
                    NewDirectory(displaypicturepath);
                    string displaypicturefilepath = Path.Combine(Server.MapPath(displaypicturepath), displaypicturefilename);
                    sellnote.DisplayPicture = displaypicturepath + displaypicturefilename;
                    notes.DisplayPicture.SaveAs(displaypicturefilepath);
                    dobj.SaveChanges();
                }

                if (notes.NotesPreview != null)
                {
                    if (sellnote.NotesPreview != null)
                    {
                        string   path = Server.MapPath(sellnote.NotesPreview);
                        FileInfo file = new FileInfo(path);
                        if (file.Exists)
                        {
                            file.Delete();
                        }
                    }

                    string notespreviewfilename = System.IO.Path.GetFileName(notes.NotesPreview.FileName);
                    string notespreviewpath     = "~/Members/" + user.ID + "/" + sellnote.ID + "/";
                    NewDirectory(notespreviewpath);
                    string notespreviewfilepath = Path.Combine(Server.MapPath(notespreviewpath), notespreviewfilename);
                    sellnote.NotesPreview = notespreviewpath + notespreviewfilename;
                    notes.NotesPreview.SaveAs(notespreviewfilepath);
                    dobj.SaveChanges();
                }

                dobj.NoteDetail.Attach(sellnote);
                dobj.Entry(sellnote).Property(x => x.DisplayPicture).IsModified = true;
                dobj.Entry(sellnote).Property(x => x.NotesPreview).IsModified   = true;
                dobj.SaveChanges();

                if (notes.UploadNotes[0] != null)
                {
                    string        path = Server.MapPath(notesattachement[0].FilePath);
                    DirectoryInfo dir  = new DirectoryInfo(path);
                    EmptyFolder(dir);


                    foreach (var item in notesattachement)
                    {
                        NotesAttachments attachement = dobj.NotesAttachments.Where(x => x.ID == item.ID).FirstOrDefault();
                        dobj.NotesAttachments.Remove(attachement);
                    }


                    foreach (HttpPostedFileBase file in notes.UploadNotes)
                    {
                        if (file != null)
                        {
                            string notesattachementfilename = System.IO.Path.GetFileName(file.FileName);
                            string notesattachementpath     = "~/Members/" + user.ID + "/" + sellnote.ID + "/Attachements/";
                            NewDirectory(notesattachementpath);
                            string notesattachementfilepath = Path.Combine(Server.MapPath(notesattachementpath), notesattachementfilename);
                            file.SaveAs(notesattachementfilepath);


                            NotesAttachments notesattachements = new NotesAttachments
                            {
                                NoteID      = sellnote.ID,
                                FileName    = notesattachementfilename,
                                FilePath    = notesattachementpath,
                                CreatedDate = DateTime.Now,
                                CreatedBy   = user.ID,
                                IsActive    = true
                            };


                            dobj.NotesAttachments.Add(notesattachements);
                            dobj.SaveChanges();
                        }
                    }
                }

                return(RedirectToAction("Dashboard", "SellNotes"));
            }
            else
            {
                return(RedirectToAction("EditNotes", new { id = notes.ID }));
            }
        }
        public ActionResult AddNotes(AddNotes add, string command)
        {
            if (add.UploadNotes[0] == null)
            {
                ModelState.AddModelError("UploadNotes", "This field is required");
                add.NoteCategoryList = dobj.NoteCategories.Where(x => x.IsActive == true).ToList();
                add.NoteTypeList     = dobj.NoteTypes.Where(x => x.IsActive == true).ToList();
                add.CountryList      = dobj.Countries.Where(x => x.IsActive == true).ToList();
                return(View(add));
            }

            if (add.IsPaid == true && add.NotesPreview == null)
            {
                ModelState.AddModelError("NotesPreview", "This field is required if selling type is paid");
                add.NoteCategoryList = dobj.NoteCategories.Where(x => x.IsActive == true).ToList();
                add.NoteTypeList     = dobj.NoteTypes.Where(x => x.IsActive == true).ToList();
                add.CountryList      = dobj.Countries.Where(x => x.IsActive == true).ToList();
                return(View(add));
            }
            foreach (HttpPostedFileBase file in add.UploadNotes)
            {
                if (!System.IO.Path.GetExtension(file.FileName).Equals(".pdf"))
                {
                    ModelState.AddModelError("UploadNotes", "Only PDF Format is allowed");
                    add.NoteCategoryList = dobj.NoteCategories.Where(x => x.IsActive == true).ToList();
                    add.NoteTypeList     = dobj.NoteTypes.Where(x => x.IsActive == true).ToList();
                    add.CountryList      = dobj.Countries.Where(x => x.IsActive == true).ToList();
                    return(View(add));
                }
            }
            if (ModelState.IsValid)
            {
                NoteDetail sellnote = new NoteDetail();

                Context.Users user = dobj.Users.FirstOrDefault(x => x.EmailID == User.Identity.Name);

                sellnote.SellerID       = user.ID;
                sellnote.Title          = add.Title;
                sellnote.Status         = command == "Save" ? 6 : 7;
                sellnote.Category       = add.Category;
                sellnote.NoteType       = add.NoteType;
                sellnote.NumberofPages  = add.NumberofPages;
                sellnote.Description    = add.Description;
                sellnote.UniversityName = add.UniversityName;
                sellnote.Country        = add.Country;
                sellnote.Course         = add.Course;
                sellnote.CourseCode     = add.CourseCode;
                sellnote.Professor      = add.Professor;
                sellnote.IsPaid         = add.IsPaid;
                if (sellnote.IsPaid)
                {
                    sellnote.SellingPrice = add.SellingPrice;
                }
                else
                {
                    sellnote.SellingPrice = 0;
                }

                if (sellnote.Status == 7)
                {
                    string sellername = user.FirstName + " " + user.LastName;
                    PublishNoteRequestmail(sellnote.Title, sellername);
                }


                sellnote.CreatedDate = DateTime.Now;
                sellnote.CreatedBy   = user.ID;
                sellnote.IsActive    = true;

                dobj.NoteDetail.Add(sellnote);
                dobj.SaveChanges();

                sellnote = dobj.NoteDetail.Find(sellnote.ID);
                if (add.DisplayPicture != null)
                {
                    string displaypicturefilename = System.IO.Path.GetFileName(add.DisplayPicture.FileName);
                    string displaypicturepath     = "~/Members/" + user.ID + "/" + sellnote.ID + "/";
                    NewDirectory(displaypicturepath);
                    string displaypicturefilepath = Path.Combine(Server.MapPath(displaypicturepath), displaypicturefilename);
                    sellnote.DisplayPicture = displaypicturepath + displaypicturefilename;
                    add.DisplayPicture.SaveAs(displaypicturefilepath);
                }

                if (add.NotesPreview != null)
                {
                    string notespreviewfilename = System.IO.Path.GetFileName(add.NotesPreview.FileName);
                    string notespreviewpath     = "~/Members/" + user.ID + "/" + sellnote.ID + "/";
                    NewDirectory(notespreviewpath);
                    string notespreviewfilepath = Path.Combine(Server.MapPath(notespreviewpath), notespreviewfilename);
                    sellnote.NotesPreview = notespreviewpath + notespreviewfilename;
                    add.NotesPreview.SaveAs(notespreviewfilepath);
                }

                dobj.NoteDetail.Attach(sellnote);
                dobj.Entry(sellnote).Property(x => x.DisplayPicture).IsModified = true;
                dobj.Entry(sellnote).Property(x => x.NotesPreview).IsModified   = true;
                dobj.SaveChanges();

                foreach (HttpPostedFileBase file in add.UploadNotes)
                {
                    if (file != null)
                    {
                        string notesattachementfilename = System.IO.Path.GetFileName(file.FileName);
                        string notesattachementpath     = "~/Members/" + user.ID + "/" + sellnote.ID + "/Attachements/";
                        NewDirectory(notesattachementpath);
                        string notesattachementfilepath = Path.Combine(Server.MapPath(notesattachementpath), notesattachementfilename);
                        file.SaveAs(notesattachementfilepath);

                        NotesAttachments notesattachements = new NotesAttachments
                        {
                            NoteID      = sellnote.ID,
                            FileName    = notesattachementfilename,
                            FilePath    = notesattachementpath,
                            CreatedDate = DateTime.Now,
                            CreatedBy   = user.ID,
                            IsActive    = true
                        };

                        dobj.NotesAttachments.Add(notesattachements);
                        dobj.SaveChanges();
                    }
                }

                return(RedirectToAction("Dashboard", "SellNotes"));
            }
            else
            {
                AddNotes viewModel = new AddNotes
                {
                    NoteCategoryList = dobj.NoteCategories.Where(x => x.IsActive == true).ToList(),
                    NoteTypeList     = dobj.NoteTypes.Where(x => x.IsActive == true).ToList(),
                    CountryList      = dobj.Countries.Where(x => x.IsActive == true).ToList()
                };

                return(View(viewModel));
            }
        }