public void SaveTimecard(tm.Timecard timecard) { Timecard data; if (timecard.ID != 0) { data = _context.Timecards.Find(timecard.ID); if (data == null) { throw new RecordNotFoundException(); } data.UpdateFromCore(timecard); _context.SaveChanges(); } else { data = timecard.ToData(); _context.Timecards.Add(data); _context.SaveChanges(); } timecard.UpdateFromData(data); if (timecard.Activities.Any()) { SaveActivities(timecard); } }
public tm.Timecard GetNearestTimecard(DateTime date, bool after) { tm.Timecard timecard = null; IOrderedQueryable <Timecard> query; if (after) { query = _context.Timecards .Where(t => t.Date > date) .OrderBy(t => t.Date) .ThenBy(t => t.ID); } else { query = _context.Timecards .Where(t => t.Date < date) .OrderByDescending(t => t.Date) .ThenByDescending(t => t.ID); } var data = query .FirstOrDefault(); if (data != null) { timecard = data.ToCore(); } if (timecard != null) { GetActivities(timecard); } return(timecard); }
public static Timecard ToData(this tm.Timecard core) { return(new Timecard { ID = core.ID, Date = core.Date, }); }
public static tm.Timecard ToCore(this Timecard data) { var core = new tm.Timecard(data.ID, data.Date); if (data.Activities != null) { core.Activities.AddRange(data.Activities.Select(a => a.ToCore())); } return(core); }
public void GetActivities(tm.Timecard timecard) { var activities = _context.Activities .Where(a => a.TimecardID == timecard.ID) .OrderBy(a => a.StartMinute) .ToList() .Select(a => a.ToCore()) .ToList(); timecard.Activities.Clear(); timecard.Activities.AddRange(activities); }
public void SaveActivities(tm.Timecard timecard) { var oldActivities = _context.Activities .Where(a => a.TimecardID == timecard.ID) .Select(a => a); _context.Activities.RemoveRange(oldActivities); _context.SaveChanges(); foreach (var activity in timecard.Activities) { var data = activity.ToData(); data.ID = 0; data.TimecardID = timecard.ID; _context.Activities.Add(data); } _context.SaveChanges(); GetActivities(timecard); }
public void PropertiesAndContainmentTest() { tc.Configuration.CurrentDateTime = new DateTime(2019, 11, 1, 8, 0, 0); var timecard = new tm.Timecard(); timecard.Date = tc.Configuration.CurrentDateTime.Date; Assert.AreEqual(tc.Configuration.CurrentDateTime.Date, timecard.Date); tc.Configuration.CurrentDateTime = new DateTime(2019, 11, 1, 8, 5, 0); timecard.Activities.Add(new tm.Activity("00000", "First activity at 8:05am")); tc.Configuration.CurrentDateTime = new DateTime(2019, 11, 1, 17, 0, 0); timecard.Activities.Add(new tm.Activity("00001", "Second activity at 5pm")); tc.Configuration.CurrentDateTime = new DateTime(2019, 11, 2, 1, 30, 0); timecard.Activities.Add(new tm.Activity("00002", "Third activity at 1:30am the next day")); Assert.AreEqual(3, timecard.Activities.Count); Assert.IsTrue(!timecard.Activities[0].IsAfterMidnight, "Morning same day has 'after midnight' flag set"); Assert.IsTrue(!timecard.Activities[1].IsAfterMidnight, "Afternoon same day has 'after midnight' flag set"); Assert.IsTrue(timecard.Activities[2].IsAfterMidnight, "Morning next day does not have 'after midnight' flag set"); tc.Configuration.CurrentDateTime = new DateTime(2019, 11, 1, 11, 15, 0); timecard.Activities.Insert(1, new tm.Activity("00042", "After first activity at 11:15am", "11:15")); Assert.AreEqual(4, timecard.Activities.Count); Assert.AreEqual("00042", timecard.Activities[1].Code, "Inserted activity not in expected place"); Assert.IsTrue(!timecard.Activities[1].IsAfterMidnight, "Later morning has 'after midnight' flag set"); tc.Configuration.CurrentDateTime = new DateTime(2019, 11, 2, 2, 52, 0); timecard.Activities.Add(new tm.Activity("00084", "Next day activity with user-provided time", "2:45")); Assert.AreEqual(5, timecard.Activities.Count); Assert.AreEqual("00084", timecard.Activities[4].Code, "Appended activity not in expected place"); Assert.AreEqual(1605, timecard.Activities[4].StartMinute, "User-provided time for next day has incorrect start minute"); Assert.IsTrue(timecard.Activities[4].IsAfterMidnight, "User-provided time for next day does not have 'after midnight' flag set"); timecard.Activities.RemoveAll(a => true); Assert.AreEqual(0, timecard.Activities.Count); }
public void CoreDataTest() { tm.Timecard tcIn; tm.Timecard tcOut; // use it to get a reference to the repo like core would do ti.IRepository repoIn = _factory.Resolve <ti.IRepository>(); // make a core timecard object tcIn = new tm.Timecard { Date = new DateTime(2019, 6, 1) }; Assert.AreEqual(tcIn.ID, 0, "Timecard did not start with ID 0 as expected"); tcIn.Activities.AddRange(new[] { new tm.Activity("00000", "Arrived; made coffee", "08:00"), new tm.Activity("00100", "Called a client", "08:15"), new tm.Activity("00200", "Worked on a project", "09:00"), new tm.Activity("", "Lunch", "12:00"), new tm.Activity("00200", "More work", "13:00"), new tm.Activity("", "Departed", "17:00"), }); // save it repoIn.SaveTimecard(tcIn); Assert.IsTrue(tcIn.ID > 0, "Didn't get a valid ID after saving timecard"); // get another reference to the repo ti.IRepository repoOut = _factory.Resolve <ti.IRepository>(); // retrieve the saved timecard tcOut = repoOut.GetTimecard(tcIn.ID); Assert.AreEqual(tcIn.ID, tcOut.ID, "Retrieved timecard ID doesn't match saved timecard ID"); Assert.AreEqual(tcOut.Activities.Count, 6, "Didn't receive expected number of activity children"); // cleanup at end of test repoOut.DeleteAllTimecards(); }
private (int TimecardCount, int ActivityCount) MakeTimecardsForEachDate(DateTime[] dates) { var repo = _factory.Resolve <ti.IRepository>(); var count = 0; foreach (var date in dates) { var tc = new tm.Timecard() { Date = date }; tc.Activities.AddRange(new[] { new tm.Activity("00000", "Arrived", "08:00"), new tm.Activity("", "Departed", "17:00"), }); repo.SaveTimecard(tc); count++; } return(count, 2); }
public static void UpdateFromData(this tm.Timecard core, Timecard data) { core.ID = data.ID; core.Date = data.Date; }
public static void UpdateFromCore(this Timecard data, tm.Timecard core) { data.ID = core.ID; data.Date = core.Date; }
public ActivityList(Timecard timecard) : base() { _timecard = timecard; }
public void LoadSaveReportTest() { using (var repo = new td.Repository(new Base.TestAppConstants())) { DateTime FirstDate = new DateTime(2018, 9, 10); DateTime SecondDate = new DateTime(2018, 9, 11); // // test saving, retrieving, and updating a timecard // var savedTimecard = new tm.Timecard(); Assert.AreEqual(0, savedTimecard.ID, "Unsaved timecard should have ID = 0"); repo.SaveTimecard(savedTimecard); Assert.AreNotEqual(0, savedTimecard.ID, "Saved timecard should not have ID = 0"); var retrievedTimecard = repo.GetTimecard(savedTimecard.ID); Assert.AreEqual(savedTimecard.ID, retrievedTimecard.ID, "Retreived timecard IDs don't match"); Assert.AreEqual(savedTimecard.Date, retrievedTimecard.Date, "Retreived timecard dates don't match"); savedTimecard.Date = FirstDate; repo.SaveTimecard(savedTimecard); var nonexistentTimecard = repo.GetTimecard(987654321); Assert.IsTrue(nonexistentTimecard == null, "Somehow retrieved timecard that doesn't exist"); // // test saving and retrieving set of activities // savedTimecard.Activities.AddRange(new List <tm.Activity> { new tm.Activity("00000", "Got to work", "08:00"), new tm.Activity("00100", "Did this", "09:00"), new tm.Activity("00200", "Did that", "10:00"), new tm.Activity("", "Lunch break", "12:00"), new tm.Activity("00200", "Did more of that", "13:00"), new tm.Activity("00300", "Did something else", "14:00"), new tm.Activity("", "Went home", "17:00"), }); Assert.IsTrue(savedTimecard.Activities.Exists(a => a.ID == 0), "Unsaved activities should have ID = 0"); repo.SaveActivities(savedTimecard); Assert.IsFalse(savedTimecard.Activities.Exists(a => a.ID == 0), "Saved activities should not have ID = 0"); // this next test expects the new activities to be saved in the order they exist in the list repo.GetActivities(retrievedTimecard); Assert.AreEqual(savedTimecard.Activities.Count, retrievedTimecard.Activities.Count, "The retrieved timecard does not have the same number of activities it started with"); for (var i = 0; i < retrievedTimecard.Activities.Count; ++i) { Assert.AreEqual(savedTimecard.Activities[i].ID, retrievedTimecard.Activities[i].ID, "The retrieved list of activities has an unexpected ID value"); Assert.AreEqual(savedTimecard.Activities[i].Code, retrievedTimecard.Activities[i].Code, "The retrieved list of activities has an unexpected Code value"); Assert.AreEqual(savedTimecard.Activities[i].Description, retrievedTimecard.Activities[i].Description, "The retrieved list of activities has an unexpected Description value"); Assert.AreEqual(savedTimecard.Activities[i].Time, retrievedTimecard.Activities[i].Time, "The retrieved list of activities has an unexpected Time value"); Assert.AreEqual(savedTimecard.Activities[i].StartMinute, retrievedTimecard.Activities[i].StartMinute, "The retrieved list of activities has an unexpected StartMinute value"); } // // test saving, retrieving, and updating an activity // var savedActivity = new tm.Activity("00400", "One more thing", "15:00") { TimecardID = savedTimecard.ID }; Assert.AreEqual(0, savedActivity.ID, "Unsaved activity should have ID = 0"); repo.SaveActivity(savedActivity); Assert.AreNotEqual(0, savedActivity.ID, "Saved activity should not have ID = 0"); var retrievedActivity = repo.GetActivity(savedActivity.ID); Assert.AreEqual(savedActivity.ID, retrievedActivity.ID, "Retrieved activity has different ID"); Assert.AreEqual(savedActivity.Code, retrievedActivity.Code, "Retrieved activity has different Code"); Assert.AreEqual(savedActivity.Description, retrievedActivity.Description, "Retrieved activity has different Description"); Assert.AreEqual(savedActivity.Time, retrievedActivity.Time, "Retrieved activity has different Time"); Assert.AreEqual(savedActivity.StartMinute, retrievedActivity.StartMinute, "Retrieved activity has different StartMinute"); savedActivity.Description = "Worked on one more thing"; savedActivity.Time = "16:00"; repo.SaveActivity(savedActivity); retrievedActivity = repo.GetActivity(savedActivity.ID); Assert.AreEqual(savedActivity.ID, retrievedActivity.ID, "Updated activity does not have the same ID"); Assert.AreEqual(savedActivity.Code, retrievedActivity.Code, "Updated activity does not have the same Code"); Assert.AreEqual(savedActivity.Description, retrievedActivity.Description, "Updated activity does not have the same Description"); Assert.AreEqual(savedActivity.Time, retrievedActivity.Time, "Updated activity does not have the same Time"); Assert.AreEqual(savedActivity.StartMinute, retrievedActivity.StartMinute, "Updated activity does not have the same StartMinute"); var nonexistentActivity = repo.GetActivity(987654321); Assert.IsTrue(nonexistentActivity == null, "Somehow retrieved activity that doesn't exist"); // // test reporting on activity by code // var anotherTimecard = new tm.Timecard() { Date = SecondDate }; anotherTimecard.Activities.AddRange(new List <tm.Activity> { new tm.Activity("00000", "Got to work", "09:00"), new tm.Activity("00200", "Half day on that", "9:00"), new tm.Activity("", "Went home", "12:00"), }); repo.SaveTimecard(anotherTimecard); var report = repo.GetReport(FirstDate, SecondDate); Assert.AreEqual(5, report.Count, $"Was expecting 5 codes in report, but got {report.Count}"); Assert.AreEqual("00000", report[0].Code, $"Unexpected code {report[0].Code} in first report item"); Assert.AreEqual("00100", report[1].Code, $"Unexpected code {report[0].Code} in second report item"); Assert.AreEqual("00200", report[2].Code, $"Unexpected code {report[0].Code} in third report item"); Assert.AreEqual("00300", report[3].Code, $"Unexpected code {report[0].Code} in fourth report item"); Assert.AreEqual("00400", report[4].Code, $"Unexpected code {report[0].Code} in fifth report item"); Assert.AreEqual(FirstDate, report[0].EarliestDate, "Earliest date is wrong in first report item"); Assert.AreEqual(FirstDate, report[1].EarliestDate, "Earliest date is wrong in second report item"); Assert.AreEqual(FirstDate, report[2].EarliestDate, "Earliest date is wrong in third report item"); Assert.AreEqual(FirstDate, report[3].EarliestDate, "Earliest date is wrong in fourth report item"); Assert.AreEqual(FirstDate, report[4].EarliestDate, "Earliest date is wrong in fifth report item"); Assert.AreEqual(SecondDate, report[0].LatestDate, "Latest date is wrong in first report item"); Assert.AreEqual(FirstDate, report[1].LatestDate, "Latest date is wrong in second report item"); Assert.AreEqual(SecondDate, report[2].LatestDate, "Latest date is wrong in third report item"); Assert.AreEqual(FirstDate, report[3].LatestDate, "Latest date is wrong in fourth report item"); Assert.AreEqual(FirstDate, report[4].LatestDate, "Latest date is wrong in fifth report item"); Assert.AreEqual(60, report[0].TotalMinutes, $"Unexpected {report[0].TotalMinutes} TotalMinutes in first report item"); Assert.AreEqual(60, report[1].TotalMinutes, $"Unexpected {report[1].TotalMinutes} TotalMinutes in second report item"); Assert.AreEqual(360, report[2].TotalMinutes, $"Unexpected {report[2].TotalMinutes} TotalMinutes in third report item"); Assert.AreEqual(120, report[3].TotalMinutes, $"Unexpected {report[3].TotalMinutes} TotalMinutes in fourth report item"); Assert.AreEqual(60, report[4].TotalMinutes, $"Unexpected {report[4].TotalMinutes} TotalMinutes in fifth report item"); // // test timecard list and deletes // var timecardList = repo.GetTimecards(0, 10, true); Assert.AreEqual(2, timecardList.Count, "Did not retrieve list of two timecards"); Assert.IsTrue(timecardList[0].Date > timecardList[1].Date, "List of timecards did not retrieve in correct order"); timecardList = repo.GetTimecards(0, 10, false); Assert.AreEqual(2, timecardList.Count, "Did not retrieve list of two timecards"); Assert.IsTrue(timecardList[0].Date < timecardList[1].Date, "List of timecards did not retrieve in correct order"); retrievedTimecard = repo.GetTimecard(anotherTimecard.ID); var activityCountBefore = retrievedTimecard.Activities.Count; Assert.AreNotEqual(0, activityCountBefore, "Retrieving timecard did not retrieve activities"); repo.DeleteActivity(retrievedTimecard.Activities[0].ID); retrievedTimecard = repo.GetTimecard(retrievedTimecard.ID); var activityCountAfter = retrievedTimecard.Activities.Count; Assert.AreEqual(1, activityCountBefore - activityCountAfter, "Did not see a difference of 1 in tally of activities after delete"); repo.DeleteTimecard(timecardList[0].ID); timecardList = repo.GetTimecards(0, 10, false); Assert.AreEqual(1, timecardList.Count, "Did not get expected number of timecards after a deletion"); var numberOfTimecards = repo.GetTimecardCount(); Assert.AreEqual(timecardList.Count, numberOfTimecards, "Number of timecards by list is not the same as repo's count function"); repo.DeleteAllTimecards(); numberOfTimecards = repo.GetTimecardCount(); Assert.AreEqual(0, numberOfTimecards, "Timcard count not zero after deleting all timecards"); } }