From 59e27c2991340ba08b2b4adaa626b42500ec3aa3 Mon Sep 17 00:00:00 2001 From: "Chris.Watts90@outlook.com" Date: Wed, 15 Feb 2017 16:51:19 +0000 Subject: [PATCH] Flatten the TimeLogsList object. put DayOfWeek and Day into the DailyLogs object to help flatten. changed GetTimeLogList to return List. #15 --- .../WindowsDataCenter/Interfaces/DailyLogs.cs | 5 +- .../Interfaces/TimeLogList.cs | 8 +-- .../SQLiteRepository/SQLiteRepository.cs | 59 +++++++++++++------ 3 files changed, 48 insertions(+), 24 deletions(-) diff --git a/DataCenter_Windows/WindowsDataCenter/Interfaces/DailyLogs.cs b/DataCenter_Windows/WindowsDataCenter/Interfaces/DailyLogs.cs index 61c9a2c..44305e8 100644 --- a/DataCenter_Windows/WindowsDataCenter/Interfaces/DailyLogs.cs +++ b/DataCenter_Windows/WindowsDataCenter/Interfaces/DailyLogs.cs @@ -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(); } + public DayOfWeek Day { get; set; } + public string DayOfWeek { get; set; } public int LogCount { get { return Logs.Count; } } public double DailyTotal { get; set; } public List Logs { get; set; } diff --git a/DataCenter_Windows/WindowsDataCenter/Interfaces/TimeLogList.cs b/DataCenter_Windows/WindowsDataCenter/Interfaces/TimeLogList.cs index 9b7bb36..721aec4 100644 --- a/DataCenter_Windows/WindowsDataCenter/Interfaces/TimeLogList.cs +++ b/DataCenter_Windows/WindowsDataCenter/Interfaces/TimeLogList.cs @@ -8,14 +8,14 @@ namespace Interfaces { public TimeLogList() { - TimeLogs = new Dictionary(); + TimeLogs = new List(); } public DateTime SelectedDate { get; set; } public int CalendarWeek { get; set; } - public int TimeLogCount { get { return TimeLogs.Values.Sum(x => x.LogCount); } } - public Dictionary TimeLogs { get; set; } + public int TimeLogCount { get { return TimeLogs.Sum(x => x.LogCount); } } + public List 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; } } diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs index e2598be..55a878e 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs @@ -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 users; @@ -40,7 +41,7 @@ namespace SQLiteRepository else { users = _connection.Query(SQLiteProcedures.GET_ALL_USERS_PAGINATE, - pageSize, (pageNumber-1)*pageSize); + pageSize, (pageNumber - 1) * pageSize); userCount = _connection.ExecuteScalar(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(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( 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 GetTimeLogList(int userId, int calendarWeek, int year) + private List GetTimeLogList(int userId, int calendarWeek, int year) { var timeLogList = _connection.Query( SQLiteProcedures.GET_TIMELOGS, @@ -386,6 +387,7 @@ namespace SQLiteRepository }).ToList(); var dict = new Dictionary(); + var logList = new List(); //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)