fixed update/create where HoursPerWeek/IsContractor info wasnt being updated/changed.

return UserId using the Out Parameter
Added GetLastTimeLog procedure. Added Comments to help figure out procedure steps later on.
This commit is contained in:
Chris.Watts90@outlook.com 2017-02-07 17:23:04 +00:00
parent 29cc798097
commit ea6fc3721d
2 changed files with 33 additions and 19 deletions

View File

@ -15,5 +15,7 @@ namespace SQLiteRepository
public const string UPDATE_CARD_USER_ID = "update CardUniqueId set UserId_FK=? where Id=?"; 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 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";
} }
} }

View File

@ -150,21 +150,21 @@ namespace SQLiteRepository
return ret; return ret;
} }
//TODO: add method which returns userId? maybe as ref? //TODO: Check time logs table on update to ensure associated cards/unique identifiers are removed/added as appropriate.
public OperationResponse UpdateUser(User user) public OperationResponse UpdateUser(User user, out int userIdResult)
{ {
//if(user.UserId <=0) return OperationResponse.FAILED; //if(user.UserId <=0) return OperationResponse.FAILED;
var ret = OperationResponse.NONE; var ret = OperationResponse.NONE;
var cardIds = new List<int>(); var cardIds = new List<int>();
//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 = var currentCards =
_connection.Query<CardUniqueId>(SQLiteProcedures.GET_CARDS_BY_USER_ID, user.UserId) _connection.Query<CardUniqueId>(SQLiteProcedures.GET_CARDS_BY_USER_ID, user.UserId)
.Select(x => new Identifier {Id = x.Id, IsAssociatedToUser = true, UniqueId = x.CardUId}); .Select(x => new Identifier {Id = x.Id, IsAssociatedToUser = true, UniqueId = x.CardUId});
var cardsToRemove = currentCards.Except(user.AssociatedIdentifiers).Select(x=>x.Id).ToList(); 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) foreach (var card in user.AssociatedIdentifiers)
{ {
var existingCard = _connection.Query<CardUniqueId>( var existingCard = _connection.Query<CardUniqueId>(
@ -189,15 +189,19 @@ namespace SQLiteRepository
#region Update/Create User #region Update/Create User
int userId; int userId;
var userQuery = _connection.Query<UserIdentity>(
SQLiteProcedures.GET_USER_BY_FIRST_AND_LAST,
user.FirstName, user.LastName);
if (userQuery.Any()) if (user.UserId != -1)
{ {
userId = userQuery.First().Id; //edit..
if (ret < OperationResponse.UPDATED) _connection.Query<UserIdentity>(
ret = OperationResponse.UPDATED; "update UserIdentity set FirstName=?, LastName=?, HoursPerWeek=?,IsContractor=? where Id=?",
user.FirstName,
user.LastName,
user.HoursPerWeek,
user.IsContractor,
user.UserId
);
userId = user.UserId;
} }
else else
{ {
@ -205,7 +209,8 @@ namespace SQLiteRepository
{ {
FirstName = user.FirstName, FirstName = user.FirstName,
LastName = user.LastName, LastName = user.LastName,
HoursPerWeek = user.HoursPerWeek HoursPerWeek = user.HoursPerWeek,
IsContractor = user.IsContractor
}; };
_connection.Insert(userInsert); _connection.Insert(userInsert);
userId = userInsert.Id; userId = userInsert.Id;
@ -229,6 +234,7 @@ namespace SQLiteRepository
} }
#endregion #endregion
userIdResult = userId;
return ret; return ret;
} }
@ -252,15 +258,21 @@ namespace SQLiteRepository
ident = cardIdQuery.First(); ident = cardIdQuery.First();
} }
var userId = ident.UserId_FK;
//TODO: Handle In/Out Flag.. //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) //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 var timeLog = new TimeLog
{ {
SwipeEventDateTime = DateTime.UtcNow, SwipeEventDateTime = DateTime.UtcNow,
UserId_FK = userId, UserId_FK = ident.UserId_FK,
IdentifierId = ident.Id IdentifierId = ident.Id,
InOut = logDirection
}; };
_connection.Insert(timeLog); _connection.Insert(timeLog);