Merge branch '#89-UpdateLogDirectionForManualLog' into Release0.2

This commit is contained in:
Chris.Watts90@outlook.com 2018-05-25 12:28:10 +01:00
commit a2925872d0

View File

@ -574,6 +574,8 @@ namespace SQLiteRepository
if (!query.Any())
return OperationResponse.FAILED;
UpdateExistingLogDirections(log);
_connection.ExecuteScalar<TimeLogDb>("delete from TimeLogDb where Id=?", log.Id);
return OperationResponse.DELETED;
@ -583,6 +585,8 @@ namespace SQLiteRepository
{
var calendarWeek = GetIso8601CalendarWeek(log.EventTime.UtcDateTime);
var year = log.EventTime.Year;
log.CalendarWeek = calendarWeek;
log.Year = year;
var dbLog = new TimeLogDb
{
SwipeEventDateTime = log.EventTime,
@ -593,6 +597,27 @@ namespace SQLiteRepository
UserId_FK = log.UserId,
IdentifierId = ConvertSourceToIdentifierId(log.Source),
};
#region update in/out directions for manual logs.
UpdateExistingLogDirections(log);
//var weekLogs = GetTimeLogList(log.UserId, calendarWeek, year);
////Get the same logs of the day that the log has been entered for
//var todaysLogs = weekLogs.FirstOrDefault(x => x.Day == log.EventTime.DayOfWeek);
//if (todaysLogs!= null)
//{
// //Get the days logs that are after the manually created date
// var logs = todaysLogs.Logs.Where(x => x.EventTime.CompareTo(log.EventTime) >= 0).OrderBy(x => x.EventTime).ToList();
// //Update each log with the inverse progressively
// var currentlogDirection = log.Direction;
// for (var i = 0; i < logs.Count; i++)
// {
// logs[i].Direction = InvertLogDirection(currentlogDirection);
// UpdateLog(logs[i]);
// currentlogDirection = logs[i].Direction;
// }
//}
#endregion
//and now insert the new log.
_connection.Insert(dbLog);
return OperationResponse.CREATED;
}
@ -789,10 +814,7 @@ namespace SQLiteRepository
else
{
// we have a time log from today already, so just do the opposite of what we last did!
if (lastEntry.Direction == LogDirectionDb.IN)
logDirection = LogDirectionDb.OUT;
else if (lastEntry.Direction == LogDirectionDb.OUT)
logDirection = LogDirectionDb.IN;
logDirection = InvertLogDirectionDb(lastEntry.Direction);
}
}
else
@ -878,5 +900,47 @@ namespace SQLiteRepository
IsContractor = user.IsContractor
};
}
private void UpdateExistingLogDirections(TimeLog log)
{
//need to make this generic so that both create and delete will update the log directions.. but ARGH.
//if you want to use this as a delete, you would delete and call this, if you want to create, call this then create.
//should look at how to improve this method so that it will compensate for if the log hasnt been deleted yet, but ott?
var weekLogs = GetTimeLogList(log.UserId, log.CalendarWeek, log.Year);
//Get the same logs of the day that the log has been entered for
var todaysLogs = weekLogs.FirstOrDefault(x => x.Day == log.EventTime.DayOfWeek);
if (todaysLogs != null)
{
//Get the days logs that are after the manually created date
var logs = todaysLogs.Logs.Where(x => x.EventTime.CompareTo(log.EventTime) >= 0).OrderBy(x => x.EventTime).ToList();
//Update each log with the inverse progressively
var currentlogDirection = log.Direction;
for (var i = 0; i < logs.Count; i++)
{
logs[i].Direction = InvertLogDirection(currentlogDirection);
UpdateLog(logs[i]);
currentlogDirection = logs[i].Direction;
}
}
}
private LogDirectionDb InvertLogDirectionDb(LogDirectionDb direction)
{
return (LogDirectionDb) (int) InvertLogDirection((LogDirection) (int) direction);
}
private LogDirection InvertLogDirection(LogDirection direction)
{
switch (direction)
{
case LogDirection.IN:
return LogDirection.OUT;
case LogDirection.OUT:
return LogDirection.IN;
default:
return LogDirection.UNKNOWN;
}
}
}
}