diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs index 1ed3431..e04765c 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs @@ -15,5 +15,7 @@ namespace SQLiteRepository public const string UPDATE_CARD_USER_ID = "update CardUniqueId set UserId_FK=? where Id=?"; public const string SEARCH_USER_LIST = "SELECT * FROM[UserIdentity] where(FirstName Like ? OR LastName Like ?)"; + + public const string GET_LAST_TIMELOG_DIRECTION = "SELECT * FROM TimeLog where UserId_FK = ? order by SwipeEventDateTime LIMIT 1"; } } \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs index 8dd9c76..d58816e 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs @@ -149,22 +149,22 @@ namespace SQLiteRepository } return ret; } - - //TODO: add method which returns userId? maybe as ref? - public OperationResponse UpdateUser(User user) + + //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) { //if(user.UserId <=0) return OperationResponse.FAILED; var ret = OperationResponse.NONE; var cardIds = new List(); - //Get a list of current cards, convert into a list of Identifier Objects.. + //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(); - #region GetUnique Identifiers (what am i doing here?) + #region GetUnique Identifiers foreach (var card in user.AssociatedIdentifiers) { var existingCard = _connection.Query( @@ -189,15 +189,19 @@ namespace SQLiteRepository #region Update/Create User int userId; - var userQuery = _connection.Query( - SQLiteProcedures.GET_USER_BY_FIRST_AND_LAST, - user.FirstName, user.LastName); - if (userQuery.Any()) + if (user.UserId != -1) { - userId = userQuery.First().Id; - if (ret < OperationResponse.UPDATED) - ret = OperationResponse.UPDATED; + //edit.. + _connection.Query( + "update UserIdentity set FirstName=?, LastName=?, HoursPerWeek=?,IsContractor=? where Id=?", + user.FirstName, + user.LastName, + user.HoursPerWeek, + user.IsContractor, + user.UserId + ); + userId = user.UserId; } else { @@ -205,13 +209,14 @@ namespace SQLiteRepository { FirstName = user.FirstName, LastName = user.LastName, - HoursPerWeek = user.HoursPerWeek + HoursPerWeek = user.HoursPerWeek, + IsContractor = user.IsContractor }; _connection.Insert(userInsert); userId = userInsert.Id; if (ret < OperationResponse.CREATED) ret = OperationResponse.CREATED; - } + } #endregion #region Update Card/Unique Id entries. @@ -229,6 +234,7 @@ namespace SQLiteRepository } #endregion + userIdResult = userId; return ret; } @@ -251,16 +257,22 @@ namespace SQLiteRepository //TODO: handle when more than one comes back. should NEVER happen but.... ident = cardIdQuery.First(); } - - var userId = ident.UserId_FK; - + //TODO: Handle In/Out Flag.. + //get the last flag + //SQLiteProcedures.GET_LAST_TIMELOG_DIRECTION + //then invert it, and store it.. + var logDirection = false; + //TODO: See if the datetime retrieved is yesterday. If yesterday, logDirection = true (in) + //TODO: Handle When the identifier is assigned to a user (identifier has -1) + //when identifier not assigned to user, just store it anyway and carry on, can update later. var timeLog = new TimeLog { SwipeEventDateTime = DateTime.UtcNow, - UserId_FK = userId, - IdentifierId = ident.Id + UserId_FK = ident.UserId_FK, + IdentifierId = ident.Id, + InOut = logDirection }; _connection.Insert(timeLog);