diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/CardUniqueId.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/CardUniqueId.cs new file mode 100644 index 0000000..00dd7fd --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/CardUniqueId.cs @@ -0,0 +1,12 @@ +using SQLite.Net.Attributes; + +namespace SQLiteRepository +{ + public sealed class CardUniqueId + { + [PrimaryKey, AutoIncrement] + public int Id { get; set; } + public int UserId_FK { get; set; } + public string CardUId { get; set; } + } +} \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Class1.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Class1.cs deleted file mode 100644 index 870adeb..0000000 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Class1.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Interfaces; -using SQLite.Net; - -namespace SQLiteRepository -{ - public class SQLiteRepository:IRepository - { - private SQLiteConnection _connection; - public UserList GetUsers() - { - - throw new NotImplementedException(); - } - - public User GetUser(int id) - { - throw new NotImplementedException(); - } - - public TimeLogList GetTimeLogs(int userId) - { - throw new NotImplementedException(); - } - - public TimeLogList GetTimeLogs(int userId, int calendarWeek) - { - throw new NotImplementedException(); - } - - public IdentifierList GetUnassignedList() - { - throw new NotImplementedException(); - } - - public OperationResponse UpdateUser(User user) - { - throw new NotImplementedException(); - } - - public OperationResponse LogEventTime(Identifier identifier) - { - throw new NotImplementedException(); - } - } -} diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Constants.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Constants.cs new file mode 100644 index 0000000..cf0bf8a --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/Constants.cs @@ -0,0 +1,7 @@ +namespace SQLiteRepository +{ + internal class Constants + { + public const int UNASSIGNED_CARD_USER_ID = -1; + } +} \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs new file mode 100644 index 0000000..959b755 --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs @@ -0,0 +1,18 @@ +namespace SQLiteRepository +{ + internal static class SQLiteProcedures + { + public const string GET_ALL_USERS = "select * from UserIdentity"; + public const string GET_USER_BY_ID = "select * from UserIdentity where Id=?"; + + public const string GET_CARDS_BY_USER_ID = "select * from CardUniqueId where UserId_FK=?"; + public const string GET_CARDS_BY_UNIQUE_ID = "select * from CardUniqueId where CardUId=?"; + public const string GET_UNASSIGNED_CARD_LIST = "select * from CardUniqueId where UserId_FK=?"; + + public const string GET_USER_BY_FIRST_AND_LAST = + "select * from UserIdentity where FirstName = ? AND LastName = ?"; + + public const string UPDATE_CARD_USER_ID = "update CardUniqueId set UserId_FK=? where Id=?"; + + } +} \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs new file mode 100644 index 0000000..3ce17fb --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs @@ -0,0 +1,243 @@ +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(); + _connection.CreateTable(); + _connection.CreateTable(); + } + + public UserList GetUsers() + { + var ret = new UserList(); + + var users = _connection.Query(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( + 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( + SQLiteProcedures.GET_USER_BY_ID, + id); + + if (!users.Any()) return ret; + + var user = users.First(); + ret = ChangeToUserObject(user); + var cards = _connection.Query( + 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( + 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: add method which returns userId? maybe as ref? + public OperationResponse UpdateUser(User user) + { + 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.. + 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?) + foreach (var card in user.AssociatedIdentifiers) + { + var existingCard = _connection.Query( + 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; + var userQuery = _connection.Query( + SQLiteProcedures.GET_USER_BY_FIRST_AND_LAST, + user.FirstName, user.LastName); + + if (userQuery.Any()) + { + userId = userQuery.First().Id; + if (ret < OperationResponse.UPDATED) + ret = OperationResponse.UPDATED; + } + else + { + var userInsert = new UserIdentity + { + FirstName = user.FirstName, + LastName = user.LastName, + HoursPerWeek = user.HoursPerWeek + }; + _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( + SQLiteProcedures.UPDATE_CARD_USER_ID, + userId, cardId); + } + foreach (var card in cardsToRemove) + { + _connection.Query( + SQLiteProcedures.UPDATE_CARD_USER_ID, + -1, card); + } + #endregion + + return ret; + } + + public OperationResponse LogEventTime(Identifier identifier) + { + var cardIdQuery = _connection.Query( + 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: handle when more than one comes back. should NEVER happen but.... + ident = cardIdQuery.First(); + } + + var userId = ident.UserId_FK; + + //TODO: Handle In/Out Flag.. + //TODO: Handle When the identifier is assigned to a user (identifier has -1) + var timeLog = new TimeLog + { + SwipeEventDateTime = DateTime.UtcNow, + UserId_FK = userId, + IdentifierId = ident.Id + }; + + _connection.Insert(timeLog); + + return OperationResponse.SUCCESS; + } + + private User ChangeToUserObject(UserIdentity user) + { + return new User + { + UserId = user.Id, + FirstName = user.FirstName, + LastName = user.LastName, + HoursPerWeek = user.HoursPerWeek + }; + } + } +} diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj index a4d184e..e4f7672 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj @@ -34,6 +34,14 @@ ..\packages\SQLite.Net.Core-PCL.3.1.1\lib\portable-win8+net45+wp8+wpa81+MonoAndroid1+MonoTouch1\SQLite.Net.dll True + + ..\packages\SQLite.Net-PCL.3.1.1\lib\net40\SQLite.Net.Platform.Generic.dll + True + + + ..\packages\SQLite.Net-PCL.3.1.1\lib\net4\SQLite.Net.Platform.Win32.dll + True + @@ -44,8 +52,13 @@ + + + + + diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/TimeLog.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/TimeLog.cs new file mode 100644 index 0000000..50bf9a1 --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/TimeLog.cs @@ -0,0 +1,15 @@ +using System; +using SQLite.Net.Attributes; + +namespace SQLiteRepository +{ + public sealed class TimeLog + { + [PrimaryKey, AutoIncrement] + public int Id { get; set; } + public int UserId_FK { get; set; } + public int IdentifierId { get; set; } + public bool InOut { get; set; } + public DateTimeOffset SwipeEventDateTime { get; set; } + } +} \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/UserIdentity.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/UserIdentity.cs new file mode 100644 index 0000000..a65f68c --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/UserIdentity.cs @@ -0,0 +1,13 @@ +using SQLite.Net.Attributes; + +namespace SQLiteRepository +{ + public sealed class UserIdentity + { + [PrimaryKey, AutoIncrement] + public int Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public float HoursPerWeek { get; set; } + } +} \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/packages.config b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/packages.config index 0c45619..a3afb48 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/packages.config +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file