/*================================================= * 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); }
/* ==============================================================================* * Prints the recieved lab to the console * not currently used but may be useful * ==============================================================================*/ private void PrintLab(Models.Lab lab) { Debug.WriteLine(lab.Title); foreach (Models.Exercise e in lab.ExerciseList) { Debug.WriteLine(e.ExerciseTitle); Debug.WriteLine(e.Content); } }
/*============================================== * Given a XML-encoded Lab string, deserialize it into a Lab object * ===========================================*/ private Models.Lab DeserializeLab(string content) { Models.Lab lab = null; XmlSerializer serializer = new XmlSerializer(typeof(Models.Lab)); using (StringReader sReader = new StringReader(content)) { lab = (Models.Lab)serializer.Deserialize(sReader); } return(lab); }
private Lab GetLab(Models.Lab lab) { var mystatus = (LabStatus)lab.LabStatus; return(new Lab { Dimensions = GetDimensions(lab.DimensionId), LabStatus = mystatus.ToString(), Weight = lab.Weight, LabId = lab.LabId }); }
/*========================================== * 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); }
private List <object> LabList() { string sql = "USE GEOL100LABS; SELECT * FROM Labs;"; List <object> labs = new List <object>(); using (MySqlConnection connection = ConnectToServer()) { using (MySqlCommand cmd = new MySqlCommand(sql, connection)) { MySqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Models.Lab lab = DeserializeLab(reader["Content"].ToString()); string due = (lab.DueDate != null) ? lab.DueDate.ToString() : null; bool isPublished = false; bool.TryParse(reader["Is_Published"].ToString(), out isPublished); DateTime?dtCreated = null, dtPublished = null; DateTime temp; if (DateTime.TryParse(reader["Date_Time_Created"].ToString(), out temp)) { dtCreated = temp; } if (DateTime.TryParse(reader["Date_Time_Published"].ToString(), out temp)) { dtPublished = temp; } labs.Add(new { LabName = reader["Lab_Name"].ToString(), LabID = reader["Lab_ID"].ToString(), IsPublished = isPublished, DateTimeCreated = dtCreated, DateTimePublished = dtPublished, DueDate = due }); } } } return(labs); }
public ActionResult Save(string title, string intro, string[] exerciseTitles, string[] exerciseContent, string[] exerciseResponses, string name, string key, string created, DateTime?due, bool isPublished, DateTime?publishDate) { labKey = key; DateTime dtCreated = DateTime.Parse(created); Models.Lab lab = new Models.Lab(); lab.Title = title; lab.Intro = intro; /* Lab DueDate will be set from the Lab Manager once it is created * I left this here as an example of the expected format */ lab.DueDate = due; object pubObj = publishDate; // handle evaluating publish date if (pubObj == null) { pubObj = DBNull.Value; } lab.ExerciseList = new List <Models.Exercise>(); List <Exercise> list = new List <Exercise>(); int children = 0; for (int i = 0; i < exerciseTitles.Length; i++) { Models.Exercise e = new Models.Exercise() { ExerciseTitle = exerciseTitles[i], Content = exerciseContent[i], ExerciseList = new List <Exercise>(), Response = "" }; e.ExerciseList = new List <Exercise>(); if (!int.TryParse(exerciseResponses[i], out children)) { e.Response = exerciseResponses[i]; // This feature has yet to be implemented list.Add(e); } else { for (int j = 0; j < children; j++) { i++; Exercise ex = new Exercise() { ExerciseTitle = exerciseTitles[i], Content = exerciseContent[i], Response = exerciseResponses[i] }; e.ExerciseList.Add(ex); list.Add(ex); } } lab.ExerciseList.Add(e); } /*using (var connection = ConnectToServer()) * { * foreach (Exercise e in list) * { * string insert = "USE GEOL100LABS; INSERT IGNORE INTO Exercises(Lab_ID, Exercise_ID, Exercise_Title) VALUES (@lab, @exercise, @title)"; * using (var cmd = new MySqlCommand(insert, connection)) * { * cmd.Parameters.AddWithValue("@lab", labKey); * cmd.Parameters.AddWithValue("@exercise", e.ExerciseID); * cmd.Parameters.AddWithValue("@title", e.ExerciseTitle); * cmd.ExecuteNonQuery(); * } * } * }*/ /* Lab is deleted due to overwritting problems * Another way to solve this problem should eventually be researched * to lighten the load of server calls */ DeleteLab(); string sql = "USE GEOL100LABS; INSERT INTO Labs(Lab_Name, Lab_ID, Content, Is_Overriden, Date_Time_Created, Is_Published, Date_Time_Published) VALUES (@name, @id, @content, @overridden, @created, @ispublished, @publish);"; string myLab = SerializeLab(lab); // These 'using' statments are the best practice for iDisposable objects using (MySqlConnection connection = ConnectToServer()) { using (MySqlCommand cmd = new MySqlCommand(sql, connection)) { // This is the parameterized query style I choose cmd.Parameters.AddWithValue("@name", name); cmd.Parameters.AddWithValue("@id", labKey); cmd.Parameters.AddWithValue("@content", myLab); cmd.Parameters.AddWithValue("@overridden", isOverriden); cmd.Parameters.AddWithValue("@created", dtCreated); cmd.Parameters.AddWithValue("@ispublished", isPublished ? "True" : "False"); cmd.Parameters.AddWithValue("@publish", pubObj); cmd.ExecuteNonQuery(); } foreach (Exercise e in list) { string insert = "USE GEOL100LABS; INSERT IGNORE INTO Exercises(Lab_ID, Exercise_ID, Exercise_Title) VALUES (@lab, @exercise, @title)"; using (var cmd = new MySqlCommand(insert, connection)) { cmd.Parameters.AddWithValue("@lab", labKey); cmd.Parameters.AddWithValue("@exercise", e.ExerciseID); cmd.Parameters.AddWithValue("@title", e.ExerciseTitle); cmd.ExecuteNonQuery(); } } } return(Json("true")); // the content of this return statement is currently irrelevant and can be changed to any string that you want }