Flatten the TimeLogsList object.
put DayOfWeek and Day into the DailyLogs object to help flatten. changed GetTimeLogList to return List<DailyLogs>. #15
This commit is contained in:
parent
8756964f0b
commit
59e27c2991
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Interfaces
|
||||
{
|
||||
@ -8,6 +9,8 @@ namespace Interfaces
|
||||
{
|
||||
Logs = new List<TimeLog>();
|
||||
}
|
||||
public DayOfWeek Day { get; set; }
|
||||
public string DayOfWeek { get; set; }
|
||||
public int LogCount { get { return Logs.Count; } }
|
||||
public double DailyTotal { get; set; }
|
||||
public List<TimeLog> Logs { get; set; }
|
||||
|
||||
@ -8,14 +8,14 @@ namespace Interfaces
|
||||
{
|
||||
public TimeLogList()
|
||||
{
|
||||
TimeLogs = new Dictionary<DayOfWeek, DailyLogs>();
|
||||
TimeLogs = new List<DailyLogs>();
|
||||
}
|
||||
public DateTime SelectedDate { get; set; }
|
||||
public int CalendarWeek { get; set; }
|
||||
public int TimeLogCount { get { return TimeLogs.Values.Sum(x => x.LogCount); } }
|
||||
public Dictionary<DayOfWeek,DailyLogs> TimeLogs { get; set; }
|
||||
public int TimeLogCount { get { return TimeLogs.Sum(x => x.LogCount); } }
|
||||
public List<DailyLogs> TimeLogs { get; set; }
|
||||
public double WeeklyTotal {
|
||||
get { return TimeLogs.Values.Sum(x => x.DailyTotal); }
|
||||
get { return TimeLogs.Sum(x => x.DailyTotal); }
|
||||
}
|
||||
public float HoursPerWeekMinutes { get; set; }
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using Interfaces;
|
||||
using SQLite.Net;
|
||||
using SQLite.Net.Platform.Win32;
|
||||
@ -16,7 +17,7 @@ namespace SQLiteRepository
|
||||
|
||||
public SQLiteRepository(ILogger logger)
|
||||
{
|
||||
if(logger == null) throw new ArgumentNullException(nameof(logger));
|
||||
if (logger == null) throw new ArgumentNullException(nameof(logger));
|
||||
_logger = logger;
|
||||
|
||||
_connection = new SQLiteConnection(new SQLitePlatformWin32(), _path);
|
||||
@ -27,7 +28,7 @@ namespace SQLiteRepository
|
||||
_logger.Trace("Initialised SQLite Repository");
|
||||
}
|
||||
|
||||
public UserList GetUsers(int pageNumber=-1, int pageSize=-1)
|
||||
public UserList GetUsers(int pageNumber = -1, int pageSize = -1)
|
||||
{
|
||||
var ret = new UserList();
|
||||
List<UserIdentity> users;
|
||||
@ -40,7 +41,7 @@ namespace SQLiteRepository
|
||||
else
|
||||
{
|
||||
users = _connection.Query<UserIdentity>(SQLiteProcedures.GET_ALL_USERS_PAGINATE,
|
||||
pageSize, (pageNumber-1)*pageSize);
|
||||
pageSize, (pageNumber - 1) * pageSize);
|
||||
userCount = _connection.ExecuteScalar<int>(SQLiteProcedures.GET_TOTAL_USER_COUNT);
|
||||
}
|
||||
|
||||
@ -176,7 +177,7 @@ namespace SQLiteRepository
|
||||
|
||||
public TimeLogList GetTimeLogs(int userId, DateTime selectedDate)
|
||||
{
|
||||
var ret = new TimeLogList {SelectedDate = selectedDate.Date};
|
||||
var ret = new TimeLogList { SelectedDate = selectedDate.Date };
|
||||
var calendarWeek = GetIso8601CalendarWeek(selectedDate);
|
||||
ret.CalendarWeek = calendarWeek;
|
||||
|
||||
@ -191,7 +192,7 @@ namespace SQLiteRepository
|
||||
|
||||
try
|
||||
{
|
||||
ret.HoursPerWeekMinutes = GetUserContractedHours(userId)*60.0f;
|
||||
ret.HoursPerWeekMinutes = GetUserContractedHours(userId) * 60.0f;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -229,7 +230,7 @@ namespace SQLiteRepository
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
//TODO: Check time logs table on update to ensure associated cards/unique identifiers are removed/added as appropriate.
|
||||
public OperationResponse UpdateUser(User user, out int userIdResult)
|
||||
{
|
||||
@ -241,8 +242,8 @@ namespace SQLiteRepository
|
||||
//Get a list of current associated identifiers, convert into a list of Identifier Objects..
|
||||
var currentCards =
|
||||
_connection.Query<CardUniqueId>(SQLiteProcedures.GET_CARDS_BY_USER_ID, user.UserId)
|
||||
.Select(x => new Identifier {Id = x.Id, IsAssociatedToUser = true, UniqueId = x.CardUId});
|
||||
var cardsToRemove = currentCards.Except(user.AssociatedIdentifiers).Select(x=>x.Id).ToList();
|
||||
.Select(x => new Identifier { Id = x.Id, IsAssociatedToUser = true, UniqueId = x.CardUId });
|
||||
var cardsToRemove = currentCards.Except(user.AssociatedIdentifiers).Select(x => x.Id).ToList();
|
||||
|
||||
#region GetUnique Identifiers
|
||||
foreach (var card in user.AssociatedIdentifiers)
|
||||
@ -264,7 +265,7 @@ namespace SQLiteRepository
|
||||
if (ret < OperationResponse.UPDATED)
|
||||
ret = OperationResponse.UPDATED;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update/Create User
|
||||
@ -311,7 +312,7 @@ namespace SQLiteRepository
|
||||
_connection.Query<CardUniqueId>(
|
||||
SQLiteProcedures.UPDATE_CARD_USER_ID,
|
||||
-1, card);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
userIdResult = userId;
|
||||
@ -337,7 +338,7 @@ namespace SQLiteRepository
|
||||
{
|
||||
//TODO: log when more than one comes back. should NEVER happen but....
|
||||
ident = cardIdQuery.First();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
// Get The User Direction (are they going in or out)?
|
||||
@ -346,7 +347,7 @@ namespace SQLiteRepository
|
||||
#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;
|
||||
var year = logTime.Year;
|
||||
#endregion
|
||||
|
||||
//TODO: Handle When the identifier is assigned to a user (identifier has -1)
|
||||
@ -359,16 +360,16 @@ namespace SQLiteRepository
|
||||
IdentifierId = ident.Id,
|
||||
Direction = logDirection,
|
||||
Year = year,
|
||||
CalendarWeek=calendarWeek
|
||||
CalendarWeek = calendarWeek
|
||||
};
|
||||
|
||||
|
||||
_connection.Insert(timeLog);
|
||||
logId = timeLog.Id;
|
||||
|
||||
return OperationResponse.SUCCESS;
|
||||
}
|
||||
|
||||
private Dictionary<DayOfWeek, DailyLogs> GetTimeLogList(int userId, int calendarWeek, int year)
|
||||
private List<DailyLogs> GetTimeLogList(int userId, int calendarWeek, int year)
|
||||
{
|
||||
var timeLogList = _connection.Query<TimeLogDb>(
|
||||
SQLiteProcedures.GET_TIMELOGS,
|
||||
@ -386,6 +387,7 @@ namespace SQLiteRepository
|
||||
}).ToList();
|
||||
|
||||
var dict = new Dictionary<DayOfWeek, DailyLogs>();
|
||||
var logList = new List<DailyLogs>();
|
||||
|
||||
//make sure each day of the week is accounted for in the dictionary.
|
||||
foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek)))
|
||||
@ -398,13 +400,32 @@ namespace SQLiteRepository
|
||||
{
|
||||
dict[log.EventTime.DayOfWeek].Logs.Add(log);
|
||||
}
|
||||
|
||||
var logGroups = timeLogs.GroupBy(x => x.EventTime.DayOfWeek);
|
||||
foreach (var group in logGroups)
|
||||
{
|
||||
var groupLogs = group.ToList();
|
||||
var dailyLog = new DailyLogs
|
||||
{
|
||||
Logs = groupLogs,
|
||||
Day = group.Key,
|
||||
DayOfWeek = group.Key.ToString()
|
||||
};
|
||||
dailyLog.DailyTotal = CalculateDailyTotal(dailyLog);
|
||||
logList.Add(dailyLog);
|
||||
}
|
||||
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()};
|
||||
logList.Add(dailyLog);
|
||||
}
|
||||
|
||||
foreach (var dailyCollection in dict)
|
||||
{
|
||||
{
|
||||
dailyCollection.Value.DailyTotal = CalculateDailyTotal(dailyCollection.Value);
|
||||
}
|
||||
|
||||
return dict.OrderBy(x => ((int)x.Key + 6) % 7).ToDictionary(x=>x.Key, x=>x.Value);
|
||||
|
||||
return logList.OrderBy(x => ((int) x.Day + 6)%7).ToList();
|
||||
}
|
||||
|
||||
private double CalculateDailyTotal(DailyLogs dailyLogs)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user