public ActionResult AllowDownload(int id) { // get logged in user Users user = context.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault(); // get download object by id Downloads download = context.Downloads.Find(id); // check if logged in user and note seller is same or not if (user.ID == download.Seller) { // get sellernoteattachement object SellerNotesAttachments attachement = context.SellerNotesAttachments.Where(x => x.NoteID == download.NoteID && x.IsActive == true).FirstOrDefault(); // update data in download table context.Downloads.Attach(download); download.AttachmentDownloadedDate = DateTime.Now; download.IsSellerHasAllowedDownload = true; download.AttachmentPath = attachement.FilePath; download.NodifiedBy = user.ID; download.ModifiedDate = DateTime.Now; context.SaveChanges(); // send mail AllowDownloadTemplate(download, user); return(RedirectToAction("BuyerRequest")); } else { return(RedirectToAction("BuyerRequest")); } }
public ActionResult DeleteDraft(int id) { // get notes using id SellerNotes note = context.SellerNotes.Where(x => x.ID == id && x.IsActive == true).FirstOrDefault(); // if note is not found if (note == null) { return(HttpNotFound()); } // get attachement files using note id IEnumerable <SellerNotesAttachments> noteattachement = context.SellerNotesAttachments.Where(x => x.NoteID == id && x.IsActive == true).ToList(); // if noteattachement count is 0 if (noteattachement.Count() == 0) { return(HttpNotFound()); } // filepaths for note and note attachements string notefolderpath = Server.MapPath("~/Members/" + note.SellerID + "/" + note.ID); string noteattachmentfolderpath = Server.MapPath("~/Members/" + note.SellerID + "/" + note.ID + "/Attachements"); // get directory DirectoryInfo notefolder = new DirectoryInfo(notefolderpath); DirectoryInfo attachementnotefolder = new DirectoryInfo(noteattachmentfolderpath); // empty directory EmptyFolder(attachementnotefolder); EmptyFolder(notefolder); // delete directory Directory.Delete(notefolderpath); // remove note from database context.SellerNotes.Remove(note); // remove attachement from database foreach (var item in noteattachement) { SellerNotesAttachments attachement = context.SellerNotesAttachments.Where(x => x.ID == item.ID).FirstOrDefault(); context.SellerNotesAttachments.Remove(attachement); } // save changes context.SaveChanges(); return(RedirectToAction("Dashboard")); }
public ActionResult EditNotes(int id) { // get logged in user var user = Context.Registration.Where(x => x.Email == User.Identity.Name).FirstOrDefault(); // get note SellerNotes note = Context.SellerNotes.Where(x => x.ID == id && x.IsActive == true && x.SellerID == user.UserId).FirstOrDefault(); // get note attachement SellerNotesAttachments attachement = Context.SellerNotesAttachments.Where(x => x.NoteID == id).FirstOrDefault(); if (note != null) { // create object of edit note viewmodel EditNotesViewModel xyz = new EditNotesViewModel { ID = note.ID, NoteID = note.ID, Title = note.Title, Category = note.Category, Picture = note.DisplayPicture, Note = attachement.FilePath, 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 = Context.NoteCategories.ToList(), NoteTypeList = Context.NoteTypes.ToList(), CountryList = Context.Countrie.ToList() }; // return viewmodel to edit notes page return(View(xyz)); } else { // if note not found return(HttpNotFound()); } }
public void Maptomodel(SellerNotes Note, SellerNotesAttachments Attachement) { Note.Title = Title; Note.Category = Category; Note.DisplayPicture = DisplayPicture; Note.NoteType = NoteType; Note.NumberofPages = NumberofPages; Note.Description = Description; Note.UniversityName = UniversityName; Note.Country = Country; Note.Course = Course; Note.CourseCode = CourseCode; Note.Professor = Professor; Note.SellingPrice = SellingPrice; Note.NotesPreview = NotePreview; Attachement.FileName = UploadNotes; }
public ActionResult AddNotes(AddNotesViewModels xyz) { if (ModelState.IsValid) { // create seller note object SellerNotes abc = new SellerNotes(); var user = Context.Registration.FirstOrDefault(x => x.Email == User.Identity.Name); abc.Status = Context.ReferenceData.Where(x => x.Value.ToLower() == "draft").Select(x => x.ID).FirstOrDefault(); abc.SellerID = user.UserId; abc.Title = xyz.Title.Trim(); abc.Country = xyz.Country; abc.Category = xyz.Category; abc.NoteType = xyz.NoteType; abc.NumberofPages = xyz.NumberofPages; abc.Description = xyz.Description.Trim(); abc.UniversityName = xyz.UniversityName.Trim(); abc.Course = xyz.Course.Trim(); abc.CourseCode = xyz.CourseCode.Trim(); abc.Professor = xyz.Professor.Trim(); abc.IsPaid = xyz.IsPaid; if (abc.IsPaid) { abc.SellingPrice = xyz.SellingPrice; } else { abc.SellingPrice = 0; } abc.IsActive = true; Context.SellerNotes.Add(abc); try { Context.SaveChanges(); } catch (Exception ex) { } // // add note in database and save // get seller note abc = Context.SellerNotes.Find(abc.ID); // if display picture is not null then save picture into directory and directory path into database if (xyz.DisplayPicture != null) { string displaypicturefilename = System.IO.Path.GetFileName(xyz.DisplayPicture.FileName); string displaypicturepath = "~/Members/" + user.UserId + "/" + abc.ID + "/"; CreateDirectoryIfMissing(displaypicturepath); string displaypicturefilepath = Path.Combine(Server.MapPath(displaypicturepath), displaypicturefilename); abc.DisplayPicture = displaypicturepath + displaypicturefilename; xyz.DisplayPicture.SaveAs(displaypicturefilepath); } // if note preview is not null then save picture into directory and directory path into database if (xyz.NotesPreview != null) { string notespreviewfilename = System.IO.Path.GetFileName(xyz.NotesPreview.FileName); string notespreviewpath = "~/Members/" + user.UserId + "/" + abc.ID + "/"; CreateDirectoryIfMissing(notespreviewpath); string notespreviewfilepath = Path.Combine(Server.MapPath(notespreviewpath), notespreviewfilename); abc.NotesPreview = notespreviewpath + notespreviewfilename; xyz.NotesPreview.SaveAs(notespreviewfilepath); } // update note preview path and display picture path and save changes // Context.SellerNotes.Add(abc); //Context.Entry(abc).Property(x => x.DisplayPicture).IsModified = true; //Context.Entry(abc).Property(x => x.NotesPreview).IsModified = true; Context.SaveChanges(); // attachement files foreach (HttpPostedFileBase file in xyz.UploadNotes) { // check if file is null or not if (file != null) { // save file in directory string notesattachementfilename = System.IO.Path.GetFileName(file.FileName); string notesattachementpath = "~/Members/" + user.UserId + "/" + abc.ID + "/Attachements/"; CreateDirectoryIfMissing(notesattachementpath); string notesattachementfilepath = Path.Combine(Server.MapPath(notesattachementpath), notesattachementfilename); file.SaveAs(notesattachementfilepath); // create object of sellernotesattachement SellerNotesAttachments notesattachements = new SellerNotesAttachments { NoteID = abc.ID, FileName = notesattachementfilename, FilePath = notesattachementpath, IsActive = true }; // save seller notes attachement Context.SellerNotesAttachments.Add(notesattachements); Context.SaveChanges(); } } return(RedirectToAction("SignUp", "SignUpMethod")); } // if model state is not valid else { // create object of xyz AddNotesViewModels viewModel = new AddNotesViewModels { NoteCategoryList = Context.NoteCategories.ToList(), NoteTypeList = Context.NoteTypes.ToList(), CountryList = Context.Countrie.ToList() }; return(View(viewModel)); } }
public ActionResult EditNotes(int id, EditNotesViewModel notes) { // check if model state is valid or not if (ModelState.IsValid) { // get logged in user var user = Context.Registration.Where(x => x.Email == User.Identity.Name).FirstOrDefault(); // get note var sellernotes = Context.SellerNotes.Where(x => x.ID == id && x.IsActive == true && x.SellerID == user.UserId).FirstOrDefault(); // if sellernote null if (sellernotes == null) { return(HttpNotFound()); } // check if note is paid or preview is not null if (notes.IsPaid == true && notes.Preview == null && sellernotes.NotesPreview == null) { //ModelState.AddModelError("NotesPreview", "This field is required if selling type is paid"); @ViewBag.Notespreview = "This field is required if selling type is paid"; return(View(notes)); } // get note attachement var notesattachement = Context.SellerNotesAttachments.Where(x => x.NoteID == notes.NoteID && x.IsActive == true).ToList(); // attache note object and update Context.SellerNotes.Attach(sellernotes); sellernotes.Title = notes.Title.Trim(); sellernotes.Category = notes.Category; sellernotes.NoteType = notes.NoteType; sellernotes.NumberofPages = notes.NumberofPages; sellernotes.Description = notes.Description.Trim(); sellernotes.Country = notes.Country; sellernotes.UniversityName = notes.UniversityName.Trim(); sellernotes.Course = notes.Course.Trim(); sellernotes.CourseCode = notes.CourseCode.Trim(); sellernotes.Professor = notes.Professor.Trim(); if (notes.IsPaid == true) { sellernotes.IsPaid = true; sellernotes.SellingPrice = notes.SellingPrice; } else { sellernotes.IsPaid = false; sellernotes.SellingPrice = 0; } Context.SaveChanges(); // if display picture is not null if (notes.DisplayPicture != null) { // if note object has already previously uploaded picture then delete it if (sellernotes.DisplayPicture != null) { string path = Server.MapPath(sellernotes.DisplayPicture); FileInfo file = new FileInfo(path); if (file.Exists) { file.Delete(); } } // save updated profile picture in directory and save directory path in database string displaypicturefilename = System.IO.Path.GetFileName(notes.DisplayPicture.FileName); string displaypicturepath = "~/Members/" + user.UserId + "/" + sellernotes.ID + "/"; CreateDirectoryIfMissing(displaypicturepath); string displaypicturefilepath = Path.Combine(Server.MapPath(displaypicturepath), displaypicturefilename); sellernotes.DisplayPicture = displaypicturepath + displaypicturefilename; notes.DisplayPicture.SaveAs(displaypicturefilepath); } // if note preview is not null if (notes.NotesPreview != null) { // if note object has already previously uploaded note preview then delete it if (sellernotes.NotesPreview != null) { string path = Server.MapPath(sellernotes.NotesPreview); FileInfo file = new FileInfo(path); if (file.Exists) { file.Delete(); } } // save updated note preview in directory and save directory path in database string notespreviewfilename = System.IO.Path.GetFileName(notes.NotesPreview.FileName); string notespreviewpath = "~/Members/" + user.UserId + "/" + sellernotes.ID + "/"; CreateDirectoryIfMissing(notespreviewpath); string notespreviewfilepath = Path.Combine(Server.MapPath(notespreviewpath), notespreviewfilename); sellernotes.NotesPreview = notespreviewpath + notespreviewfilename; notes.NotesPreview.SaveAs(notespreviewfilepath); } // check if user upload notes or not if (notes.UploadNotes[0] != null) { // if user upload notes then delete directory that have previously uploaded notes string path = Server.MapPath(notesattachement[0].FilePath); DirectoryInfo dir = new DirectoryInfo(path); EmptyFolder(dir); // remove previously uploaded attachement from database foreach (var item in notesattachement) { SellerNotesAttachments attachement = Context.SellerNotesAttachments.Where(x => x.ID == item.ID).FirstOrDefault(); Context.SellerNotesAttachments.Remove(attachement); } // add newly uploaded attachement in database and save it in database foreach (HttpPostedFileBase file in notes.UploadNotes) { // check if file is null or not if (file != null) { // save file in directory string notesattachementfilename = System.IO.Path.GetFileName(file.FileName); string notesattachementpath = "~/Members/" + user.UserId + "/" + sellernotes.ID + "/Attachements/"; CreateDirectoryIfMissing(notesattachementpath); string notesattachementfilepath = Path.Combine(Server.MapPath(notesattachementpath), notesattachementfilename); file.SaveAs(notesattachementfilepath); // create object of sellernotesattachement SellerNotesAttachments notesattachements = new SellerNotesAttachments { NoteID = sellernotes.ID, FileName = notesattachementfilename, FilePath = notesattachementpath, IsActive = true }; // save seller notes attachement Context.SellerNotesAttachments.Add(notesattachements); Context.SaveChanges(); } } } return(RedirectToAction("Dashboard", "Dashboard")); } else { return(RedirectToAction("EditNotes", new { id = notes.ID })); } }
public ActionResult CloneNote(int noteid) { // get logged in user var user = context.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault(); // get rejected note by id var rejectednote = context.SellerNotes.Find(noteid); // create object of sellernote for create clone of note SellerNotes clonenote = new SellerNotes(); clonenote.SellerID = rejectednote.SellerID; clonenote.Status = context.ReferenceData.Where(x => x.Value.ToLower() == "draft").Select(x => x.ID).FirstOrDefault(); 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 context.SellerNotes.Add(clonenote); context.SaveChanges(); // get clonenote clonenote = context.SellerNotes.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)); context.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)); context.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 SellerNotesAttachments attachement = new SellerNotesAttachments(); attachement.NoteID = clonenote.ID; attachement.FileName = Path.GetFileName(file.ToString()); attachement.FilePath = clonenoteattachement; attachement.CreatedDate = DateTime.Now; attachement.CreatedBy = user.ID; attachement.IsActive = true; context.SellerNotesAttachments.Add(attachement); context.SaveChanges(); } return(RedirectToAction("Dashboard", "SellYourNotes")); }
public ActionResult AddNotes(AddNotesViewModel addnotesviewmodel) { // check if upload note is null or not if (addnotesviewmodel.UploadNotes[0] == null) { ModelState.AddModelError("UploadNotes", "This field is required"); addnotesviewmodel.NoteCategoryList = context.NoteCategories.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.NoteTypeList = context.NoteTypes.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.CountryList = context.Countries.Where(x => x.IsActive == true).ToList(); return(View(addnotesviewmodel)); } // check and raise error for note preview is null for paid notes if (addnotesviewmodel.IsPaid == true && addnotesviewmodel.NotesPreview == null) { ModelState.AddModelError("NotesPreview", "This field is required if selling type is paid"); addnotesviewmodel.NoteCategoryList = context.NoteCategories.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.NoteTypeList = context.NoteTypes.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.CountryList = context.Countries.Where(x => x.IsActive == true).ToList(); return(View(addnotesviewmodel)); } foreach (HttpPostedFileBase file in addnotesviewmodel.UploadNotes) { if (!System.IO.Path.GetExtension(file.FileName).Equals(".pdf")) { ModelState.AddModelError("UploadNotes", "Only PDF Format is allowed"); addnotesviewmodel.NoteCategoryList = context.NoteCategories.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.NoteTypeList = context.NoteTypes.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.CountryList = context.Countries.Where(x => x.IsActive == true).ToList(); return(View(addnotesviewmodel)); } } // check model state if (ModelState.IsValid) { // create seller note object SellerNotes sellernotes = new SellerNotes(); Users user = context.Users.FirstOrDefault(x => x.EmailID == User.Identity.Name); sellernotes.SellerID = user.ID; sellernotes.Title = addnotesviewmodel.Title.Trim(); sellernotes.Status = context.ReferenceData.Where(x => x.Value.ToLower() == "draft").Select(x => x.ID).FirstOrDefault(); sellernotes.Category = addnotesviewmodel.Category; sellernotes.NoteType = addnotesviewmodel.NoteType; sellernotes.NumberOfPages = addnotesviewmodel.NumberofPages; sellernotes.Description = addnotesviewmodel.Description.Trim(); sellernotes.UniversityName = addnotesviewmodel.UniversityName.Trim(); sellernotes.Country = addnotesviewmodel.Country; sellernotes.Course = addnotesviewmodel.Course.Trim(); sellernotes.CourseCode = addnotesviewmodel.CourseCode.Trim(); sellernotes.Professor = addnotesviewmodel.Professor.Trim(); sellernotes.IsPaid = addnotesviewmodel.IsPaid; if (sellernotes.IsPaid) { sellernotes.SellingPrice = addnotesviewmodel.SellingPrice; } else { sellernotes.SellingPrice = 0; } sellernotes.CreatedDate = DateTime.Now; sellernotes.CreatedBy = user.ID; sellernotes.IsActive = true; // add note in database and save context.SellerNotes.Add(sellernotes); context.SaveChanges(); // get seller note sellernotes = context.SellerNotes.Find(sellernotes.ID); // if display picture is not null then save picture into directory and directory path into database if (addnotesviewmodel.DisplayPicture != null) { string displaypicturefilename = System.IO.Path.GetFileName(addnotesviewmodel.DisplayPicture.FileName); string displaypicturepath = "~/Members/" + user.ID + "/" + sellernotes.ID + "/"; CreateDirectoryIfMissing(displaypicturepath); string displaypicturefilepath = Path.Combine(Server.MapPath(displaypicturepath), displaypicturefilename); sellernotes.DisplayPicture = displaypicturepath + displaypicturefilename; addnotesviewmodel.DisplayPicture.SaveAs(displaypicturefilepath); } // if note preview is not null then save picture into directory and directory path into database if (addnotesviewmodel.NotesPreview != null) { string notespreviewfilename = System.IO.Path.GetFileName(addnotesviewmodel.NotesPreview.FileName); string notespreviewpath = "~/Members/" + user.ID + "/" + sellernotes.ID + "/"; CreateDirectoryIfMissing(notespreviewpath); string notespreviewfilepath = Path.Combine(Server.MapPath(notespreviewpath), notespreviewfilename); sellernotes.NotesPreview = notespreviewpath + notespreviewfilename; addnotesviewmodel.NotesPreview.SaveAs(notespreviewfilepath); } // update note preview path and display picture path and save changes context.SellerNotes.Attach(sellernotes); context.Entry(sellernotes).Property(x => x.DisplayPicture).IsModified = true; context.Entry(sellernotes).Property(x => x.NotesPreview).IsModified = true; context.SaveChanges(); // attachement files foreach (HttpPostedFileBase file in addnotesviewmodel.UploadNotes) { // check if file is null or not if (file != null) { // save file in directory string notesattachementfilename = System.IO.Path.GetFileName(file.FileName); string notesattachementpath = "~/Members/" + user.ID + "/" + sellernotes.ID + "/Attachements/"; CreateDirectoryIfMissing(notesattachementpath); string notesattachementfilepath = Path.Combine(Server.MapPath(notesattachementpath), notesattachementfilename); file.SaveAs(notesattachementfilepath); // create object of sellernotesattachement SellerNotesAttachments notesattachements = new SellerNotesAttachments { NoteID = sellernotes.ID, FileName = notesattachementfilename, FilePath = notesattachementpath, CreatedDate = DateTime.Now, CreatedBy = user.ID, IsActive = true }; // save seller notes attachement context.SellerNotesAttachments.Add(notesattachements); context.SaveChanges(); } } return(RedirectToAction("Dashboard", "SellYourNotes")); } // if model state is not valid else { // create object of addnotesviewmodel AddNotesViewModel viewModel = new AddNotesViewModel { NoteCategoryList = context.NoteCategories.Where(x => x.IsActive == true).ToList(), NoteTypeList = context.NoteTypes.Where(x => x.IsActive == true).ToList(), CountryList = context.Countries.Where(x => x.IsActive == true).ToList() }; return(View(viewModel)); } }