From f871c2363173f3a2e688b57731c7977cb0fc46ed Mon Sep 17 00:00:00 2001 From: "Chris.Watts90@outlook.com" Date: Mon, 4 Jun 2018 11:07:29 +0100 Subject: [PATCH] add converters to help convert from/to data transfer objects. removed conversion code from SQLiteRepository.cs in favour of new converters. pulled out string queries from SQLiteRepository and moved to SQLiteProcedures.cs. #95 --- .../Converters/LogDirectionConverter.cs | 51 ++++++++++++ .../Converters/LogSourceConverter.cs | 37 +++++++++ .../Converters/TimeLogConverter.cs | 80 ------------------- .../SQLiteRepository/SQLiteProcedures.cs | 2 + .../SQLiteRepository/SQLiteRepository.cs | 37 +++------ .../SQLiteRepository/SQLiteRepository.csproj | 4 + .../WindowsDataCenter.csproj | 1 + 7 files changed, 104 insertions(+), 108 deletions(-) create mode 100644 DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Converters/LogDirectionConverter.cs create mode 100644 DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Converters/LogSourceConverter.cs diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Converters/LogDirectionConverter.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Converters/LogDirectionConverter.cs new file mode 100644 index 0000000..72a536e --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Converters/LogDirectionConverter.cs @@ -0,0 +1,51 @@ +using Interfaces; + +namespace SQLiteRepository.Converters +{ + static class LogDirectionConverter + { + public static LogDirection ConvertToLogDirectionDto(LogDirectionDb direction) + { + switch (direction) + { + case LogDirectionDb.IN: + return LogDirection.IN; + case LogDirectionDb.OUT: + return LogDirection.OUT; + default: + return LogDirection.UNKNOWN; + } + } + + public static LogDirectionDb ConvertFromLogDirectionDto(LogDirection direction) + { + switch (direction) + { + case LogDirection.IN: + return LogDirectionDb.IN; + case LogDirection.OUT: + return LogDirectionDb.OUT; + default: + return LogDirectionDb.UNKNOWN; + } + } + + public static LogDirectionDb InvertLogDirectionDb(LogDirectionDb direction) + { + return (LogDirectionDb)(int)InvertLogDirection((LogDirection)(int)direction); + } + + public static LogDirection InvertLogDirection(LogDirection direction) + { + switch (direction) + { + case LogDirection.IN: + return LogDirection.OUT; + case LogDirection.OUT: + return LogDirection.IN; + default: + return LogDirection.UNKNOWN; + } + } + } +} \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Converters/LogSourceConverter.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Converters/LogSourceConverter.cs new file mode 100644 index 0000000..6ebc7c1 --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Converters/LogSourceConverter.cs @@ -0,0 +1,37 @@ +using Interfaces; + +namespace SQLiteRepository.Converters +{ + static class LogSourceConverter + { + public static LogSource ConvertToLogSourceDto(LogSourceDb source) + { + switch (source) + { + case LogSourceDb.IDENTIFIER: + return LogSource.IDENTIFIER; + case LogSourceDb.TRAYAPP: + return LogSource.TRAYAPP; + case LogSourceDb.UI: + return LogSource.UI; + default: + return LogSource.UNKNOWN; + } + } + + public static LogSourceDb ConvertFromLogSourceDto(LogSource source) + { + switch (source) + { + case LogSource.IDENTIFIER: + return LogSourceDb.IDENTIFIER; + case LogSource.TRAYAPP: + return LogSourceDb.TRAYAPP; + case LogSource.UI: + return LogSourceDb.UI; + default: + return LogSourceDb.UNKNOWN; + } + } + } +} \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Converters/TimeLogConverter.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Converters/TimeLogConverter.cs index 3669272..a118836 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Converters/TimeLogConverter.cs +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Converters/TimeLogConverter.cs @@ -39,84 +39,4 @@ namespace SQLiteRepository.Converters }; } } - - static class LogSourceConverter - { - public static LogSource ConvertToLogSourceDto(LogSourceDb source) - { - switch (source) - { - case LogSourceDb.IDENTIFIER: - return LogSource.IDENTIFIER; - case LogSourceDb.TRAYAPP: - return LogSource.TRAYAPP; - case LogSourceDb.UI: - return LogSource.UI; - default: - return LogSource.UNKNOWN; - } - } - - public static LogSourceDb ConvertFromLogSourceDto(LogSource source) - { - switch (source) - { - case LogSource.IDENTIFIER: - return LogSourceDb.IDENTIFIER; - case LogSource.TRAYAPP: - return LogSourceDb.TRAYAPP; - case LogSource.UI: - return LogSourceDb.UI; - default: - return LogSourceDb.UNKNOWN; - } - } - } - - static class LogDirectionConverter - { - public static LogDirection ConvertToLogDirectionDto(LogDirectionDb direction) - { - switch (direction) - { - case LogDirectionDb.IN: - return LogDirection.IN; - case LogDirectionDb.OUT: - return LogDirection.OUT; - default: - return LogDirection.UNKNOWN; - } - } - - public static LogDirectionDb ConvertFromLogDirectionDto(LogDirection direction) - { - switch (direction) - { - case LogDirection.IN: - return LogDirectionDb.IN; - case LogDirection.OUT: - return LogDirectionDb.OUT; - default: - return LogDirectionDb.UNKNOWN; - } - } - - public static LogDirectionDb InvertLogDirectionDb(LogDirectionDb direction) - { - return (LogDirectionDb)(int)InvertLogDirection((LogDirection)(int)direction); - } - - public static LogDirection InvertLogDirection(LogDirection direction) - { - switch (direction) - { - case LogDirection.IN: - return LogDirection.OUT; - case LogDirection.OUT: - return LogDirection.IN; - default: - return LogDirection.UNKNOWN; - } - } - } } diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs index a8a08a9..c6d0e80 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs @@ -126,5 +126,7 @@ namespace SQLiteRepository + nameof(TimeLogDb.Year) + "=?," + nameof(TimeLogDb.Source) + "=? " + "where " + nameof(TimeLogDb.Id) + "=?"; + + public const string GET_DB_VERSION = "select * from DbVersion"; } } \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs index 9d32477..9fedbd3 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs @@ -515,20 +515,12 @@ namespace SQLiteRepository public OperationResponse CreateLog(TimeLog log) { - var calendarWeek = GetIso8601CalendarWeek(log.EventTime.UtcDateTime); - var year = log.EventTime.Year; - log.CalendarWeek = calendarWeek; - log.Year = year; - var dbLog = new TimeLogDb - { - SwipeEventDateTime = log.EventTime, - Direction = (LogDirectionDb)(int)log.Direction, - Year = year, - CalendarWeek = calendarWeek, - Source = (LogSourceDb)(int)log.Source, - UserId_FK = log.UserId, - IdentifierId = ConvertSourceToIdentifierId(log.Source), - }; + log.CalendarWeek = GetIso8601CalendarWeek(log.EventTime.UtcDateTime); + log.Year= log.EventTime.Year; + + var dbLog = TimeLogConverter.ConvertFromTimeLogDto(log); + dbLog.IdentifierId = ConvertSourceToIdentifierId(log.Source); + #region update in/out directions for manual logs. UpdateExistingLogDirections(log); #endregion @@ -541,6 +533,7 @@ namespace SQLiteRepository { var query = _connection.Query( SQLiteProcedures.GET_TIMELOG_ENTRY, log.Id); + if(!query.Any()) return OperationResponse.FAILED; @@ -562,7 +555,7 @@ namespace SQLiteRepository private void CheckForDbUpgrade() { - var data = _connection.Query("select * from DbVersion"); + var data = _connection.Query(SQLiteProcedures.GET_DB_VERSION); if (!data.Any()) { //Pre-Upgrade database, need upgrading @@ -813,7 +806,7 @@ namespace SQLiteRepository private void UpdateIdentifierLastUsed(DateTimeOffset dt, int cardId) { - var res = _connection.ExecuteScalar(SQLiteProcedures.UPDATE_CARD_LAST_USED, + _connection.ExecuteScalar(SQLiteProcedures.UPDATE_CARD_LAST_USED, dt, cardId); } @@ -862,18 +855,6 @@ namespace SQLiteRepository return dt.Date.CompareTo(DateTime.Today.Date) < 0; } - private User ChangeToUserObject(UserIdentity user) - { - return new User - { - UserId = user.Id, - FirstName = user.FirstName, - LastName = user.LastName, - HoursPerWeek = user.HoursPerWeek, - IsContractor = user.IsContractor - }; - } - private void UpdateExistingLogDirections(TimeLog log) { //need to make this generic so that both create and delete will update the log directions.. but ARGH. diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj index 46517fc..a666342 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj @@ -78,6 +78,10 @@ + + + + diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj index 4dee973..e18caab 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj @@ -55,6 +55,7 @@ prompt MinimumRecommendedRules.ruleset true + bin\ReleaseInstallers\WindowsDataCenter.XML