public static TaskTimeSpan ToDBObject(this TaskTimeSpanService timeSpan) { return(new TaskTimeSpan { Id = timeSpan.Id, Start = timeSpan.Start, End = timeSpan.End, TaskId = timeSpan.TaskId }); }
/// <summary> Updates the time span in the database with the specified properties. </summary> public void UpdateTimeSpan(TaskTimeSpanService messageObject) { using (var context = new StudyTasksContext()) { var dbElement = context.TimeSpans.FirstOrDefault(timeSpanDB => timeSpanDB.Id == messageObject.Id); Contract.Assert(dbElement != null, "Cannot update an element that does not exist"); dbElement.TaskId = messageObject.TaskId; dbElement.Start = messageObject.Start; dbElement.End = messageObject.End; context.SaveChanges(); } }
/// <summary> /// Add a timespan to the database /// </summary> /// <param name="timeSpan">The timespan</param> /// <returns>The id of the timespan in the database</returns> public int AddTimeSpanTo(TaskTimeSpanService timeSpan) { if (timeSpan == null) { throw new ArgumentNullException(nameof(timeSpan)); } if (timeSpan.Start == new DateTime()) { throw new ArgumentException("timespan has the default DateTime instead of an assigned one"); } if (timeSpan.Id != 0) { throw new ArgumentException("timespan has an ID assigned but is ignored"); } if (timeSpan.TaskId == 0) { throw new ArgumentException("timespan has no assigned TaskId"); } int timeSpanId = timeSpan.Id; using (var context = new StudyTasksContext()) { var timeSpanDB = context.TimeSpans.FirstOrDefault(_ => _.Id == timeSpan.Id); if (timeSpanDB != null) { context.TimeSpans.Remove(timeSpanDB); } else { timeSpanDB = timeSpan.ToDBObject(); } var createdTimeSpan = context.TimeSpans.Add(timeSpanDB); context.SaveChanges(); timeSpanId = createdTimeSpan.Id; } return(timeSpanId); }