implemented the GetTimeLogs methods.
re-referenced TimeLog to TimeLogDB implemented the LogEventTime method, now logs direction user was going. also logs the CalendarWeek and Year, returns the Id of the time log entry now too.
This commit is contained in:
parent
3d64fc90ff
commit
0867cbd7a6
@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using Interfaces;
|
||||
using SQLite.Net;
|
||||
using SQLite.Net.Platform.Win32;
|
||||
@ -18,7 +20,7 @@ namespace SQLiteRepository
|
||||
|
||||
_connection.CreateTable<CardUniqueId>();
|
||||
_connection.CreateTable<UserIdentity>();
|
||||
_connection.CreateTable<TimeLog>();
|
||||
_connection.CreateTable<TimeLogDB>();
|
||||
}
|
||||
|
||||
public UserList GetUsers()
|
||||
@ -123,12 +125,26 @@ namespace SQLiteRepository
|
||||
|
||||
public TimeLogList GetTimeLogs(int userId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var ret = new TimeLogList();
|
||||
//get this calendar week;
|
||||
var now = DateTime.UtcNow;
|
||||
var calendarWeek = GetIso8601CalendarWeek(now);
|
||||
ret.CalendarWeek = calendarWeek;
|
||||
|
||||
ret.TimeLogs = GetTimeLogList(userId, calendarWeek, now.Year);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public TimeLogList GetTimeLogs(int userId, int calendarWeek)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var ret = new TimeLogList();
|
||||
var now = DateTime.UtcNow;
|
||||
ret.CalendarWeek = calendarWeek;
|
||||
|
||||
ret.TimeLogs = GetTimeLogList(userId, calendarWeek, now.Year);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public IdentifierList GetUnassignedIdentifierList()
|
||||
@ -238,12 +254,13 @@ namespace SQLiteRepository
|
||||
return ret;
|
||||
}
|
||||
|
||||
public OperationResponse LogEventTime(Identifier identifier)
|
||||
public OperationResponse LogEventTime(Identifier identifier, out int logId)
|
||||
{
|
||||
var cardIdQuery = _connection.Query<CardUniqueId>(
|
||||
SQLiteProcedures.GET_CARDS_BY_UNIQUE_ID,
|
||||
identifier.UniqueId);
|
||||
|
||||
#region Get/Insert the PK Id Identifier to associate the time log to.
|
||||
var ident = new CardUniqueId();
|
||||
if (!cardIdQuery.Any())
|
||||
{
|
||||
@ -257,49 +274,104 @@ namespace SQLiteRepository
|
||||
//TODO: log when more than one comes back. should NEVER happen but....
|
||||
ident = cardIdQuery.First();
|
||||
}
|
||||
//TODO: change log direction to an ENUM
|
||||
//TODO: Handle In/Out Flag..
|
||||
var logDirection = false;
|
||||
//get the last flag
|
||||
if (ident.UserId_FK != -1)
|
||||
#endregion
|
||||
|
||||
// Get The User Direction (are they going in or out)?
|
||||
var logDirection = GetLogDirection(ident.UserId_FK);
|
||||
|
||||
#region Get the current time (for swiping). and calendar week/year to help recall the data.
|
||||
var logTime = DateTime.UtcNow;
|
||||
var calendarWeek = GetIso8601CalendarWeek(logTime);
|
||||
var year = logTime.Year;
|
||||
#endregion
|
||||
|
||||
//TODO: Handle When the identifier is assigned to a user (identifier has -1)
|
||||
//when identifier not assigned to user, just store it anyway and carry on, can update later.
|
||||
|
||||
var timeLog = new TimeLogDB
|
||||
{
|
||||
var lastEntry = _connection.Query<TimeLog>(
|
||||
SwipeEventDateTime = DateTime.UtcNow,
|
||||
UserId_FK = ident.UserId_FK,
|
||||
IdentifierId = ident.Id,
|
||||
Direction = logDirection,
|
||||
Year = year,
|
||||
CalendarWeek=calendarWeek
|
||||
};
|
||||
|
||||
_connection.Insert(timeLog);
|
||||
logId = timeLog.Id;
|
||||
|
||||
return OperationResponse.SUCCESS;
|
||||
}
|
||||
|
||||
private List<TimeLog> GetTimeLogList(int userId, int calendarWeek, int year)
|
||||
{
|
||||
List<TimeLog> ret;
|
||||
var timeLogList = _connection.Query<TimeLogDB>(
|
||||
SQLiteProcedures.GET_TIMELOGS,
|
||||
userId, calendarWeek, year);
|
||||
|
||||
ret= timeLogList.Select(x => new TimeLog
|
||||
{
|
||||
Id = x.Id,
|
||||
CalendarWeek = x.CalendarWeek,
|
||||
Direction = (LogDirection)x.Direction,
|
||||
IdentifierId = x.IdentifierId,
|
||||
EventTime = x.SwipeEventDateTime,
|
||||
UserId = x.UserId_FK,
|
||||
Year = x.Year
|
||||
}).ToList();
|
||||
return ret;
|
||||
}
|
||||
|
||||
private LogDirectionDB GetLogDirection(int userId)
|
||||
{
|
||||
var logDirection = LogDirectionDB.UNKNOWN;
|
||||
if (userId != -1)
|
||||
{
|
||||
var lastEntry = _connection.Query<TimeLogDB>(
|
||||
SQLiteProcedures.GET_LAST_TIMELOG_DIRECTION,
|
||||
ident.UserId_FK);
|
||||
userId);
|
||||
if (lastEntry.Any())
|
||||
{
|
||||
var lastLog = lastEntry.First();
|
||||
// See if the datetime retrieved is yesterday. If yesterday, logDirection = true (in)
|
||||
if (IsLogDateTimeYesterdayOrOlder(lastLog.SwipeEventDateTime.DateTime))
|
||||
{
|
||||
logDirection = true;
|
||||
logDirection = LogDirectionDB.IN;
|
||||
}
|
||||
else
|
||||
{
|
||||
logDirection = !lastLog.InOut;
|
||||
}
|
||||
// we have a time log from today already, so just do the opposite of what we last did!
|
||||
if (lastLog.Direction == LogDirectionDB.IN)
|
||||
logDirection = LogDirectionDB.OUT;
|
||||
else if (lastLog.Direction == LogDirectionDB.OUT)
|
||||
logDirection = LogDirectionDB.IN;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logDirection = false; //TODO: need an "unknown" state..
|
||||
//assume its the first then!
|
||||
logDirection = LogDirectionDB.IN;
|
||||
}
|
||||
}
|
||||
return logDirection;
|
||||
}
|
||||
//SQLiteProcedures.GET_LAST_TIMELOG_DIRECTION
|
||||
//then invert it, and store it..
|
||||
|
||||
//TODO: Handle When the identifier is assigned to a user (identifier has -1)
|
||||
//when identifier not assigned to user, just store it anyway and carry on, can update later.
|
||||
var timeLog = new TimeLog
|
||||
/// <summary>
|
||||
/// Get the calendar week of the year according to the ISO8601 standard (starts monday).
|
||||
/// </summary>
|
||||
/// <param name="date">the date to get the calendar week of.</param>
|
||||
/// <returns>the calendar week of the year in integer form (1-52)</returns>
|
||||
private int GetIso8601CalendarWeek(DateTime date)
|
||||
{
|
||||
SwipeEventDateTime = DateTime.UtcNow,
|
||||
UserId_FK = ident.UserId_FK,
|
||||
IdentifierId = ident.Id,
|
||||
InOut = logDirection
|
||||
};
|
||||
|
||||
_connection.Insert(timeLog);
|
||||
|
||||
return OperationResponse.SUCCESS;
|
||||
var day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(date);
|
||||
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
|
||||
{
|
||||
date = date.AddDays(3);
|
||||
}
|
||||
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek,
|
||||
DayOfWeek.Monday);
|
||||
}
|
||||
|
||||
private bool IsLogDateTimeYesterdayOrOlder(DateTime dt)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user