From 69f3e502e031238c91e1a73cbdc801c564f190f1 Mon Sep 17 00:00:00 2001 From: "Chris.Watts90@outlook.com" Date: Fri, 3 Mar 2017 13:00:01 +0000 Subject: [PATCH] Add LastEventDateTime property to User object Add Code to populate the LastEventDateTime property in SQLiteRepository - getting the last TimeLogEvent datetime. will return min value if no logs available. #53 --- .../WindowsDataCenter/Interfaces/User.cs | 5 +-- .../SQLiteRepository/SQLiteRepository.cs | 32 ++++++++++++++----- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/DataCenter_Windows/WindowsDataCenter/Interfaces/User.cs b/DataCenter_Windows/WindowsDataCenter/Interfaces/User.cs index b9e3214..406824e 100644 --- a/DataCenter_Windows/WindowsDataCenter/Interfaces/User.cs +++ b/DataCenter_Windows/WindowsDataCenter/Interfaces/User.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace Interfaces { @@ -19,7 +20,7 @@ namespace Interfaces { get { return AssociatedIdentifiers.Count; } } - + public DateTime LastEventDateTime { get; set; } public List AssociatedIdentifiers { get; set; } public bool State { get; set; } diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs index d39734c..5840581 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs @@ -98,6 +98,14 @@ namespace SQLiteRepository return ret; } + private DateTime GetLastLogDateTime(TimeLogDb timeLog) + { + if (timeLog != null) + { + return timeLog.SwipeEventDateTime.DateTime; + } + return DateTime.MinValue; + } private bool GetUserState(LogDirectionDb logDirection) { switch (logDirection) @@ -503,23 +511,20 @@ namespace SQLiteRepository var logDirection = LogDirectionDb.UNKNOWN; if (userId != -1) { - var lastEntry = _connection.Query( - SQLiteProcedures.GET_LAST_TIMELOG_DIRECTION, - userId); - if (lastEntry.Any()) + var lastEntry = GetLastTimeLog(userId); + if (lastEntry != null) { - var lastLog = lastEntry.First(); // See if the datetime retrieved is yesterday. If yesterday, logDirection = true (in) - if (IsLogDateTimeYesterdayOrOlder(lastLog.SwipeEventDateTime.DateTime)) + if (IsLogDateTimeYesterdayOrOlder(lastEntry.SwipeEventDateTime.DateTime)) { logDirection = LogDirectionDb.IN; } else { // we have a time log from today already, so just do the opposite of what we last did! - if (lastLog.Direction == LogDirectionDb.IN) + if (lastEntry.Direction == LogDirectionDb.IN) logDirection = LogDirectionDb.OUT; - else if (lastLog.Direction == LogDirectionDb.OUT) + else if (lastEntry.Direction == LogDirectionDb.OUT) logDirection = LogDirectionDb.IN; } } @@ -532,6 +537,17 @@ namespace SQLiteRepository return logDirection; } + private TimeLogDb GetLastTimeLog(int userId) + { + var lastEntry = _connection.Query( + SQLiteProcedures.GET_LAST_TIMELOG_DIRECTION, + userId); + if (lastEntry.Any()) + { + return lastEntry.First(); + } + return null; + } /// /// Get the calendar week of the year according to the ISO8601 standard (starts monday). ///