Implement CreateLog, DeleteLog, UpdateLog methods from interface.
include LogSourceDb in project #29
This commit is contained in:
parent
4f4d247dc6
commit
101c19e4f4
@ -87,7 +87,7 @@ namespace SQLiteRepository
|
||||
}
|
||||
if (pageNumber == -1 && pageSize == -1)
|
||||
{
|
||||
ret.PageSize = 10;
|
||||
ret.PageSize = 10;
|
||||
ret.PageNumber = 1;
|
||||
}
|
||||
else
|
||||
@ -353,7 +353,7 @@ namespace SQLiteRepository
|
||||
|
||||
#region Update Group Associations
|
||||
|
||||
SetUserGroups(userId, user.Groups.Where(x=>x.IsAssociatedToUser).ToList());
|
||||
SetUserGroups(userId, user.Groups.Where(x => x.IsAssociatedToUser).ToList());
|
||||
|
||||
#endregion
|
||||
|
||||
@ -392,7 +392,7 @@ namespace SQLiteRepository
|
||||
|
||||
#region Check the user hasnt registered an event in the last few minutes..
|
||||
|
||||
if (ident.UserId_FK!=-1)
|
||||
if (ident.UserId_FK != -1)
|
||||
{ //only check log gap if the card is associated to a user
|
||||
var hysteresisThresholdMinutes = Convert.ToInt32(ConfigurationHandler.ConfigurationHandler.GetConfiguration("SwipeTimeGap") ?? "3");
|
||||
var threshold = DateTime.UtcNow.AddMinutes(0 - hysteresisThresholdMinutes);
|
||||
@ -405,7 +405,7 @@ namespace SQLiteRepository
|
||||
_logger.Error("Not logging event for user id: {0}, logged event within TimeGap Threshold of {1}", ident.UserId_FK, threshold);
|
||||
logId = -1;
|
||||
return OperationResponse.FAILED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -415,7 +415,7 @@ namespace SQLiteRepository
|
||||
var calendarWeek = GetIso8601CalendarWeek(logTime);
|
||||
var year = logTime.Year;
|
||||
#endregion
|
||||
|
||||
|
||||
var timeLog = new TimeLogDb
|
||||
{
|
||||
SwipeEventDateTime = DateTime.UtcNow,
|
||||
@ -423,7 +423,8 @@ namespace SQLiteRepository
|
||||
IdentifierId = ident.Id,
|
||||
Direction = logDirection,
|
||||
Year = year,
|
||||
CalendarWeek = calendarWeek
|
||||
CalendarWeek = calendarWeek,
|
||||
Source = LogSourceDb.IDENTIFIER
|
||||
};
|
||||
|
||||
_connection.Insert(timeLog);
|
||||
@ -438,13 +439,13 @@ namespace SQLiteRepository
|
||||
//TODO: check group name can only be entered once.
|
||||
public OperationResponse CreateGroup(Group group, out int groupId)
|
||||
{
|
||||
var groupDb = new GroupDb { GroupName=group.Name};
|
||||
var groupDb = new GroupDb { GroupName = group.Name };
|
||||
var resp = _connection.Insert(groupDb);
|
||||
groupId = groupDb.GroupId;
|
||||
return OperationResponse.CREATED;
|
||||
}
|
||||
|
||||
public List<Group> GetGroups(int userId=-1)
|
||||
public List<Group> GetGroups(int userId = -1)
|
||||
{
|
||||
var ret = new List<Group>();
|
||||
List<GroupDb> query;
|
||||
@ -462,10 +463,10 @@ namespace SQLiteRepository
|
||||
var t = new UserGroupJoinDb();
|
||||
query =
|
||||
_connection.Query<GroupDb>(
|
||||
"select gdb.GroupId, gdb.GroupName, gdb.AssignedUserCount"+
|
||||
" from GroupDb gdb"+
|
||||
" left join UserGroupJoinDb ujdb"+
|
||||
" on gdb.GroupId = ujdb.GroupId_FK"+
|
||||
"select gdb.GroupId, gdb.GroupName, gdb.AssignedUserCount" +
|
||||
" from GroupDb gdb" +
|
||||
" left join UserGroupJoinDb ujdb" +
|
||||
" on gdb.GroupId = ujdb.GroupId_FK" +
|
||||
" where ujdb.UserId_FK = ?",
|
||||
userId);
|
||||
}
|
||||
@ -473,9 +474,9 @@ namespace SQLiteRepository
|
||||
{
|
||||
ret.Add(new Group
|
||||
{
|
||||
Id=group.GroupId,
|
||||
Id = group.GroupId,
|
||||
Name = group.GroupName,
|
||||
UserCount = int.Parse(group.AssignedUserCount??"0")
|
||||
UserCount = int.Parse(group.AssignedUserCount ?? "0")
|
||||
});
|
||||
}
|
||||
return ret;
|
||||
@ -516,9 +517,76 @@ namespace SQLiteRepository
|
||||
return OperationResponse.DELETED;
|
||||
}
|
||||
|
||||
public OperationResponse DeleteLog(TimeLog log)
|
||||
{
|
||||
var query = _connection.Query<TimeLogDb>(
|
||||
"select * from TimeLogDb where Id=?", log.Id);
|
||||
|
||||
if (!query.Any())
|
||||
return OperationResponse.FAILED;
|
||||
|
||||
_connection.ExecuteScalar<TimeLogDb>("delete from TimeLogDb where Id=?", log.Id);
|
||||
|
||||
return OperationResponse.DELETED;
|
||||
}
|
||||
|
||||
public OperationResponse CreateLog(TimeLog log)
|
||||
{
|
||||
var calendarWeek = GetIso8601CalendarWeek(log.EventTime.UtcDateTime);
|
||||
var year = log.EventTime.Year;
|
||||
var dbLog = new TimeLogDb
|
||||
{
|
||||
SwipeEventDateTime = log.EventTime,
|
||||
Direction = (LogDirectionDb)(int)log.Direction,
|
||||
Year = year,
|
||||
CalendarWeek = calendarWeek,
|
||||
Source = (LogSourceDb)(int)log.Source,
|
||||
UserId_FK = log.UserId,
|
||||
IdentifierId = ConvertSourceToIdentifierId(log.Source),
|
||||
};
|
||||
_connection.Insert(dbLog);
|
||||
return OperationResponse.CREATED;
|
||||
}
|
||||
|
||||
public OperationResponse UpdateLog(TimeLog log)
|
||||
{
|
||||
var query = _connection.Query<TimeLogDb>(
|
||||
"select * from TimeLogDb where Id=?", log.Id);
|
||||
if(!query.Any())
|
||||
return OperationResponse.FAILED;
|
||||
|
||||
if (log.CalendarWeek > 52 || log.CalendarWeek < 1)
|
||||
{
|
||||
log.CalendarWeek = GetIso8601CalendarWeek(log.EventTime.UtcDateTime);
|
||||
}
|
||||
if (log.Year < 2017)
|
||||
{
|
||||
log.Year = log.EventTime.Year;
|
||||
}
|
||||
_connection.ExecuteScalar<TimeLogDb>(
|
||||
"update TimeLogDb set UserId_FK=?, Direction=?,SwipeEventDateTime=?,CalendarWeek=?,Year=?,Source=? where Id=?",
|
||||
log.UserId, (LogDirectionDb) (int) log.Direction, log.EventTime, log.CalendarWeek, log.Year,
|
||||
(LogSourceDb) (int) log.Source, log.Id);
|
||||
|
||||
return OperationResponse.UPDATED;
|
||||
}
|
||||
|
||||
private int ConvertSourceToIdentifierId(LogSource logSource)
|
||||
{
|
||||
switch (logSource)
|
||||
{
|
||||
case LogSource.UI:
|
||||
return -100;
|
||||
case LogSource.TRAYAPP:
|
||||
return -200;
|
||||
default:
|
||||
return -10;
|
||||
}
|
||||
}
|
||||
|
||||
private bool SetUserGroups(int userId, List<Group> groups)
|
||||
{
|
||||
var groupIds = GetGroupIds(groups.Select(x=>x.Name).ToList());
|
||||
var groupIds = GetGroupIds(groups.Select(x => x.Name).ToList());
|
||||
return SetUserGroups(userId, groupIds);
|
||||
}
|
||||
|
||||
@ -527,7 +595,7 @@ namespace SQLiteRepository
|
||||
//remove the existing user>group associations
|
||||
_connection.Query<UserGroupJoinDb>("delete from UserGroupJoinDb where UserId_FK = ?", userId);
|
||||
//add the new group associations.
|
||||
_connection.InsertAll(groupIds.Select(x => new UserGroupJoinDb {GroupId_FK = x, UserId_FK = userId}));
|
||||
_connection.InsertAll(groupIds.Select(x => new UserGroupJoinDb { GroupId_FK = x, UserId_FK = userId }));
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -560,7 +628,7 @@ namespace SQLiteRepository
|
||||
EventTime = x.SwipeEventDateTime,
|
||||
UserId = x.UserId_FK,
|
||||
Year = x.Year
|
||||
}).ToList();
|
||||
}).OrderBy(x=>x.EventTime.UtcDateTime).ToList();
|
||||
|
||||
var dict = new Dictionary<DayOfWeek, DailyLogs>();
|
||||
var logList = new List<DailyLogs>();
|
||||
@ -572,7 +640,7 @@ namespace SQLiteRepository
|
||||
}
|
||||
|
||||
//add the logs to the respective day of the week.
|
||||
foreach (var log in timeLogs)
|
||||
foreach (var log in timeLogs.OrderBy(x=>x.EventTime))
|
||||
{
|
||||
dict[log.EventTime.DayOfWeek].Logs.Add(log);
|
||||
}
|
||||
@ -592,7 +660,7 @@ namespace SQLiteRepository
|
||||
foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek)))
|
||||
{
|
||||
if (logList.Any(x => x.Day == day)) continue;
|
||||
var dailyLog = new DailyLogs {Day = day, DayOfWeek = day.ToString()};
|
||||
var dailyLog = new DailyLogs { Day = day, DayOfWeek = day.ToString() };
|
||||
logList.Add(dailyLog);
|
||||
}
|
||||
|
||||
@ -601,13 +669,13 @@ namespace SQLiteRepository
|
||||
dailyCollection.Value.DailyTotal = CalculateDailyTotal(dailyCollection.Value);
|
||||
}
|
||||
|
||||
return logList.OrderBy(x => ((int) x.Day + 6)%7).ToList();
|
||||
return logList.OrderBy(x => ((int)x.Day + 6) % 7).ToList();
|
||||
}
|
||||
|
||||
private double CalculateDailyTotal(DailyLogs dailyLogs)
|
||||
{
|
||||
var totalInTime = TimeSpan.FromSeconds(0);
|
||||
var logs = dailyLogs.Logs.OrderBy(x => x.Id).ToArray();
|
||||
var logs = dailyLogs.Logs.OrderBy(x => x.EventTime.UtcDateTime).ToArray();
|
||||
var totalCalcMax = IsOdd(logs.Length) ? logs.Length - 1 : logs.Length;
|
||||
for (int i = 0; i < totalCalcMax; i += 2)
|
||||
{
|
||||
|
||||
@ -61,6 +61,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="CardUniqueId.cs" />
|
||||
<Compile Include="GroupDb.cs" />
|
||||
<Compile Include="LogSourceDb.cs" />
|
||||
<Compile Include="SQLiteRepository.cs" />
|
||||
<Compile Include="Constants.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
||||
Loading…
Reference in New Issue
Block a user