/*================================================= * Creates a default lab view model with the * following properties: * DateTimeCreated is set to time of object's creation as expected. * DateTimePublished is null * IsOverridden and IsPublished are false * Name, LabID, and Lab's title are each labKey * Default Lab has one title-less, content-less exercise * ==============================================*/ private ViewModels.LabViewModel DefaultLabViewModel() { string now = DateTime.Now.ToString(); ViewModels.LabViewModel model; string key = GenerateID(labKey); Models.Lab lab = new Models.Lab() { Title = labKey, Intro = "", DueDate = null, ExerciseList = new List <Models.Exercise>() { new Models.Exercise() { ExerciseTitle = "Test Exercise", Content = "Lorem Ipsum" } } }; model = new ViewModels.LabViewModel(lab); model.DateTimeCreated = now; model.Name = labKey; model.LabID = key; model.IsOverridden = false; model.IsPublished = false; return(model); }
/*========================================== * Attempt to import the lab view model. If * succesful, function will return will return true and model * is set. Otherwise, return false and model will be null. * (Intent is not to use model if method returns false). * =======================================*/ private ViewModels.LabViewModel TryImportLabViewModel() { ViewModels.LabViewModel model = null; try { string sql = "USE GEOL100LABS; SELECT * FROM Labs WHERE Lab_ID = @key"; using (var connection = ConnectToServer()) { using (var cmd = new MySqlCommand(sql, connection)) { cmd.Parameters.AddWithValue("@key", labKey); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { string myLab = reader["Content"].ToString(); Models.Lab lab = DeserializeLab(myLab); model = new ViewModels.LabViewModel(lab); model.Name = reader["Lab_Name"].ToString(); model.LabID = reader["Lab_ID"].ToString(); model.DateTimeCreated = reader["Date_Time_Created"].ToString(); bool b; if (bool.TryParse(reader["Is_Published"].ToString(), out b)) { model.IsPublished = b; if (b) { model.DateTimePublished = reader["Date_Time_Published"].ToString(); } } } } } } } catch (Exception) { model = DefaultLabViewModel(); } if (model == null) { model = DefaultLabViewModel(); } return(model); }