//This method adds entry information //and also file contents to the database //whenever new Questions are added Boolean AddSolution(HttpPostedFileBase hpf, String FileName, UploadModel model) { AnswerApp.Models.AnswerAppDataContext db = new AnswerApp.Models.AnswerAppDataContext(); //Disect the file name for it's file properties String[] properties = FileName.Split(new char[1] { '_' }); String Textbook_Title = properties[0]; String Unit_Title = properties[1]; String Chapter_Title = properties[2]; String Section_Title = properties[3]; String Page_Number = properties[4]; String Question_Number = properties[5].Split(new char[1] { '.' })[0];//Truncate ".pdf" from the end of the file name String Practice_Problem = null; if (properties.Length > 6) { Practice_Problem = properties[6]; }//An 7th argument indicates a Practice Problem if (Practice_Problem != null) { Practice_Problem = properties[6].Split(new char[1] { '.' })[0]; }//Truncate ".pdf" from the end of the file name //Search teh database for this Textbook IQueryable<Textbook> RetrievedTextbooks = from theTextbooks in db.Textbooks where theTextbooks.Title.Equals(Textbook_Title) select theTextbooks; Textbook[] TextbookResults = RetrievedTextbooks.ToArray<Textbook>(); if (TextbookResults.Length == 0)//The Textbook does not yet exists { //Create a new Textbook AnswerApp.Models.Textbook theTextbook = new AnswerApp.Models.Textbook(); //Populate the Textbook with the properties extracted from the file name theTextbook.Title = Textbook_Title; if (theTextbook.Title.Length < 1) { return false; } db.Textbooks.InsertOnSubmit(theTextbook); db.SubmitChanges(); } //Search teh database for this Unit IQueryable<Unit> RetrievedUnits = from theUnits in db.Units where theUnits.Textbook_Title.Equals(Textbook_Title) && theUnits.Unit_Title.Equals(Unit_Title) select theUnits; Unit[] UnitResults = RetrievedUnits.ToArray<Unit>(); if (UnitResults.Length == 0)//The Unit does not yet exists { //Create a new Unit AnswerApp.Models.Unit theUnit = new AnswerApp.Models.Unit(); //Populate the Unit with the properties extracted from the file name theUnit.Textbook_Title = Textbook_Title; theUnit.Unit_Title = Unit_Title; //Populate the relational Id's based on previous hierarchical entries theUnit.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id; db.Units.InsertOnSubmit(theUnit); db.SubmitChanges(); } //Search the database for this Chapter IQueryable<Chapter> RetrievedChapters = from theChapters in db.Chapters where theChapters.Textbook_Title.Equals(Textbook_Title) && theChapters.Unit_Title.Equals(Unit_Title) && theChapters.Chapter_Title.Equals(Chapter_Title) select theChapters; Chapter[] ChapterResults = RetrievedChapters.ToArray<Chapter>(); if (ChapterResults.Length == 0)//The Chapter does not yet exists { //Create a new Chapter AnswerApp.Models.Chapter theChapter = new AnswerApp.Models.Chapter(); //Populate the Chapter with the properties extracted from the file name theChapter.Textbook_Title = Textbook_Title; theChapter.Unit_Title = Unit_Title; theChapter.Chapter_Title = Chapter_Title; //Populate the relational Id's based on previous hierarchical entries theChapter.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id; theChapter.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id; if (theChapter.Chapter_Title.Equals("MACOSX")) { return false;//"Error: You have attempted to add a chapter titled MACOSX... ... ... gAwD! O.o ...skipping..."; } db.Chapters.InsertOnSubmit(theChapter); db.SubmitChanges(); } //Search teh database for this Section IQueryable<Section> RetrievedSections = from theSections in db.Sections where theSections.Textbook_Title.Equals(Textbook_Title) && theSections.Unit_Title.Equals(Unit_Title) && theSections.Chapter_Title.Equals(Chapter_Title) && theSections.Section_Title.Equals(Section_Title) select theSections; Section[] SectionResults = RetrievedSections.ToArray<Section>(); if (SectionResults.Length == 0)//The Section does not yet exists { //Create a new Section AnswerApp.Models.Section theSection = new AnswerApp.Models.Section(); //Populate the Section with the properties extracted from the file name theSection.Textbook_Title = Textbook_Title; theSection.Unit_Title = Unit_Title; theSection.Chapter_Title = Chapter_Title; theSection.Section_Title = Section_Title; //Populate the relational Id's based on previous hierarchical entries theSection.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id; theSection.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id; theSection.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id; if (theSection.Section_Title.Length < 1) { return false; } db.Sections.InsertOnSubmit(theSection); db.SubmitChanges(); } //Search teh database for this Page IQueryable<Page> RetrievedPages = from thePages in db.Pages where thePages.Textbook_Title.Equals(Textbook_Title) //&& thePages.Unit_Title.Equals(Unit_Title) //&& thePages.Chapter_Title.Equals(Chapter_Title) //&& thePages.Section_Title.Equals(Section_Title) && thePages.Page_Number.Equals(Page_Number) select thePages; Page[] PageResults = RetrievedPages.ToArray<Page>(); if (PageResults.Length == 0)//The Page does not yet exists { //Create a new Page AnswerApp.Models.Page thePage = new AnswerApp.Models.Page(); //Populate the Page with the properties extracted from the file name thePage.Textbook_Title = Textbook_Title; thePage.Unit_Title = Unit_Title; thePage.Chapter_Title = Chapter_Title; thePage.Section_Title = Section_Title; thePage.Page_Number = Page_Number; //Populate the relational Id's based on previous hierarchical entries thePage.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id; thePage.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id; thePage.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id; thePage.Section_Id = db.Sections.Single(d => d.Section_Title.Equals(Section_Title)).Section_Id; int test; if (!int.TryParse(thePage.Page_Number, out test)) { return false; } db.Pages.InsertOnSubmit(thePage); db.SubmitChanges(); } //Search teh database for this Question IQueryable<Question> retrieved = from theAnswers in db.Questions where theAnswers.Textbook_Title.Equals(Textbook_Title) && theAnswers.Unit_Title.Equals(Unit_Title) && theAnswers.Chapter_Title.Equals(Chapter_Title) && theAnswers.Section_Title.Equals(Section_Title) && theAnswers.Page_Number.Equals(Page_Number) && theAnswers.Question_Number.Equals(Question_Number) select theAnswers; Question[] results = retrieved.ToArray<Question>(); if (results.Length != 0)//The Answer already exists { //Use the existing Question AnswerApp.Models.Question theQuestion = results.First(); if (Practice_Problem != null)//This is a Practice Problem { theQuestion.Practice_Problem = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer; } else//(Practice_Problem == null) This is an Answer { theQuestion.Answer = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer; } } else//(results.Length == 0) This is a new Answer { //Create a new Question AnswerApp.Models.Question theQuestion = new AnswerApp.Models.Question(); //Populate the Question with the properties extracted from the file name theQuestion.Textbook_Title = Textbook_Title; theQuestion.Unit_Title = Unit_Title; theQuestion.Chapter_Title = Chapter_Title; theQuestion.Section_Title = Section_Title; theQuestion.Page_Number = Page_Number; theQuestion.Question_Number = Question_Number; //Populate the relational Id's based on previous hierarchical entries theQuestion.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id; theQuestion.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id; theQuestion.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id; theQuestion.Section_Id = db.Sections.Single(d => d.Section_Title.Equals(Section_Title)).Section_Id; theQuestion.Page_Id = db.Pages.Single(d => d.Page_Number.Equals(Page_Number)).Page_Id; if (Practice_Problem != null)//This is a Practice Problem { theQuestion.Practice_Problem = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer; } else//(Practice_Problem == null) This is an Answer { theQuestion.Answer = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer; } int test; if (!int.TryParse(theQuestion.Page_Number, out test)) { return false; } //Insert the new Question into the database db.Questions.InsertOnSubmit(theQuestion); } db.SubmitChanges();//Commit the changes to the database. return true; }
//This method adds entry information //and also file contents to the database //whenever new Images are added Boolean AddImage(HttpPostedFileBase hpf, String FileName, UploadModel model) { AnswerApp.Models.AnswerAppDataContext db = new AnswerApp.Models.AnswerAppDataContext(); //Disect the file name for it's file properties String ExtensionlessFileName = FileName.Replace(".jpg", ""); ExtensionlessFileName = ExtensionlessFileName.Replace(".gif", ""); String[] properties = ExtensionlessFileName.Split(new char[2] { '_', '/' }); String Textbook_Title = null; String Unit_Title = null; String Chapter_Title = null; //String Section_Title = null; if (properties.Length > 0) { Textbook_Title = properties[0]; } if (properties.Length > 1) { Unit_Title = properties[1]; } if (properties.Length > 2) { Chapter_Title = properties[2]; } //if (properties.Length > 3) { Section_Title = properties[2]; } Boolean Textbook_Image = false; Boolean Unit_Image = false; Boolean Chapter_Image = false; //Boolean Section_Image = false; if (properties.Length == 1) { Textbook_Image = true; } if (properties.Length == 2) { Unit_Image = true; } if (properties.Length == 3) { Chapter_Image = true; } //if (properties.Length == 4) { SectionImage = true; } if (Textbook_Title != null) { //ViewData["Info"] += "(in3)"; //Search the database for this Textbook IQueryable<Textbook> RetrievedTextbooks = from theTextbooks in db.Textbooks where theTextbooks.Title.Equals(Textbook_Title) select theTextbooks; Textbook[] TextbookResults = RetrievedTextbooks.ToArray<Textbook>(); if (TextbookResults.Length == 0)//The Textbook does not yet exists { //Create a new Textbook AnswerApp.Models.Textbook theTextbook = new AnswerApp.Models.Textbook(); //Populate the Textbook with the properties extracted from the file name theTextbook.Title = Textbook_Title; if (theTextbook.Title.Length < 1) { return false; } if (Textbook_Image) { theTextbook.Image = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); } db.Textbooks.InsertOnSubmit(theTextbook); } else if (Textbook_Image) { //Gather a handle to the existing textbook AnswerApp.Models.Textbook theTextbook = TextbookResults.First(); //Populate the existing Textbook with the properties extracted from the file name theTextbook.Title = Textbook_Title; theTextbook.Image = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); } db.SubmitChanges(); } if (Unit_Title != null) { //Search teh database for this Unit IQueryable<Unit> RetrievedUnits = from theUnits in db.Units where theUnits.Textbook_Title.Equals(Textbook_Title) && theUnits.Unit_Title.Equals(Unit_Title) select theUnits; Unit[] UnitResults = RetrievedUnits.ToArray<Unit>(); if (UnitResults.Length == 0)//The Unit does not yet exists { //Create a new Unit AnswerApp.Models.Unit theUnit = new AnswerApp.Models.Unit(); //Populate the Unit with the properties extracted from the file name theUnit.Textbook_Title = Textbook_Title; theUnit.Unit_Title = Unit_Title; //Populate the relational Id's based on previous hierarchical entries theUnit.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id; if (theUnit.Unit_Title.Length < 1) { return false;// "Error: You have attempted to add a gUnit without a Unit Title. skipping..."; } if (Unit_Image) { theUnit.Image = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); } db.Units.InsertOnSubmit(theUnit); } else if (Unit_Image) { //Gather a handle to the existing Unit AnswerApp.Models.Unit theUnit = UnitResults.First(); //Populate the existing Unit with the properties extracted from the file name theUnit.Unit_Title = Unit_Title; theUnit.Image = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); } db.SubmitChanges(); } if (Chapter_Title != null) { //Search the database for this Chapter IQueryable<Chapter> RetrievedChapters = from theChapters in db.Chapters where theChapters.Textbook_Title.Equals(Textbook_Title) && theChapters.Unit_Title.Equals(Unit_Title) && theChapters.Chapter_Title.Equals(Chapter_Title) select theChapters; Chapter[] ChapterResults = RetrievedChapters.ToArray<Chapter>(); if (ChapterResults.Length == 0)//The Chapter does not yet exists { //Create a new Chapter AnswerApp.Models.Chapter theChapter = new AnswerApp.Models.Chapter(); //Populate the Chapter with the properties extracted from the file name theChapter.Textbook_Title = Textbook_Title; theChapter.Unit_Title = Unit_Title; theChapter.Chapter_Title = Chapter_Title; //Populate the relational Id's based on previous hierarchical entries theChapter.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id; theChapter.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id; if (theChapter.Chapter_Title.Equals("MACOSX")) { return false;//"Error: You have attempted to add a chapter titled MACOSX... ... ... gAwD! O.o ...skipping..."; } if (Chapter_Image) { theChapter.Image = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); } db.Chapters.InsertOnSubmit(theChapter); } else if (Chapter_Image) { //Gather a handle to the existing Chapter AnswerApp.Models.Chapter theChapter = ChapterResults.First(); //Populate the existing CHapter with the properties extracted from the file name theChapter.Chapter_Title = Chapter_Title; theChapter.Image = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); } db.SubmitChanges(); } /* if(Section_Title != null) { //Search the database for this Section IQueryable<Section> RetrievedSections = from theSections in db.Sections where theSections.Textbook_Title.Equals(Textbook_Title) && theSections.Unit_Title.Equals(Unit_Title) && theSections.Chapter_Title.Equals(Chapter_Title) && theSections.Section_Title.Equals(Section_Title) select theSections; Section[] SectionResults = RetrievedSections.ToArray<Section>(); if (SectionResults.Length == 0)//The Section does not yet exists { //Create a new Section AnswerApp.Models.Section theSection = new AnswerApp.Models.Section(); //Populate the Section with the properties extracted from the file name theSection.Textbook_Title = Textbook_Title; theSection.Unit_Title = Unit_Title; theSection.Chapter_Title = Chapter_Title; theSection.Section_Title = Section_Title; //Populate the relational Id's based on previous hierarchical entries theSection.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id; theSection.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id; theSection.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id; db.Sections.InsertOnSubmit(theSection); db.SubmitChanges(); } } */ db.SubmitChanges();//Commit the changes to the database. return true; }
//Allows the user to upload new Answers along with their respective //Practice Problem and the answer to that Practice Problem public ActionResult Upload(UploadModel model, string returnUrl) { if (!Request.IsAuthenticated) { return RedirectToAction("ResourceUnavailable", "Home"); } if (!(User.Identity.Name.Equals("mcaskilladmin") || User.Identity.Name.Equals("perkinsadmin") || User.Identity.Name.Equals("uploadadmin") || User.Identity.Name.Equals("administrator"))) { return RedirectToAction("ResourceUnavailable", "Home"); } List<ViewDataUploadFilesResult> r = new List<ViewDataUploadFilesResult>(); HttpPostedFileBase hpf = null;// = Request.Files[file] as HttpPostedFileBase; String FileName = null; foreach (string file in Request.Files) { hpf = Request.Files[file] as HttpPostedFileBase; if (hpf.ContentLength == 0) continue; //string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "images\\" + Path.GetFileName(hpf.FileName)); string savedFileName = Path.Combine(Path.GetFileName(hpf.FileName)); //hpf.SaveAs(savedFileName);//Replace this with database insertion FileName = Path.GetFileName(hpf.FileName);// hpf.FileName; if (FileName.EndsWith(".zip")) { var zip = new Ionic.Zip.ZipInputStream(hpf.InputStream); Ionic.Zip.ZipEntry zipEntry; while ((zipEntry = zip.GetNextEntry()) != null) { // Read the entire file var data = new byte[zipEntry.UncompressedSize]; zip.Read(data, 0, (int)zipEntry.UncompressedSize); ViewData["Info"] += "<p>" + zipEntry.Info + "File Name: " + zipEntry.FileName + "</p>"; // Create new file //var f = new file(); //f.site_id = m_Site.site_id; //f.data = data; //f.filename = dest_filename; //f.folder = dest_folder; // Save it //jabDB.Save(f); //ObjectToFile(data, "../../../Content/" + zipEntry.FileName); if (zipEntry.FileName.EndsWith(".jpg") || zipEntry.FileName.EndsWith(".gif")) { ViewData["Info"] += "File Upload: " + AddImageFromZip(data, zipEntry.FileName); } else { ViewData["Info"] += "File Upload: " + AddFromZip(data, zipEntry.FileName); } } return View("Upload", r); }//New*/ else if (FileName.EndsWith(".jpg") || FileName.EndsWith(".gif")) { AddImage(hpf, FileName, model); } else { AddSolution(hpf, FileName, model); } /*r.Add(new ViewDataUploadFilesResult() { Name = savedFileName, Length = hpf.ContentLength });*/ } ViewData["FileName"] = FileName; if(User.Identity.Name.Equals("administrator")){} return View("Upload", r); }
//INCOMPLETE public ActionResult Upload(UploadModel model, string returnUrl) { List<ViewDataUploadFilesResult> r = new List<ViewDataUploadFilesResult>(); HttpPostedFileBase hpf = null;// = Request.Files[file] as HttpPostedFileBase; String FileName = null; foreach (string file in Request.Files) { hpf = Request.Files[file] as HttpPostedFileBase; if (hpf.ContentLength == 0) continue; string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "images\\" + Path.GetFileName(hpf.FileName)); hpf.SaveAs(savedFileName);//Replace this with database insertion FileName = hpf.FileName; r.Add(new ViewDataUploadFilesResult() { Name = savedFileName, Length = hpf.ContentLength }); } AnswerApp.Models.AnswerAppDataContext db = new AnswerApp.Models.AnswerAppDataContext(); AnswerApp.Models.Question theQuestion = db.Questions.Single<Question>(d => d.Question_Id == 2);// new AnswerApp.Models.Question(); if (hpf != null) { //Disect the file name for it's file properties String[] properties = new String[7]; properties = FileName.Split(new char[1] { '_' }); //String Textbook_Title = properties[0]; //String Unit_Title = properties[2]; //String Chapter_Title = properties[2]; //String Section_Title = properties[3]; //String Page_Number = properties[4]; //String Question_Number = properties[5]; String Practice_Problem = properties[6].Split(new char[1] { '.' })[0]; if (Practice_Problem.Equals("Practice Problem")) { theQuestion.Practice_Problem = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); } else { theQuestion.Answer = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); } } db.SubmitChanges(); Question retrieved = db.Questions.Single(d => d.Question_Id == theQuestion.Question_Id); ViewBag.RetrievedAnswer = retrieved.ToString(); ViewBag.RetrievedAnswer = retrieved.Question_Id; return View("Upload", r); }
//Allows the user to upload new Answers along with their respective //Practice Problem and the answer to that Practice Problem public ActionResult Upload(UploadModel model, string returnUrl) { List<ViewDataUploadFilesResult> r = new List<ViewDataUploadFilesResult>(); HttpPostedFileBase hpf = null;// = Request.Files[file] as HttpPostedFileBase; String FileName = null; foreach (string file in Request.Files) { hpf = Request.Files[file] as HttpPostedFileBase; if (hpf.ContentLength == 0) continue; //string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "images\\" + Path.GetFileName(hpf.FileName)); string savedFileName = Path.Combine(Path.GetFileName(hpf.FileName)); //hpf.SaveAs(savedFileName);//Replace this with database insertion FileName = Path.GetFileName(hpf.FileName);// hpf.FileName; r.Add(new ViewDataUploadFilesResult() { Name = savedFileName, Length = hpf.ContentLength }); AnswerApp.Models.AnswerAppDataContext db = new AnswerApp.Models.AnswerAppDataContext(); //Disect the file name for it's file properties String[] properties = FileName.Split(new char[1] { '_' }); String Textbook_Title = properties[0]; String Unit_Title = properties[1]; String Chapter_Title = properties[2]; String Section_Title = properties[3]; String Page_Number = properties[4]; String Question_Number = properties[5].Split(new char[1] { '.' })[0];//Truncate ".pdf" from the end of the file name String Practice_Problem = null; if (properties.Length > 6) { Practice_Problem = properties[6]; }//An 7th argument indicates a Practice Problem if (Practice_Problem != null) { Practice_Problem = properties[6].Split(new char[1] { '.' })[0]; }//Truncate ".pdf" from the end of the file name //Search teh database for this Textbook IQueryable<Textbook> RetrievedTextbooks = from theTextbooks in db.Textbooks where theTextbooks.Title.Equals(Textbook_Title) select theTextbooks; Textbook[] TextbookResults = RetrievedTextbooks.ToArray<Textbook>(); if (TextbookResults.Length == 0)//The Textbook does not yet exists { //Create a new Textbook AnswerApp.Models.Textbook theTextbook = new AnswerApp.Models.Textbook(); //Populate the Textbook with the properties extracted from the file name theTextbook.Title = Textbook_Title; db.Textbooks.InsertOnSubmit(theTextbook); db.SubmitChanges(); } //Search teh database for this Unit IQueryable<Unit> RetrievedUnits = from theUnits in db.Units where theUnits.Textbook_Title.Equals(Textbook_Title) && theUnits.Unit_Title.Equals(Unit_Title) select theUnits; Unit[] UnitResults = RetrievedUnits.ToArray<Unit>(); if (UnitResults.Length == 0)//The Unit does not yet exists { //Create a new Unit AnswerApp.Models.Unit theUnit = new AnswerApp.Models.Unit(); //Populate the Unit with the properties extracted from the file name theUnit.Textbook_Title = Textbook_Title; theUnit.Unit_Title = Unit_Title; //Populate the relational Id's based on previous hierarchical entries theUnit.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id; db.Units.InsertOnSubmit(theUnit); db.SubmitChanges(); } //Search the database for this Chapter IQueryable<Chapter> RetrievedChapters = from theChapters in db.Chapters where theChapters.Textbook_Title.Equals(Textbook_Title) && theChapters.Unit_Title.Equals(Unit_Title) && theChapters.Chapter_Title.Equals(Chapter_Title) select theChapters; Chapter[] ChapterResults = RetrievedChapters.ToArray<Chapter>(); if (ChapterResults.Length == 0)//The Chapter does not yet exists { //Create a new Chapter AnswerApp.Models.Chapter theChapter = new AnswerApp.Models.Chapter(); //Populate the Chapter with the properties extracted from the file name theChapter.Textbook_Title = Textbook_Title; theChapter.Unit_Title = Unit_Title; theChapter.Chapter_Title = Chapter_Title; //Populate the relational Id's based on previous hierarchical entries theChapter.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id; theChapter.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id; db.Chapters.InsertOnSubmit(theChapter); db.SubmitChanges(); } //Search teh database for this Section IQueryable<Section> RetrievedSections = from theSections in db.Sections where theSections.Textbook_Title.Equals(Textbook_Title) && theSections.Unit_Title.Equals(Unit_Title) && theSections.Chapter_Title.Equals(Chapter_Title) && theSections.Section_Title.Equals(Section_Title) select theSections; Section[] SectionResults = RetrievedSections.ToArray<Section>(); if (SectionResults.Length == 0)//The Section does not yet exists { //Create a new Section AnswerApp.Models.Section theSection = new AnswerApp.Models.Section(); //Populate the Section with the properties extracted from the file name theSection.Textbook_Title = Textbook_Title; theSection.Unit_Title = Unit_Title; theSection.Chapter_Title = Chapter_Title; theSection.Section_Title = Section_Title; //Populate the relational Id's based on previous hierarchical entries theSection.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id; theSection.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id; theSection.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id; db.Sections.InsertOnSubmit(theSection); db.SubmitChanges(); } //Search teh database for this Page IQueryable<Page> RetrievedPages = from thePages in db.Pages where thePages.Textbook_Title.Equals(Textbook_Title) && thePages.Unit_Title.Equals(Unit_Title) && thePages.Chapter_Title.Equals(Chapter_Title) && thePages.Section_Title.Equals(Section_Title) && thePages.Page_Number.Equals(Page_Number) select thePages; Page[] PageResults = RetrievedPages.ToArray<Page>(); if (PageResults.Length == 0)//The Page does not yet exists { //Create a new Page AnswerApp.Models.Page thePage = new AnswerApp.Models.Page(); //Populate the Page with the properties extracted from the file name thePage.Textbook_Title = Textbook_Title; thePage.Unit_Title = Unit_Title; thePage.Chapter_Title = Chapter_Title; thePage.Section_Title = Section_Title; thePage.Page_Number = Page_Number; //Populate the relational Id's based on previous hierarchical entries thePage.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id; thePage.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id; thePage.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id; thePage.Section_Id = db.Sections.Single(d => d.Section_Title.Equals(Section_Title)).Section_Id; db.Pages.InsertOnSubmit(thePage); } //Search teh database for this Question IQueryable<Question> retrieved = from theAnswers in db.Questions where theAnswers.Textbook_Title.Equals(Textbook_Title) && theAnswers.Unit_Title.Equals(Unit_Title) && theAnswers.Chapter_Title.Equals(Chapter_Title) && theAnswers.Section_Title.Equals(Section_Title) && theAnswers.Page_Number.Equals(Page_Number) && theAnswers.Question_Number.Equals(Question_Number) select theAnswers; Question[] results = retrieved.ToArray<Question>(); if (results.Length != 0)//The Answer already exists { //Use the existing Question AnswerApp.Models.Question theQuestion = results.First(); if (Practice_Problem != null)//This is a Practice Problem { theQuestion.Practice_Problem = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer; } else//(Practice_Problem == null) This is an Answer { theQuestion.Answer = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer; } } else//(results.Length == 0) This is a new Answer { //Create a new Question AnswerApp.Models.Question theQuestion = new AnswerApp.Models.Question(); //Populate the Question with the properties extracted from the file name theQuestion.Textbook_Title = Textbook_Title; theQuestion.Unit_Title = Unit_Title; theQuestion.Chapter_Title = Chapter_Title; theQuestion.Section_Title = Section_Title; theQuestion.Page_Number = Page_Number; theQuestion.Question_Number = Question_Number; //Populate the relational Id's based on previous hierarchical entries theQuestion.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id; theQuestion.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id; theQuestion.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id; theQuestion.Section_Id = db.Sections.Single(d => d.Section_Title.Equals(Section_Title)).Section_Id; theQuestion.Page_Id = db.Pages.Single(d => d.Page_Number.Equals(Page_Number)).Page_Id; if (Practice_Problem != null)//This is a Practice Problem { theQuestion.Practice_Problem = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer; } else//(Practice_Problem == null) This is an Answer { theQuestion.Answer = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer; } //Insert the new Question into the database db.Questions.InsertOnSubmit(theQuestion); } db.SubmitChanges();//Commit the changes to the database. } if(User.Identity.Name.Equals("administrator")){} return View("Upload", r); }
//INCOMPLETE public ActionResult Upload(UploadModel model, string returnUrl) { List<ViewDataUploadFilesResult> r = new List<ViewDataUploadFilesResult>(); HttpPostedFileBase hpf = null;// = Request.Files[file] as HttpPostedFileBase; foreach (string file in Request.Files) { hpf = Request.Files[file] as HttpPostedFileBase; if (hpf.ContentLength == 0) continue; string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "images\\" + Path.GetFileName(hpf.FileName)); hpf.SaveAs(savedFileName);//Replace this with database insertion r.Add(new ViewDataUploadFilesResult() { Name = savedFileName, Length = hpf.ContentLength }); } ViewData["FileName"] = "This is a file name."; AnswerApp.Models.AnswerAppDataContext db = new AnswerApp.Models.AnswerAppDataContext(); AnswerApp.Models.Question theQuestion = new AnswerApp.Models.Question(); //db.Questions.Single(d => d.Question_Number.Equals("8"));// if (hpf != null) { theQuestion.Answer = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length); //db.Questions.InsertOnSubmit(theQuestion); } db.SubmitChanges(); Question retrieved = db.Questions.Single(d => d.Question_Id == theQuestion.Question_Id); ViewBag.RetrievedAnswer = retrieved.ToString(); ViewBag.RetrievedAnswer = retrieved.Question_Id; return View("Upload", r); }