private void FuncToCall6(object context) { if (CreatedMainTopicTitle == null || CreatedSubTopicTitle == "") { MessageBox.Show("You must enter a valid name in order to delete a main topic."); } else { using (SelfAssessmentDbContext db = new SelfAssessmentDbContext()) { List <SubTopic> subTopics = db.SubTopics .Include(s => s.MainTopic) .Where(m => m.MainTopic.Title == SelectedMainTopic.Title) .ToList(); foreach (SubTopic subTopic in subTopics) { db.SubTopics.Remove(subTopic); } MainTopic mainTopic = db.MainTopics.Where(m => m.Title == SelectedMainTopic.Title).FirstOrDefault(); db.Set <MainTopic>().Remove(mainTopic); db.SaveChanges(); } SelectedMainTopic = null; MainTopics = Context.MainTopics.ToList(); CreatedMainTopicTitle = null; SubTopics = null; } }
private void FuncToCall4(object context) { if (CreatedMainTopicTitle == null || CreatedSubTopicTitle == "") { MessageBox.Show("You must enter a name in order to create a main topic."); } else { using (SelfAssessmentDbContext db = new SelfAssessmentDbContext()) { MainTopic checkMainTopic = db.MainTopics.Where(m => m.Title == CreatedMainTopicTitle).FirstOrDefault(); if (checkMainTopic == null) { db.MainTopics.Add(new MainTopic() { Title = CreatedMainTopicTitle }); db.SaveChanges(); } else { MessageBox.Show("This main topic already exists!"); } } MainTopics = Context.MainTopics.ToList(); CreatedMainTopicTitle = null; } }
private void FuncToCall5(object context) { if (CreatedMainTopicTitle == null || CreatedSubTopicTitle == "") { MessageBox.Show("You must enter a valid name in order to update a main topic."); } else { using (SelfAssessmentDbContext db = new SelfAssessmentDbContext()) { MainTopic mainTopic = db.MainTopics.Where(m => m.Title == SelectedMainTopic.Title).FirstOrDefault(); mainTopic.Title = CreatedMainTopicTitle; db.Set <MainTopic>().Update(mainTopic); db.SaveChanges(); } MainTopics = Context.MainTopics.ToList(); CreatedMainTopicTitle = null; } }
public async Task <SubTopic> CreateNewSubTopic(string mainTopicTitle, string subTopicTitle, string subTopicIntro, string subTopicContent, string subTopicSummary) { using (SelfAssessmentDbContext context = new SelfAssessmentDbContext()) { MainTopic mainTopic = context.MainTopics.Where(m => m.Title == mainTopicTitle).FirstOrDefault(); SubTopic newSubTopic = new SubTopic() { Title = subTopicTitle, Introduction = subTopicIntro, Content = subTopicContent, Summary = subTopicSummary, MainTopic = mainTopic }; EntityEntry <SubTopic> createdResult = await context.Set <SubTopic>().AddAsync(newSubTopic); await context.SaveChangesAsync(); return(createdResult.Entity); } }
public void Setup() { User user = new User() { Username = "******", PasswordHashed = "test2", Email = "*****@*****.**", DateJoined = DateTime.Now }; Account account = new Account() { User = user, }; TestSeries testSeries = new TestSeries() { TestSeriesName = "TestTestSeries", }; Test test = new Test() { TestSeries = testSeries, TestName = "TestName" }; Test secondTest = new Test() { TestSeries = testSeries, TestName = "TestName2" }; TestResult testResult = new TestResult() { Account = account, Mark = 30, Test = test }; MainTopic mainTopic = new MainTopic() { Title = "MainTopic" }; SubTopic subTopic = new SubTopic() { MainTopic = mainTopic, Title = "SubTopic" }; using (SelfAssessmentDbContext context = new SelfAssessmentDbContext()) { context.Accounts.Add(account); context.TestSeries.Add(testSeries); context.MainTopics.Add(mainTopic); context.SubTopics.Add(subTopic); context.Tests.Add(test); context.Tests.Add(secondTest); context.TestResults.Add(testResult); context.SaveChanges(); } }