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:
Chris.Watts90@outlook.com 2017-02-15 16:51:19 +00:00
parent 8756964f0b
commit 59e27c2991
3 changed files with 48 additions and 24 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
namespace Interfaces namespace Interfaces
{ {
@ -8,6 +9,8 @@ namespace Interfaces
{ {
Logs = new List<TimeLog>(); Logs = new List<TimeLog>();
} }
public DayOfWeek Day { get; set; }
public string DayOfWeek { get; set; }
public int LogCount { get { return Logs.Count; } } public int LogCount { get { return Logs.Count; } }
public double DailyTotal { get; set; } public double DailyTotal { get; set; }
public List<TimeLog> Logs { get; set; } public List<TimeLog> Logs { get; set; }

View File

@ -8,14 +8,14 @@ namespace Interfaces
{ {
public TimeLogList() public TimeLogList()
{ {
TimeLogs = new Dictionary<DayOfWeek, DailyLogs>(); TimeLogs = new List<DailyLogs>();
} }
public DateTime SelectedDate { get; set; } public DateTime SelectedDate { get; set; }
public int CalendarWeek { get; set; } public int CalendarWeek { get; set; }
public int TimeLogCount { get { return TimeLogs.Values.Sum(x => x.LogCount); } } public int TimeLogCount { get { return TimeLogs.Sum(x => x.LogCount); } }
public Dictionary<DayOfWeek,DailyLogs> TimeLogs { get; set; } public List<DailyLogs> TimeLogs { get; set; }
public double WeeklyTotal { public double WeeklyTotal {
get { return TimeLogs.Values.Sum(x => x.DailyTotal); } get { return TimeLogs.Sum(x => x.DailyTotal); }
} }
public float HoursPerWeekMinutes { get; set; } public float HoursPerWeekMinutes { get; set; }
} }

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Interfaces; using Interfaces;
using SQLite.Net; using SQLite.Net;
using SQLite.Net.Platform.Win32; using SQLite.Net.Platform.Win32;
@ -16,7 +17,7 @@ namespace SQLiteRepository
public SQLiteRepository(ILogger logger) public SQLiteRepository(ILogger logger)
{ {
if(logger == null) throw new ArgumentNullException(nameof(logger)); if (logger == null) throw new ArgumentNullException(nameof(logger));
_logger = logger; _logger = logger;
_connection = new SQLiteConnection(new SQLitePlatformWin32(), _path); _connection = new SQLiteConnection(new SQLitePlatformWin32(), _path);
@ -27,7 +28,7 @@ namespace SQLiteRepository
_logger.Trace("Initialised SQLite Repository"); _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(); var ret = new UserList();
List<UserIdentity> users; List<UserIdentity> users;
@ -40,7 +41,7 @@ namespace SQLiteRepository
else else
{ {
users = _connection.Query<UserIdentity>(SQLiteProcedures.GET_ALL_USERS_PAGINATE, 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); userCount = _connection.ExecuteScalar<int>(SQLiteProcedures.GET_TOTAL_USER_COUNT);
} }
@ -176,7 +177,7 @@ namespace SQLiteRepository
public TimeLogList GetTimeLogs(int userId, DateTime selectedDate) 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); var calendarWeek = GetIso8601CalendarWeek(selectedDate);
ret.CalendarWeek = calendarWeek; ret.CalendarWeek = calendarWeek;
@ -191,7 +192,7 @@ namespace SQLiteRepository
try try
{ {
ret.HoursPerWeekMinutes = GetUserContractedHours(userId)*60.0f; ret.HoursPerWeekMinutes = GetUserContractedHours(userId) * 60.0f;
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -241,8 +242,8 @@ namespace SQLiteRepository
//Get a list of current associated identifiers, convert into a list of Identifier Objects.. //Get a list of current associated identifiers, convert into a list of Identifier Objects..
var currentCards = var currentCards =
_connection.Query<CardUniqueId>(SQLiteProcedures.GET_CARDS_BY_USER_ID, user.UserId) _connection.Query<CardUniqueId>(SQLiteProcedures.GET_CARDS_BY_USER_ID, user.UserId)
.Select(x => new Identifier {Id = x.Id, IsAssociatedToUser = true, UniqueId = x.CardUId}); .Select(x => new Identifier { Id = x.Id, IsAssociatedToUser = true, UniqueId = x.CardUId });
var cardsToRemove = currentCards.Except(user.AssociatedIdentifiers).Select(x=>x.Id).ToList(); var cardsToRemove = currentCards.Except(user.AssociatedIdentifiers).Select(x => x.Id).ToList();
#region GetUnique Identifiers #region GetUnique Identifiers
foreach (var card in user.AssociatedIdentifiers) foreach (var card in user.AssociatedIdentifiers)
@ -359,7 +360,7 @@ namespace SQLiteRepository
IdentifierId = ident.Id, IdentifierId = ident.Id,
Direction = logDirection, Direction = logDirection,
Year = year, Year = year,
CalendarWeek=calendarWeek CalendarWeek = calendarWeek
}; };
_connection.Insert(timeLog); _connection.Insert(timeLog);
@ -368,7 +369,7 @@ namespace SQLiteRepository
return OperationResponse.SUCCESS; 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>( var timeLogList = _connection.Query<TimeLogDb>(
SQLiteProcedures.GET_TIMELOGS, SQLiteProcedures.GET_TIMELOGS,
@ -386,6 +387,7 @@ namespace SQLiteRepository
}).ToList(); }).ToList();
var dict = new Dictionary<DayOfWeek, DailyLogs>(); var dict = new Dictionary<DayOfWeek, DailyLogs>();
var logList = new List<DailyLogs>();
//make sure each day of the week is accounted for in the dictionary. //make sure each day of the week is accounted for in the dictionary.
foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek))) foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek)))
@ -398,13 +400,32 @@ namespace SQLiteRepository
{ {
dict[log.EventTime.DayOfWeek].Logs.Add(log); 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) foreach (var dailyCollection in dict)
{ {
dailyCollection.Value.DailyTotal = CalculateDailyTotal(dailyCollection.Value); 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) private double CalculateDailyTotal(DailyLogs dailyLogs)