public ActionResult MakeNoteInReview(int id) { // get note by id var note = _dbcontext.SellerNotes.Where(x => x.ID == id).FirstOrDefault(); if (note == null) { return(HttpNotFound()); } // get logged in user var user = _dbcontext.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault(); // notes status id for in review var inreviewid = _dbcontext.ReferenceDatas.Where(x => x.Value.ToLower() == "in review").Select(x => x.ID).FirstOrDefault(); // update status note.Status = inreviewid; note.ModifiedDate = DateTime.Now; note.ModifiedBy = user.UserID; // save note in database _dbcontext.Entry(note).State = EntityState.Modified; _dbcontext.SaveChanges(); return(RedirectToAction("NotesUnderReview")); }
public ActionResult UnPublishNote(FormCollection form) { int noteid = Convert.ToInt32(form["noteid"]); string remark = form["unpublish-remark"]; // get user var user = _dbcontext.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault(); // get note var note = _dbcontext.SellerNotes.Where(x => x.ID == noteid).FirstOrDefault(); // get note status removed id int removedid = _dbcontext.ReferenceDatas.Where(x => x.Value.ToLower() == "removed").Select(x => x.ID).FirstOrDefault(); if (note == null) { return(HttpNotFound()); } // update note status note.Status = removedid; note.ModifiedBy = user.UserID; note.ModifiedDate = DateTime.Now; // save note in database _dbcontext.Entry(note).State = EntityState.Modified; _dbcontext.SaveChanges(); // get seller user objecct var seller = _dbcontext.Users.Where(x => x.UserID == note.SellerID).FirstOrDefault(); // send mail to seller UnpublishNoteTemplate(remark, seller); return(RedirectToAction("Dashboard", "Admin")); }
public ActionResult ChangePassword(ChangePasswordViewModel changepasswordviewmodel) { // check if model state is valid or not if (ModelState.IsValid) { // get logged in user var loggedinuser = _dbcontext.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault(); // check if user is logged in or not if (loggedinuser != null) { // match old password if (loggedinuser.Password == changepasswordviewmodel.OldPassword) { // update password loggedinuser.Password = changepasswordviewmodel.NewPassword; loggedinuser.ModifiedDate = DateTime.Now; loggedinuser.ModifiedBy = loggedinuser.UserID; _dbcontext.Users.Attach(loggedinuser); _dbcontext.Entry(loggedinuser).Property(x => x.Password).IsModified = true; _dbcontext.Entry(loggedinuser).Property(x => x.ModifiedDate).IsModified = true; _dbcontext.Entry(loggedinuser).Property(x => x.ModifiedBy).IsModified = true; _dbcontext.SaveChanges(); return(RedirectToAction("login")); } else { // password mismatch error ModelState.AddModelError("OldPassword", "Your old password is not match with your current pasword"); return(View(changepasswordviewmodel)); } } // if user is not logged in else { return(RedirectToAction("Login")); } } else { return(View(changepasswordviewmodel)); } }
public ActionResult DeactiveMember(int memberid) { // get logged in admin var user = _dbcontext.Users.Where(x => x.EmailID == User.Identity.Name).First(); // get ids of note status removed and published var removedid = _dbcontext.ReferenceDatas.Where(x => x.Value.ToLower() == "removed").Select(x => x.ID).FirstOrDefault(); var publishedid = _dbcontext.ReferenceDatas.Where(x => x.Value.ToLower() == "published").Select(x => x.ID).FirstOrDefault(); // get member by member id var member = _dbcontext.Users.Where(x => x.UserID == memberid && x.IsActive == true).First(); // make member inactive member.IsActive = false; member.ModifiedDate = DateTime.Now; member.ModifiedBy = user.UserID; // save updated member record _dbcontext.Entry(member).State = EntityState.Modified; _dbcontext.SaveChanges(); // get member's published notes list var notelist = _dbcontext.SellerNotes.Where(x => x.SellerID == member.UserID && x.Status == publishedid && x.IsActive == true).ToList(); // make member's each published note status removed foreach (var note in notelist) { note.Status = removedid; note.ModifiedDate = DateTime.Now; note.ModifiedBy = user.UserID; _dbcontext.Entry(note).State = EntityState.Modified; _dbcontext.SaveChanges(); } return(RedirectToAction("Members", "AdminMembers")); }
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 = _dbcontext.NoteCategories.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.NoteTypeList = _dbcontext.NoteTypes.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.CountryList = _dbcontext.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 = _dbcontext.NoteCategories.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.NoteTypeList = _dbcontext.NoteTypes.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.CountryList = _dbcontext.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 = _dbcontext.NoteCategories.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.NoteTypeList = _dbcontext.NoteTypes.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.CountryList = _dbcontext.Countries.Where(x => x.IsActive == true).ToList(); return(View(addnotesviewmodel)); } } // check model state if (ModelState.IsValid) { // create seller note object SellerNote sellernotes = new SellerNote(); User user = _dbcontext.Users.FirstOrDefault(x => x.EmailID == User.Identity.Name); sellernotes.SellerID = user.UserID; sellernotes.Title = addnotesviewmodel.Title.Trim(); sellernotes.Status = _dbcontext.ReferenceDatas.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.UserID; sellernotes.IsActive = true; // add note in database and save _dbcontext.SellerNotes.Add(sellernotes); _dbcontext.SaveChanges(); // get seller note sellernotes = _dbcontext.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.UserID + "/" + 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.UserID + "/" + 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 _dbcontext.SellerNotes.Attach(sellernotes); _dbcontext.Entry(sellernotes).Property(x => x.DisplayPicture).IsModified = true; _dbcontext.Entry(sellernotes).Property(x => x.NotesPreview).IsModified = true; _dbcontext.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.UserID + "/" + sellernotes.ID + "/Attachements/"; CreateDirectoryIfMissing(notesattachementpath); string notesattachementfilepath = Path.Combine(Server.MapPath(notesattachementpath), notesattachementfilename); file.SaveAs(notesattachementfilepath); // create object of sellernotesattachement SellerNotesAttachment notesattachements = new SellerNotesAttachment { NoteID = sellernotes.ID, FileName = notesattachementfilename, FilePath = notesattachementpath, CreatedDate = DateTime.Now, CreatedBy = user.UserID, IsActive = true }; // save seller notes attachement _dbcontext.SellerNotesAttachments.Add(notesattachements); _dbcontext.SaveChanges(); } } return(RedirectToAction("Dashboard", "SellYourNotes")); } // if model state is not valid else { // create object of addnotesviewmodel AddNotesViewModel viewModel = new AddNotesViewModel { NoteCategoryList = _dbcontext.NoteCategories.Where(x => x.IsActive == true).ToList(), NoteTypeList = _dbcontext.NoteTypes.Where(x => x.IsActive == true).ToList(), CountryList = _dbcontext.Countries.Where(x => x.IsActive == true).ToList() }; return(View(viewModel)); } }
public ActionResult UserProfile(UserProfileViewModel userprofileviewmodel) { // get logged in user var user = _dbcontext.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault(); // check if ModelState is valid or not if (ModelState.IsValid) { // check if profile picture is not null if (userprofileviewmodel.ProfilePicture != null) { // get image size and check that it is not more than 10 MB var profilepicsize = userprofileviewmodel.ProfilePicture.ContentLength; if (profilepicsize > 10 * 1024 * 1024) { // if image size is more than 10 MB show error ModelState.AddModelError("ProfilePicture", "Image size limit is 10 MB"); userprofileviewmodel.CountryList = _dbcontext.Countries.Where(x => x.IsActive == true).ToList(); userprofileviewmodel.GenderList = _dbcontext.ReferenceDatas.Where(x => x.RefCategory == "Gender" && x.IsActive == true).ToList(); return(View(userprofileviewmodel)); } } // get logged in user's profile if exists var profile = _dbcontext.UserProfiles.Where(x => x.UserID == user.UserID).FirstOrDefault(); // if profile is not null then edit userprofile if (profile != null) { profile.DOB = userprofileviewmodel.DOB; profile.Gender = userprofileviewmodel.Gender; profile.PhonenumberCountryCode = userprofileviewmodel.PhoneNumberCountryCode.Trim(); profile.PhoneNumber = userprofileviewmodel.PhoneNumber.Trim(); profile.AddressLine1 = userprofileviewmodel.AddressLine1.Trim(); profile.AddressLine2 = userprofileviewmodel.AddressLine2.Trim(); profile.City = userprofileviewmodel.City.Trim(); profile.State = userprofileviewmodel.State.Trim(); profile.ZipCode = userprofileviewmodel.ZipCode.Trim(); profile.Country = userprofileviewmodel.Country.Trim(); profile.University = userprofileviewmodel.University.Trim(); profile.College = userprofileviewmodel.College.Trim(); profile.ModifiedDate = DateTime.Now; profile.ModifiedBy = user.UserID; // check if loggedin user's profile picture is not null and user upload new profile picture then delete old one if (userprofileviewmodel.ProfilePicture != null && profile.ProfilePicture != null) { string path = Server.MapPath(profile.ProfilePicture); FileInfo file = new FileInfo(path); if (file.Exists) { file.Delete(); } } // if user upload profile picture then save it and store path in database if (userprofileviewmodel.ProfilePicture != null) { string filename = System.IO.Path.GetFileName(userprofileviewmodel.ProfilePicture.FileName); string fileextension = System.IO.Path.GetExtension(userprofileviewmodel.ProfilePicture.FileName); string newfilename = "DP_" + DateTime.Now.ToString("ddMMyyyy_hhmmss") + fileextension; string profilepicturepath = "~/Members/" + profile.UserID + "/"; CreateDirectoryIfMissing(profilepicturepath); string path = Path.Combine(Server.MapPath(profilepicturepath), newfilename); profile.ProfilePicture = profilepicturepath + newfilename; userprofileviewmodel.ProfilePicture.SaveAs(path); } // update logged in user's profile and save _dbcontext.Entry(profile).State = EntityState.Modified; _dbcontext.SaveChanges(); // update first name and lastname and save user.FirstName = userprofileviewmodel.FirstName.Trim(); user.LastName = userprofileviewmodel.LastName.Trim(); _dbcontext.Entry(user).State = EntityState.Modified; _dbcontext.SaveChanges(); } // if profile is null then create user profile else { // create new userprofile object UserProfile userprofile = new UserProfile(); userprofile.UserID = user.UserID; userprofile.DOB = userprofileviewmodel.DOB; userprofile.Gender = userprofileviewmodel.Gender; userprofile.PhonenumberCountryCode = userprofileviewmodel.PhoneNumberCountryCode.Trim(); userprofile.PhoneNumber = userprofileviewmodel.PhoneNumber.Trim(); userprofile.AddressLine1 = userprofileviewmodel.AddressLine1.Trim(); userprofile.AddressLine2 = userprofileviewmodel.AddressLine2.Trim(); userprofile.City = userprofileviewmodel.City.Trim(); userprofile.State = userprofileviewmodel.State.Trim(); userprofile.ZipCode = userprofileviewmodel.ZipCode.Trim(); userprofile.Country = userprofileviewmodel.Country.Trim(); userprofile.University = userprofileviewmodel.University.Trim(); userprofile.College = userprofileviewmodel.College.Trim(); userprofile.CreatedDate = DateTime.Now; userprofile.CreatedBy = user.UserID; // if profile picture is not null then save it and store path in database with filename is timestamp if (userprofileviewmodel.ProfilePicture != null) { string filename = System.IO.Path.GetFileName(userprofileviewmodel.ProfilePicture.FileName); string fileextension = System.IO.Path.GetExtension(userprofileviewmodel.ProfilePicture.FileName); string newfilename = "DP_" + DateTime.Now.ToString("ddMMyyyy_hhmmss") + fileextension; string profilepicturepath = "~/Members/" + userprofile.UserID + "/"; CreateDirectoryIfMissing(profilepicturepath); string path = Path.Combine(Server.MapPath(profilepicturepath), newfilename); userprofile.ProfilePicture = profilepicturepath + newfilename; userprofileviewmodel.ProfilePicture.SaveAs(path); } // save logged in user's profile _dbcontext.UserProfiles.Add(userprofile); _dbcontext.SaveChanges(); // save firstname and lastname and save user.FirstName = userprofileviewmodel.FirstName.Trim(); user.LastName = userprofileviewmodel.LastName.Trim(); _dbcontext.Entry(user).State = EntityState.Modified; _dbcontext.SaveChanges(); } // if userprofile is created or edited then redirect to search page return(RedirectToAction("Search", "SearchNotes")); } // if ModelState is invalid then redirect to userProfile page else { userprofileviewmodel.CountryList = _dbcontext.Countries.Where(x => x.IsActive == true).ToList(); userprofileviewmodel.GenderList = _dbcontext.ReferenceDatas.Where(x => x.RefCategory == "Gender" && x.IsActive == true).ToList(); return(View(userprofileviewmodel)); } }
public ActionResult MyProfile(AdminProfileViewModel obj) { // get logged in user var user = _dbcontext.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault(); // get logged in user profile var userprofile = _dbcontext.UserProfiles.Where(x => x.UserID == user.UserID).FirstOrDefault(); // check if secondary email is already exists in User or UserProfile table or not // if email already exists then give error bool secondaryemailalreadyexistsinusers = _dbcontext.Users.Where(x => x.EmailID == obj.SecondaryEmail).Any(); bool secondaryemailalreadyexistsinuserprofile = _dbcontext.UserProfiles.Where(x => x.SecondaryEmailAddress == obj.Email && x.UserID != user.UserID).Any(); if (secondaryemailalreadyexistsinusers || secondaryemailalreadyexistsinuserprofile) { ModelState.AddModelError("SecondaryEmail", "This email address is already exists"); obj.CountryCodeList = _dbcontext.Countries.Where(x => x.IsActive).OrderBy(x => x.CountryCode).Select(x => x.CountryCode).ToList(); return(View(obj)); } // update user's data user.FirstName = obj.FirstName.Trim(); user.LastName = obj.LastName.Trim(); // update userprofile's data userprofile.SecondaryEmailAddress = obj.SecondaryEmail.Trim(); userprofile.PhonenumberCountryCode = obj.PhoneNumberCountryCode.Trim(); userprofile.PhoneNumber = obj.PhoneNumber.Trim(); // user upploaded profile picture and there is also previous profile picture then delete previous profile picture if (userprofile.ProfilePicture != null && obj.ProfilePicture != null) { string path = Server.MapPath(userprofile.ProfilePicture); FileInfo file = new FileInfo(path); if (file.Exists) { file.Delete(); } } // save new profile picture and update data in userprofile table if (obj.ProfilePicture != null) { // get extension string fileextension = System.IO.Path.GetExtension(obj.ProfilePicture.FileName); // set new name of file string newfilename = "DP_" + DateTime.Now.ToString("ddMMyyyy_hhmmss") + fileextension; // set where to save picture string profilepicturepath = "~/Members/" + userprofile.UserID + "/"; // create directory if not exists CreateDirectoryIfMissing(profilepicturepath); // get physical path and save profile picture there string path = Path.Combine(Server.MapPath(profilepicturepath), newfilename); obj.ProfilePicture.SaveAs(path); // save path in database userprofile.ProfilePicture = profilepicturepath + newfilename; } // update userprofile's data userprofile.ModifiedDate = DateTime.Now; userprofile.ModifiedBy = user.UserID; // update data and save data in database _dbcontext.Entry(user).State = EntityState.Modified; _dbcontext.Entry(userprofile).State = EntityState.Modified; _dbcontext.SaveChanges(); return(RedirectToAction("Dashboard", "Admin")); }
public ActionResult DownloadNotes(int noteid) { // get note var note = _dbcontext.SellerNotes.Find(noteid); // if note is not found if (note == null) { return(HttpNotFound()); } // get first object of seller note attachement for attachement var noteattachement = _dbcontext.SellerNotesAttachments.Where(x => x.NoteID == note.ID).FirstOrDefault(); // get logged in user var user = _dbcontext.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault(); // variable for attachement path string path; // note's seller if and logged in user's id is same it means user want to download his own book // then we need to provide downloaded note without entry in download tables if (note.SellerID == user.UserID) { // get attavhement path path = Server.MapPath(noteattachement.FilePath); DirectoryInfo dir = new DirectoryInfo(path); // create zip of attachement using (var memoryStream = new MemoryStream()) { using (var ziparchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { foreach (var item in dir.GetFiles()) { // file path is attachement path + file name string filepath = path + item.ToString(); ziparchive.CreateEntryFromFile(filepath, item.ToString()); } } // return zip return(File(memoryStream.ToArray(), "application/zip", note.Title + ".zip")); } } // if note is free then we need to add entry in download table with allow download is true // downloaded date time is current date time for first time download // if user download again then we have to return zip of attachement without changes in data if (note.IsPaid == false) { // if user has already downloaded note then get download object var downloadfreenote = _dbcontext.Downloads.Where(x => x.NoteID == noteid && x.Downloader == user.UserID && x.IsSellerHasAllowedDownload == true && x.AttachementPath != null).FirstOrDefault(); // if user is not downloaded if (downloadfreenote == null) { // create download object Download download = new Download { NoteID = note.ID, Seller = note.SellerID, Downloader = user.UserID, IsSellerHasAllowedDownload = true, AttachementPath = noteattachement.FilePath, IsAttachementDownload = true, AttaachementDownloadID = DateTime.Now, IsPaid = note.IsPaid, PurchasedPrice = note.SellingPrice, NoteTitle = note.Title, NoteCategory = note.NoteCategory.Name, CreatedDate = DateTime.Now, CreatedBy = user.UserID, }; // save download object in database _dbcontext.Downloads.Add(download); _dbcontext.SaveChanges(); path = Server.MapPath(download.AttachementPath); } // if user is already downloaded note then get attachement path else { path = Server.MapPath(downloadfreenote.AttachementPath); } DirectoryInfo dir = new DirectoryInfo(path); // create zip of attachement using (var memoryStream = new MemoryStream()) { using (var ziparchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { foreach (var item in dir.GetFiles()) { // file path is attachement path + file name string filepath = path + item.ToString(); ziparchive.CreateEntryFromFile(filepath, item.ToString()); } } // return zip return(File(memoryStream.ToArray(), "application/zip", note.Title + ".zip")); } } // if note is paid else { // get download object var downloadpaidnote = _dbcontext.Downloads.Where(x => x.NoteID == noteid && x.Downloader == user.UserID && x.IsSellerHasAllowedDownload == true && x.AttachementPath != null).FirstOrDefault(); // if user is not already downloaded if (downloadpaidnote != null) { // if user is download note first time then we need to update following record in download table if (downloadpaidnote.IsAttachementDownload == false) { downloadpaidnote.IsAttachementDownload = true; downloadpaidnote.ModifiedDate = DateTime.Now; downloadpaidnote.ModifiedBy = user.UserID; // update ans save data in database _dbcontext.Entry(downloadpaidnote).State = EntityState.Modified; _dbcontext.SaveChanges(); } // get attachement path path = Server.MapPath(downloadpaidnote.AttachementPath); DirectoryInfo dir = new DirectoryInfo(path); // create zip of attachement using (var memoryStream = new MemoryStream()) { using (var ziparchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { foreach (var item in dir.GetFiles()) { // file path is attachement path + file name string filepath = path + item.ToString(); ziparchive.CreateEntryFromFile(filepath, item.ToString()); } } // return zip return(File(memoryStream.ToArray(), "application/zip", note.Title + ".zip")); } } return(RedirectToAction("Notes", "SearchNotes", new { id = noteid })); } }