added some logging to SQLiteRepository.

#9
This commit is contained in:
Chris.Watts90@outlook.com 2017-02-13 16:57:18 +00:00
parent 00a6894197
commit 36282ee355

View File

@ -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<CardUniqueId>();
_connection.CreateTable<UserIdentity>();
_connection.CreateTable<TimeLogDb>();
_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<UserIdentity>(
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<UserIdentity>(
SQLiteProcedures.GET_USER_BY_ID,
id);
if (!users.Any()) return ret;
var user = users.First();
ret = ChangeToUserObject(user);
var cards = _connection.Query<CardUniqueId>(
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<UserIdentity>(
SQLiteProcedures.GET_USER_BY_ID,
id);
if (!users.Any()) return ret;
var user = users.First();
ret = ChangeToUserObject(user);
var cards = _connection.Query<CardUniqueId>(
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);
}
/// <summary>
/// Check whether the specified DateTime is from yesterday or older.
/// </summary>
/// <param name="dt">the DateTime object to check is yesterday or older</param>
/// <returns>true - is yesterday or older.</returns>
private bool IsLogDateTimeYesterdayOrOlder(DateTime dt)
{
return dt.Date.CompareTo(DateTime.Today.Date) < 0;