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
This commit is contained in:
Chris.Watts90@outlook.com 2017-03-03 13:00:01 +00:00
parent 309e8c093d
commit 69f3e502e0
2 changed files with 27 additions and 10 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
namespace Interfaces namespace Interfaces
{ {
@ -19,7 +20,7 @@ namespace Interfaces
{ {
get { return AssociatedIdentifiers.Count; } get { return AssociatedIdentifiers.Count; }
} }
public DateTime LastEventDateTime { get; set; }
public List<Identifier> AssociatedIdentifiers { get; set; } public List<Identifier> AssociatedIdentifiers { get; set; }
public bool State { get; set; } public bool State { get; set; }

View File

@ -98,6 +98,14 @@ namespace SQLiteRepository
return ret; return ret;
} }
private DateTime GetLastLogDateTime(TimeLogDb timeLog)
{
if (timeLog != null)
{
return timeLog.SwipeEventDateTime.DateTime;
}
return DateTime.MinValue;
}
private bool GetUserState(LogDirectionDb logDirection) private bool GetUserState(LogDirectionDb logDirection)
{ {
switch (logDirection) switch (logDirection)
@ -503,23 +511,20 @@ namespace SQLiteRepository
var logDirection = LogDirectionDb.UNKNOWN; var logDirection = LogDirectionDb.UNKNOWN;
if (userId != -1) if (userId != -1)
{ {
var lastEntry = _connection.Query<TimeLogDb>( var lastEntry = GetLastTimeLog(userId);
SQLiteProcedures.GET_LAST_TIMELOG_DIRECTION, if (lastEntry != null)
userId);
if (lastEntry.Any())
{ {
var lastLog = lastEntry.First();
// See if the datetime retrieved is yesterday. If yesterday, logDirection = true (in) // 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; logDirection = LogDirectionDb.IN;
} }
else else
{ {
// we have a time log from today already, so just do the opposite of what we last did! // 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; logDirection = LogDirectionDb.OUT;
else if (lastLog.Direction == LogDirectionDb.OUT) else if (lastEntry.Direction == LogDirectionDb.OUT)
logDirection = LogDirectionDb.IN; logDirection = LogDirectionDb.IN;
} }
} }
@ -532,6 +537,17 @@ namespace SQLiteRepository
return logDirection; return logDirection;
} }
private TimeLogDb GetLastTimeLog(int userId)
{
var lastEntry = _connection.Query<TimeLogDb>(
SQLiteProcedures.GET_LAST_TIMELOG_DIRECTION,
userId);
if (lastEntry.Any())
{
return lastEntry.First();
}
return null;
}
/// <summary> /// <summary>
/// Get the calendar week of the year according to the ISO8601 standard (starts monday). /// Get the calendar week of the year according to the ISO8601 standard (starts monday).
/// </summary> /// </summary>