From 36282ee3558620da1d70c85c4ed07debaeb746e4 Mon Sep 17 00:00:00 2001 From: "Chris.Watts90@outlook.com" Date: Mon, 13 Feb 2017 16:57:18 +0000 Subject: [PATCH] added some logging to SQLiteRepository. #9 --- .../SQLiteRepository/SQLiteRepository.cs | 74 ++++++++++++++----- 1 file changed, 56 insertions(+), 18 deletions(-) diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs index 3da2464..1f94020 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; using Interfaces; using SQLite.Net; using SQLite.Net.Platform.Win32; @@ -12,15 +11,20 @@ namespace SQLiteRepository public class SQLiteRepository : IRepository { private readonly SQLiteConnection _connection; + private readonly ILogger _logger; private string _path = "flexitimedb.db"; - public SQLiteRepository() + public SQLiteRepository(ILogger logger) { + if(logger == null) throw new ArgumentNullException(nameof(logger)); + _logger = logger; + _connection = new SQLiteConnection(new SQLitePlatformWin32(), _path); _connection.CreateTable(); _connection.CreateTable(); _connection.CreateTable(); + _logger.Trace("Initialised SQLite Repository"); } public UserList GetUsers(int pageNumber=-1, int pageSize=-1) @@ -89,12 +93,14 @@ namespace SQLiteRepository public UserList Search(string searchParam) { + _logger.Trace("Searching SQLite database for the term: {0}", searchParam); var ret = new UserList(); searchParam = string.Format("%{0}%", searchParam); var users = _connection.Query( SQLiteProcedures.SEARCH_USER_LIST, searchParam, searchParam); + _logger.Trace("Got {0} results for term: {1}", users.Count, searchParam); if (!users.Any()) { ret.PageNumber = 1; @@ -120,6 +126,7 @@ namespace SQLiteRepository } ret.Users.Add(userObj); } + //TODO: figure out paging here. - should there be any? ret.PageSize = 20; ret.PageNumber = 1; @@ -130,21 +137,33 @@ namespace SQLiteRepository { var ret = new User(); - var users = _connection.Query( - SQLiteProcedures.GET_USER_BY_ID, - id); - - if (!users.Any()) return ret; - - var user = users.First(); - ret = ChangeToUserObject(user); - var cards = _connection.Query( - SQLiteProcedures.GET_CARDS_BY_USER_ID, - user.Id); - - foreach (var card in cards) + try { - ret.AssociatedIdentifiers.Add(new Identifier { UniqueId = card.CardUId, IsAssociatedToUser = true, Id = card.Id}); + var users = _connection.Query( + SQLiteProcedures.GET_USER_BY_ID, + id); + + if (!users.Any()) return ret; + + var user = users.First(); + ret = ChangeToUserObject(user); + var cards = _connection.Query( + SQLiteProcedures.GET_CARDS_BY_USER_ID, + user.Id); + + foreach (var card in cards) + { + ret.AssociatedIdentifiers.Add(new Identifier { UniqueId = card.CardUId, IsAssociatedToUser = true, Id = card.Id }); + } + } + catch (Exception ex) + { + _logger.Error(ex, "Error in GetUser with Id: {0}", id); + ret.UserId = id; + ret.FirstName = ret.LastName = string.Empty; + ret.HoursPerWeek = -1.0f; + ret.IsContractor = false; + ret.AssociatedIdentifiers.Clear(); } return ret; @@ -158,7 +177,14 @@ namespace SQLiteRepository var calendarWeek = GetIso8601CalendarWeek(now); ret.CalendarWeek = calendarWeek; ret.SelectedDate = now.Date; - ret.TimeLogs = GetTimeLogList(userId, calendarWeek, now.Year); + try + { + ret.TimeLogs = GetTimeLogList(userId, calendarWeek, now.Year); + } + catch (Exception ex) + { + _logger.Error(ex, "Error in GetTimeLogs with Id: {0}", userId); + } return ret; } @@ -169,7 +195,14 @@ namespace SQLiteRepository var calendarWeek = GetIso8601CalendarWeek(selectedDate); ret.CalendarWeek = calendarWeek; - ret.TimeLogs = GetTimeLogList(userId, calendarWeek, selectedDate.Year); + try + { + ret.TimeLogs = GetTimeLogList(userId, calendarWeek, selectedDate.Year); + } + catch (Exception ex) + { + _logger.Error(ex, "Error in GetTimeLogs with Id: {0} and selected date: {1}", userId, selectedDate); + } return ret; } @@ -400,6 +433,11 @@ namespace SQLiteRepository DayOfWeek.Monday); } + /// + /// Check whether the specified DateTime is from yesterday or older. + /// + /// the DateTime object to check is yesterday or older + /// true - is yesterday or older. private bool IsLogDateTimeYesterdayOrOlder(DateTime dt) { return dt.Date.CompareTo(DateTime.Today.Date) < 0;