public void MaptoModel(Note_Details note, NotesAttachment attachment) { note.Title = Title; note.Category_Id = Category; //note.Image = DisplayPicture; note.Type_Id = Type; note.Pages = Pages; note.Description = Description; note.University = Institute; note.Country_Id = Country; note.Course = Course; note.Course_Code = CourseCode; note.Professor = Professor; note.Price = Price; //note.Note_Preview = NotePreview; //attachment.FileName = UploadNotes; }
public static bool EditNote(int UserID, NoteModel Nm, string AppRoot) { using (var context = new NotesMarketPlaceEntities()) { var NoteInDB = context.Notes.FirstOrDefault(n => n.NoteID == Nm.NoteId && n.SellerID == UserID); if (NoteInDB == null) { return(false); } else if (NoteInDB.NoteStatus != 0) { return(false); } NoteInDB.Title = Nm.Title; NoteInDB.Category = context.NoteCategories.FirstOrDefault(n => n.CategoryName == Nm.Category).CategoryID; NoteInDB.NoteType = context.NoteTypes.FirstOrDefault(n => n.TypeName == Nm.Type).TypeID; NoteInDB.NoteDescription = Nm.Description; NoteInDB.Price = Nm.SellFor ? Nm.Price : 0; NoteInDB.University = Nm.Institution; NoteInDB.Country = context.Countries.FirstOrDefault(c => c.CountryName == Nm.Country).CountryID; NoteInDB.CourseCode = Nm.CourseCode; NoteInDB.Course = Nm.CourseName; NoteInDB.Professor = Nm.Professor; NoteInDB.NumberOfPages = Nm.Pages; NoteInDB.NoteStatus = Nm.Status; NoteInDB.IsPaid = Nm.SellFor; NoteInDB.ModifiedBy = UserID; NoteInDB.ModifiedDate = System.DateTime.Now; string ContentPath = AppRoot + "\\Content\\NotesData\\" + NoteInDB.NoteID.ToString() + "\\"; Directory.CreateDirectory(ContentPath); //Saving Preview And Coverpage if (Nm.NotesCoverPage != null || Nm.PreviewFile != null) { if (Nm.NotesCoverPage != null) { NoteInDB.DisplayPicture = @"/Content/NotesData/" + NoteInDB.NoteID.ToString() + @"/CoverPage-" + NoteInDB.NoteID.ToString() + Path.GetExtension(Nm.NotesCoverPage.FileName); Nm.NotesCoverPage.SaveAs(ContentPath + "CoverPage-" + NoteInDB.NoteID.ToString() + Path.GetExtension(Nm.NotesCoverPage.FileName)); } if (Nm.PreviewFile != null) { Nm.PreviewFile.SaveAs(ContentPath + "Preview-" + NoteInDB.NoteID.ToString() + ".pdf"); } } if (Nm.NotesFiles != null && Nm.NotesFiles[0] != null) { string filenames = ""; int i = 1; long filesize = 0; string Serverpath = AppRoot + "\\Members\\" + UserID + "\\Notes\\" + NoteInDB.NoteID + "\\"; Directory.CreateDirectory(Serverpath); DirectoryInfo Dir = new DirectoryInfo(Serverpath); foreach (FileInfo file in Dir.EnumerateFiles()) { file.Delete(); } foreach (HttpPostedFileBase file in Nm.NotesFiles) { if (file != null) { file.SaveAs(Serverpath + "Attachment-" + NoteInDB.NoteID + "-" + i.ToString() + ".pdf"); filenames += "Attachment-" + NoteInDB.NoteID + "-" + i.ToString() + ".pdf;"; i++; } } filesize = Dir.EnumerateFiles().Sum(file => file.Length) / 1000; string SizeFormatString = filesize > 1024 ? "{0:0.0}MB" : "{0:0}KB"; NotesAttachment Na = context.NotesAttachments.FirstOrDefault(n => n.NoteID == NoteInDB.NoteID); Na.FilesName = filenames; Na.ModifiedBy = UserID; Na.ModifiedDate = System.DateTime.Now; Na.AttachmentSize = String.Format(SizeFormatString, filesize > 1024 ? filesize / 1024 : filesize); } try { context.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e); return(false); } return(true); } }
/* Get Note Attachments folder location as string if user authenticated to download note. * if it submitted buyer request it return string "BuyerRequestSubmitted" * otherwise return null if note not found, * else if book is free or user has purchased the book it return attachment path * It also return notetitle as first element of array. */ public static string[] GetNoteAttachments(int NoteID, int BuyerID) { string ReturnPath = null; string NoteTitle = null; using (NotesMarketPlaceEntities context = new NotesMarketPlaceEntities()) { Note note = context.Notes.FirstOrDefault(n => n.NoteID == NoteID); /* if note not found return null */ if (note == null) { return(null); } /* if note is free or user is allowed to download note or user is book owner or user is admin then return attachment paths*/ else if ( !note.IsPaid || (context.Downloads.Any(d => d.NoteID == NoteID && d.BuyerID == BuyerID && d.IsAllowed)) || BuyerID == note.SellerID || context.Users.FirstOrDefault(u => u.UserID == BuyerID).RoleID < 3 ) { NotesAttachment notesAttachmentDB = context.NotesAttachments.FirstOrDefault(na => na.NoteID == NoteID); if (notesAttachmentDB == null) { return(null); } else { ReturnPath = notesAttachmentDB.FilePath; } //update download entry if (context.Downloads.Any(d => d.NoteID == NoteID && d.BuyerID == BuyerID && d.IsAllowed)) { Download dwn = context.Downloads.FirstOrDefault(d => d.NoteID == NoteID && d.BuyerID == BuyerID && d.IsAllowed); dwn.IsDownloaded = true; dwn.DownloadDate = System.DateTime.Now; dwn.ModifiedBy = BuyerID; dwn.ModifiedDate = System.DateTime.Now; } //if download entry not found and book is free create new entry only if owner is not downloading note else if ((!note.IsPaid && BuyerID != note.SellerID) || context.Users.FirstOrDefault(u => u.UserID == BuyerID).RoleID < 3) { Download dwn = new Download() { NoteID = NoteID, BuyerID = BuyerID, SellerID = note.SellerID, IsAllowed = true, IsDownloaded = true, DownloadDate = System.DateTime.Now, ModifiedBy = BuyerID, ModifiedDate = System.DateTime.Now, CreatedBy = BuyerID, CreatedDate = System.DateTime.Now, IsPaid = note.IsPaid, NoteCategory = note.Category, NoteTitle = note.Title, PurchasedPrice = note.Price, AttachmentsID = context.NotesAttachments.FirstOrDefault(a => a.NoteID == NoteID).AttachementID }; context.Downloads.Add(dwn); } } // if there is already a buyer request that seller has not accepted else if (context.Downloads.Any(d => d.NoteID == NoteID && d.BuyerID == BuyerID && !d.IsAllowed)) { ReturnPath = "BuyerRequestAlreadySubmitted"; } //if note is paid and there are no buyer requests create new one else if (note.IsPaid) { Download dwn = new Download() { NoteID = NoteID, BuyerID = BuyerID, SellerID = note.SellerID, IsAllowed = false, IsDownloaded = false, DownloadDate = null, ModifiedBy = BuyerID, ModifiedDate = System.DateTime.Now, CreatedBy = BuyerID, CreatedDate = System.DateTime.Now, IsPaid = note.IsPaid, NoteCategory = note.Category, NoteTitle = note.Title, PurchasedPrice = note.Price, AttachmentsID = context.NotesAttachments.FirstOrDefault(a => a.NoteID == NoteID).AttachementID }; context.Downloads.Add(dwn); //return confirmation that buyer request is submitted; seller email; seller full name User seller = context.Users.FirstOrDefault(u => u.UserID == note.SellerID); ReturnPath = "BuyerRequestSubmitted;" + seller.Email + ';' + seller.FirstName + ' ' + seller.LastName; } NoteTitle = context.Notes.FirstOrDefault(n => n.NoteID == NoteID).Title; context.SaveChanges(); } return(new string[] { NoteTitle, ReturnPath }); }
//Adds New Note to Database public static int AddNote(NoteModel Nm, int UID, string AppRoot) { using (NotesMarketPlaceEntities context = new NotesMarketPlaceEntities()) { Note note = new Note() { Title = Nm.Title, Category = context.NoteCategories.FirstOrDefault(n => n.CategoryName == Nm.Category).CategoryID, NoteType = context.NoteTypes.FirstOrDefault(n => n.TypeName == Nm.Type).TypeID, NoteDescription = Nm.Description, DisplayPicture = Nm.DisplayPicture, Price = Nm.SellFor ? Nm.Price : 0, University = Nm.Institution, Country = context.Countries.FirstOrDefault(c => c.CountryName == Nm.Country).CountryID, CourseCode = Nm.CourseCode, Course = Nm.CourseName, Professor = Nm.Professor, NumberOfPages = Nm.Pages, SellerID = UID, NoteStatus = Nm.Status, IsActive = true, IsPaid = Nm.SellFor, CreatedBy = UID, ModifiedBy = UID, CreatedDate = System.DateTime.Now, ModifiedDate = System.DateTime.Now }; context.Notes.Add(note); try { context.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e); return(0); } string filenames = ""; int i = 1; //Saving Preview And Coverpage if (Nm.NotesCoverPage != null || Nm.PreviewFile != null) { string ContentPath = AppRoot + "\\Content\\NotesData\\" + note.NoteID.ToString() + "\\"; Directory.CreateDirectory(ContentPath); if (Nm.NotesCoverPage != null) { note.DisplayPicture = @"/Content/NotesData/" + note.NoteID.ToString() + @"/CoverPage-" + note.NoteID.ToString() + Path.GetExtension(Nm.NotesCoverPage.FileName); Nm.NotesCoverPage.SaveAs(ContentPath + "CoverPage-" + note.NoteID.ToString() + Path.GetExtension(Nm.NotesCoverPage.FileName)); } if (Nm.PreviewFile != null) { note.Preview = @"/Content/NotesData/" + note.NoteID.ToString() + @"/Preview-" + note.NoteID.ToString() + Path.GetExtension(Nm.PreviewFile.FileName); Nm.PreviewFile.SaveAs(ContentPath + "Preview-" + note.NoteID.ToString() + Path.GetExtension(Nm.PreviewFile.FileName)); } } string Serverpath = AppRoot + "\\Members\\" + UID + "\\Notes\\" + note.NoteID + "\\"; Directory.CreateDirectory(Serverpath); DirectoryInfo Dir = new DirectoryInfo(Serverpath); foreach (HttpPostedFileBase file in Nm.NotesFiles) { if (file != null) { file.SaveAs(Serverpath + "Attachment-" + note.NoteID + "-" + i.ToString() + ".pdf"); filenames += "Attachment-" + note.NoteID + "-" + i.ToString() + ".pdf;"; i++; } } double filesize = Dir.EnumerateFiles().Sum(file => file.Length) / 1000; string SizeFormatString = filesize > 1024 ? "{0:0.0}MB" : "{0:0}KB"; NotesAttachment Na = new NotesAttachment() { NoteID = note.NoteID, FilePath = UID.ToString() + @"/Notes/" + note.NoteID + @"/", FilesName = filenames, CreatedBy = UID, ModifiedBy = UID, IsActive = true, ModifiedDate = System.DateTime.Now, CreatedDate = System.DateTime.Now, AttachmentSize = String.Format(SizeFormatString, filesize > 1024 ? filesize / 1024 : filesize) }; context.NotesAttachments.Add(Na); try { context.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e); return(0); } return(note.NoteID); } }