public List <Question> GetQuestions(int id) { List <Question> questions = new List <Question>(); try { SavedTest savedTest = context.SavedTests.Find(id); List <int> questionIds = new List <int>(); for (int i = 0; i < savedTest.Questions.Split("|").Length; i++) { if (savedTest.Questions.Split("|")[i] != "") { questionIds.Add(int.Parse(savedTest.Questions.Split("|")[i])); } } foreach (int item in questionIds) { questions.Add(context.Questions.Where(x => x.Id == item).SingleOrDefault()); } return(questions); } catch (Exception) { return(questions); } }
public string SaveTest(SaveTestModel savedTest, string profId) { string str = ""; try { for (int i = 0; i < savedTest.Ids.Length; i++) { str = string.Concat(str, savedTest.Ids[i].ToString(), "|"); } SavedTest test = new SavedTest() { Questions = str, Group = savedTest.Group, Name = savedTest.TestName, Date = DateTime.Now, Grade = (from x in context.Questions where x.Id == savedTest.Ids[0] select x.Grade).FirstOrDefault(), ProfessorId = profId, SubjectId = savedTest.SubjectId }; context.SavedTests.Add(test); context.SaveChanges(); return("Successfully saved test"); } catch (Exception ex) { return(ex.Message); } }
public string DeleteTest(int id) { try { //context.SavedTests.FromSql($"Delete from SavedTests where Id = {id}"); SavedTest savedTest = context.SavedTests.Find(id); context.SavedTests.Remove(savedTest); context.SaveChanges(); return("Test deleted"); } catch (Exception ex) { return(ex.Message); } }