returned user id in user edit started working on the direction/logtime method
323 lines
11 KiB
C#
323 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Interfaces;
|
|
using SQLite.Net;
|
|
using SQLite.Net.Platform.Win32;
|
|
|
|
namespace SQLiteRepository
|
|
{
|
|
public class SQLiteRepository : IRepository
|
|
{
|
|
private readonly SQLiteConnection _connection;
|
|
private string _path = "flexitimedb.db";
|
|
|
|
public SQLiteRepository()
|
|
{
|
|
_connection = new SQLiteConnection(new SQLitePlatformWin32(), _path);
|
|
|
|
_connection.CreateTable<CardUniqueId>();
|
|
_connection.CreateTable<UserIdentity>();
|
|
_connection.CreateTable<TimeLog>();
|
|
}
|
|
|
|
public UserList GetUsers()
|
|
{
|
|
var ret = new UserList();
|
|
|
|
var users = _connection.Query<UserIdentity>(SQLiteProcedures.GET_ALL_USERS);
|
|
|
|
if (!users.Any())
|
|
{
|
|
ret.PageNumber = 1;
|
|
ret.PageSize = 20;
|
|
return ret;
|
|
}
|
|
|
|
foreach (var user in users)
|
|
{
|
|
var userObj = ChangeToUserObject(user);
|
|
var cards = _connection.Query<CardUniqueId>(
|
|
SQLiteProcedures.GET_CARDS_BY_USER_ID,
|
|
user.Id);
|
|
|
|
foreach (var card in cards)
|
|
{
|
|
userObj.AssociatedIdentifiers.Add(new Identifier()
|
|
{
|
|
UniqueId = card.CardUId,
|
|
IsAssociatedToUser = true,
|
|
Id = card.Id
|
|
});
|
|
}
|
|
ret.Users.Add(userObj);
|
|
}
|
|
ret.PageSize = 20;
|
|
ret.PageNumber = 1;
|
|
|
|
return ret;
|
|
}
|
|
|
|
public UserList Search(string searchParam)
|
|
{
|
|
var ret = new UserList();
|
|
searchParam = string.Format("%{0}%", searchParam);
|
|
var users = _connection.Query<UserIdentity>(
|
|
SQLiteProcedures.SEARCH_USER_LIST,
|
|
searchParam, searchParam);
|
|
|
|
if (!users.Any())
|
|
{
|
|
ret.PageNumber = 1;
|
|
ret.PageSize = 20;
|
|
return ret;
|
|
}
|
|
|
|
foreach (var user in users)
|
|
{
|
|
var userObj = ChangeToUserObject(user);
|
|
var cards = _connection.Query<CardUniqueId>(
|
|
SQLiteProcedures.GET_CARDS_BY_USER_ID,
|
|
user.Id);
|
|
|
|
foreach (var card in cards)
|
|
{
|
|
userObj.AssociatedIdentifiers.Add(new Identifier()
|
|
{
|
|
UniqueId = card.CardUId,
|
|
IsAssociatedToUser = true,
|
|
Id = card.Id
|
|
});
|
|
}
|
|
ret.Users.Add(userObj);
|
|
}
|
|
ret.PageSize = 20;
|
|
ret.PageNumber = 1;
|
|
|
|
return ret;
|
|
}
|
|
|
|
public User GetUser(int id)
|
|
{
|
|
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)
|
|
{
|
|
ret.AssociatedIdentifiers.Add(new Identifier { UniqueId = card.CardUId, IsAssociatedToUser = true, Id = card.Id});
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
public TimeLogList GetTimeLogs(int userId)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public TimeLogList GetTimeLogs(int userId, int calendarWeek)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public IdentifierList GetUnassignedIdentifierList()
|
|
{
|
|
var ret = new IdentifierList();
|
|
var cardQuery = _connection.Query<CardUniqueId>(
|
|
SQLiteProcedures.GET_UNASSIGNED_CARD_LIST,
|
|
Constants.UNASSIGNED_CARD_USER_ID);
|
|
|
|
foreach (var card in cardQuery)
|
|
{
|
|
ret.data.Add(new Identifier
|
|
{
|
|
Id = card.Id,
|
|
IsAssociatedToUser = card.UserId_FK != Constants.UNASSIGNED_CARD_USER_ID,
|
|
UniqueId = card.CardUId
|
|
});
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
//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<int>();
|
|
|
|
//Get a list of current associated identifiers, convert into a list of Identifier Objects..
|
|
var currentCards =
|
|
_connection.Query<CardUniqueId>(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
|
|
foreach (var card in user.AssociatedIdentifiers)
|
|
{
|
|
var existingCard = _connection.Query<CardUniqueId>(
|
|
SQLiteProcedures.GET_CARDS_BY_UNIQUE_ID, card.UniqueId);
|
|
|
|
if (!existingCard.Any())
|
|
{
|
|
var cardInsert = new CardUniqueId { CardUId = card.UniqueId, UserId_FK = -1 };
|
|
_connection.Insert(cardInsert);
|
|
cardIds.Add(cardInsert.Id);
|
|
if (ret < OperationResponse.CREATED)
|
|
ret = OperationResponse.CREATED; //only change it if my status supercedes.
|
|
}
|
|
else
|
|
{
|
|
cardIds.Add(existingCard.First().Id);
|
|
if (ret < OperationResponse.UPDATED)
|
|
ret = OperationResponse.UPDATED;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Update/Create User
|
|
int userId;
|
|
|
|
if (user.UserId != -1)
|
|
{
|
|
//edit..
|
|
_connection.Query<UserIdentity>(
|
|
"update UserIdentity set FirstName=?, LastName=?, HoursPerWeek=?,IsContractor=? where Id=?",
|
|
user.FirstName,
|
|
user.LastName,
|
|
user.HoursPerWeek,
|
|
user.IsContractor,
|
|
user.UserId
|
|
);
|
|
userId = user.UserId;
|
|
}
|
|
else
|
|
{
|
|
var userInsert = new UserIdentity
|
|
{
|
|
FirstName = user.FirstName,
|
|
LastName = user.LastName,
|
|
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.
|
|
foreach (var cardId in cardIds)
|
|
{
|
|
_connection.Query<CardUniqueId>(
|
|
SQLiteProcedures.UPDATE_CARD_USER_ID,
|
|
userId, cardId);
|
|
}
|
|
foreach (var card in cardsToRemove)
|
|
{
|
|
_connection.Query<CardUniqueId>(
|
|
SQLiteProcedures.UPDATE_CARD_USER_ID,
|
|
-1, card);
|
|
}
|
|
#endregion
|
|
|
|
userIdResult = userId;
|
|
return ret;
|
|
}
|
|
|
|
public OperationResponse LogEventTime(Identifier identifier)
|
|
{
|
|
var cardIdQuery = _connection.Query<CardUniqueId>(
|
|
SQLiteProcedures.GET_CARDS_BY_UNIQUE_ID,
|
|
identifier.UniqueId);
|
|
|
|
var ident = new CardUniqueId();
|
|
if (!cardIdQuery.Any())
|
|
{
|
|
//new card, create it!
|
|
ident.UserId_FK = -1;
|
|
ident.CardUId = identifier.UniqueId;
|
|
_connection.Insert(ident);
|
|
}
|
|
else
|
|
{
|
|
//TODO: log when more than one comes back. should NEVER happen but....
|
|
ident = cardIdQuery.First();
|
|
}
|
|
//TODO: change log direction to an ENUM
|
|
//TODO: Handle In/Out Flag..
|
|
var logDirection = false;
|
|
//get the last flag
|
|
if (ident.UserId_FK != -1)
|
|
{
|
|
var lastEntry = _connection.Query<TimeLog>(
|
|
SQLiteProcedures.GET_LAST_TIMELOG_DIRECTION,
|
|
ident.UserId_FK);
|
|
if (lastEntry.Any())
|
|
{
|
|
var lastLog = lastEntry.First();
|
|
// See if the datetime retrieved is yesterday. If yesterday, logDirection = true (in)
|
|
if (IsLogDateTimeYesterdayOrOlder(lastLog.SwipeEventDateTime.DateTime))
|
|
{
|
|
logDirection = true;
|
|
}
|
|
else
|
|
{
|
|
logDirection = !lastLog.InOut;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
logDirection = false; //TODO: need an "unknown" state..
|
|
}
|
|
//SQLiteProcedures.GET_LAST_TIMELOG_DIRECTION
|
|
//then invert it, and store it..
|
|
|
|
//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 = ident.UserId_FK,
|
|
IdentifierId = ident.Id,
|
|
InOut = logDirection
|
|
};
|
|
|
|
_connection.Insert(timeLog);
|
|
|
|
return OperationResponse.SUCCESS;
|
|
}
|
|
|
|
private bool IsLogDateTimeYesterdayOrOlder(DateTime dt)
|
|
{
|
|
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
|
|
};
|
|
}
|
|
}
|
|
}
|