/// <summary> /// Searches the table Errands for an errand with the input id, if it exists it gets deleted. /// </summary> /// <param name="id"> id to match with ErrandId. </param> /// <returns> True if correctly deleted, else false. </returns> public bool DeleteErrand(int id) { Errand dbEntry = context.Errands.FirstOrDefault(s => s.ErrandId == id); if (dbEntry != null) { context.Errands.Remove(dbEntry); context.SaveChanges(); return(true); } else { return(false); } }
/// <summary> /// Saves a new errand to the database, or updates a current one, depending on if the errand /// already has an "ErrandId". /// </summary> /// <param name="errand"> Errand to be saved or updated. </param> public void SaveErrand(Errand errand) { if (errand.ErrandId == 0) { errand.RefNumber = "2018-45-" + GetCurrentRefNum(); errand.StatusId = "S_A"; context.Errands.Add(errand); UpdateSequence(); } else { Errand dbEntry = context.Errands.FirstOrDefault(s => s.ErrandId == errand.ErrandId); if (dbEntry != null) { context.Errands.Update(dbEntry = errand); } } context.SaveChanges(); }