From a892e1d2c84e5b7071993eb3d88ee6aff241f544 Mon Sep 17 00:00:00 2001 From: "chris.watts90@outlook.com" Date: Mon, 30 Jan 2017 22:38:49 +0000 Subject: [PATCH] suspect service1 file incomplete due to selected line commits. Added sqlite references. added new CardsController, SwipeDataController and UsersController classes which all pull in SQLite to retrieve, or store their data. --- .../WindowsDataCenter/App.config | 45 ++++- .../WindowsDataCenter/CardData.cs | 7 + .../WindowsDataCenter/CardsController.cs | 58 ++++++ .../WindowsDataCenter/Service1.cs | 70 ++++++- .../WindowsDataCenter/SwipeDataController.cs | 59 ++++++ .../WindowsDataCenter/UsersController.cs | 183 ++++++++++++++++++ .../WindowsDataCenter.csproj | 72 ++++++- .../WindowsDataCenter/packages.config | 15 +- .../WindowsDataServiceHost/App.config | 18 +- 9 files changed, 505 insertions(+), 22 deletions(-) create mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/CardData.cs create mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/CardsController.cs create mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/SwipeDataController.cs create mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/UsersController.cs diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/App.config b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/App.config index 88fa402..689d99e 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/App.config +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/App.config @@ -1,6 +1,43 @@ - + - - - + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/CardData.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/CardData.cs new file mode 100644 index 0000000..eee79f0 --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/CardData.cs @@ -0,0 +1,7 @@ +namespace WindowsDataCenter +{ + public class CardData + { + public string CardUId { get; set; } + } +} \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/CardsController.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/CardsController.cs new file mode 100644 index 0000000..699c447 --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/CardsController.cs @@ -0,0 +1,58 @@ +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Web.Http; +using SQLite.Net; +using SQLite.Net.Platform.Win32; + +namespace WindowsDataCenter +{ + [RoutePrefix("api/cards")] + public class CardsController : ApiController + { + private const int UNASSIGNED_CARD_USER_ID = -1; + private readonly SQLiteConnection _connection; + private string path = "flexitimedb.db"; + public CardsController() + { + if (_connection == null) + { + _connection = new SQLiteConnection(new SQLitePlatformWin32(), path); + } + } + + [HttpGet] + [Route("unassigned")] + public IHttpActionResult GetUnassignedCards() + { + var cardQuery = _connection.Query( + "select * from CardUniqueId where UserId_FK=?", + UNASSIGNED_CARD_USER_ID); + + return Ok(cardQuery.Select(x => new {x.Id, x.UserId_FK, x.CardUId, IsSelected = false})); + } + + [HttpPost] + [Route("create")] + public IHttpActionResult CreateNewCardEntry([FromBody] CardUniqueId card) + { + card.UserId_FK = -1; + var affectedRows = _connection.Insert(card); + return + ResponseMessage(new HttpResponseMessage(HttpStatusCode.Created) + { + Content = new StringContent(card.Id.ToString()) + }); + } + + [HttpGet] + [Route("get")] + public IHttpActionResult GetAllCardData() + { + var cards = _connection.Query( + "select * from CardUniqueId"); + return Json(cards); + } + + } +} \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Service1.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Service1.cs index c8b91bd..4088a04 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Service1.cs +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Service1.cs @@ -4,15 +4,16 @@ using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; -using System.Net; -using System.Net.Http; using System.ServiceProcess; using System.Text; using System.Threading; using System.Threading.Tasks; -using System.Web.Http; +using System.Web; +using System.Web.Http.Dependencies; using Microsoft.Owin.Hosting; -using Owin; +using SQLite.Net; +using SQLite.Net.Attributes; +using SQLite.Net.Platform.Win32; namespace WindowsDataCenter { @@ -27,6 +28,10 @@ namespace WindowsDataCenter private bool _stopMainWorkerThread; private Thread _mainWorkerThread; + public IKernel _kernel; + + private SQLiteConnection _dbConnection; + public void Start() { OnStart(new string[] {}); @@ -39,6 +44,15 @@ namespace WindowsDataCenter protected override void OnStart(string[] args) { + _dbConnection = new SQLiteConnection(new SQLitePlatformWin32(), "flexitimedb.db"); + + //initialise ninject kernel. + NinjectHelper.GetInstance(_dbConnection); + + _dbConnection.CreateTable(); + _dbConnection.CreateTable(); + _dbConnection.CreateTable(); + _mainWorkerThread = new Thread(MainWorkerThread) { IsBackground = false, @@ -69,15 +83,61 @@ namespace WindowsDataCenter } } } + { { - } + public sealed class CardUniqueId + { + [PrimaryKey, AutoIncrement] + public int Id { get; set; } + public int UserId_FK { get; set; } + public string CardUId { get; set; } } + public sealed class UserIdentity { + [PrimaryKey, AutoIncrement] + public int Id { get; set; } + //public string CardUId { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public float HoursPerWeek { get; set; } + } + + public sealed class TimeLog + { + [PrimaryKey, AutoIncrement] + public int Id { get; set; } + public int UserId_FK { get; set; } + public bool InOut { get; set; } + public DateTimeOffset SwipeEventDateTime { get; set; } + } + + public sealed class UserObject + { + public UserObject() { + AssociatedCards = new List(); } + public int Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public float HoursPerWeek { get; set; } + public List AssociatedCards { get; set; } + } + + public sealed class UserList + { + public UserList() + { + Users = new List(); + } + + public int UserCount { get; set; } + public int PageSize { get; set; } + public int CurrentPageNumber { get; set; } + public List Users { get; set; } } { diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/SwipeDataController.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/SwipeDataController.cs new file mode 100644 index 0000000..0403697 --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/SwipeDataController.cs @@ -0,0 +1,59 @@ +using System; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Web.Http; +using System.Web.Http.Results; +using SQLite.Net; + +namespace WindowsDataCenter +{ + [RoutePrefix("api/swipedata")] + public class SwipeDataController : ApiController + { + private SQLiteConnection _connection; + + public SwipeDataController(SQLiteConnection conn) + { + _connection = conn; + } + + [HttpPost] + [Route("")] + public IHttpActionResult PostData([FromBody] CardData cData) + { + var cardIdQuery = _connection.Query( + "select * from CardUniqueIds where CardUId = ?", + cData.CardUId); + + var userId = 0; + if (!cardIdQuery.Any()) + { + //new card, create it! + userId = _connection.Insert(new CardUniqueId() + { + UserId_FK = -1, + CardUId = cData.CardUId + }); + } + else + { + //TODO: handle when more than one comes back. should NEVER happen but.... + userId = cardIdQuery.First().UserId_FK; + } + + var timeLog = new TimeLog + { + SwipeEventDateTime = DateTime.UtcNow, + UserId_FK = userId + }; + + var tLogId = _connection.Insert(timeLog); + return + ResponseMessage(new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent(tLogId.ToString()) + }); + } + } +} \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/UsersController.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/UsersController.cs new file mode 100644 index 0000000..bf79c65 --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/UsersController.cs @@ -0,0 +1,183 @@ +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Web.Http; +using SQLite.Net; +using SQLite.Net.Platform.Win32; + +namespace WindowsDataCenter +{ + [RoutePrefix("api/users")] + public class UsersController : ApiController + { + private readonly SQLiteConnection _connection; + private string path = "flexitimedb.db"; + public UsersController() + { + if (_connection == null) + { + _connection = new SQLiteConnection(new SQLitePlatformWin32(), path); + } + } + + [HttpGet] + [Route("")] + public IHttpActionResult GetUsers([FromUri] int pageSize = -1, [FromUri] int pageNumber =-1) + { + var ret = new UserList + { + PageSize = pageSize, + CurrentPageNumber = pageNumber + }; + //get the complete list of users. + var users = _connection.Query( + "select * from UserIdentity"); + + if (!users.Any()) return Ok(ret); + + ret.UserCount = users.Count; + foreach (var user in users) + { + var userObj = ChangeToUserObject(user); + var cards = _connection.Query( + "select CardUId from CardUniqueId where UserId_FK = ?", + user.Id); + foreach (var card in cards) + { + userObj.AssociatedCards.Add(new CardData { CardUId = card.CardUId }); + } + ret.Users.Add(userObj); + } + return Ok(ret); + } + + [HttpGet] + [Route("{id:int}")] + public IHttpActionResult GetUserDetails(int id) + { + var ret = new UserObject(); + + var users = _connection.Query( + "select * from UserIdentity where Id=?", + id); + + if (!users.Any()) return Ok(ret); + + var user = users.First(); + ret = ChangeToUserObject(user); + var cards = _connection.Query( + "select CardUId from CardUniqueId where UserId_FK = ?", + user.Id); + + foreach (var card in cards) + { + ret.AssociatedCards.Add(new CardData { CardUId = card.CardUId }); + } + return Ok(ret); + } + + [HttpPost] + [Route("create")] + public IHttpActionResult CreateUser([FromBody] UserObject user) + { + //var userId = _connection.Insert(user); + List cardIds = new List(); + foreach (var card in user.AssociatedCards) + { + var existingCard = _connection.Query( + "select Id from CardUniqueId where CardUId = ?", card.CardUId); + if (!existingCard.Any()) + { + var cardInsert = new CardUniqueId {CardUId = card.CardUId, UserId_FK = -1}; + _connection.Insert(cardInsert); + cardIds.Add(cardInsert.Id); + } + else + { + cardIds.Add(existingCard.First().Id); + } + } + int userId; + var userQuery = _connection.Query( + "select * from UserIdentity where FirstName = ? AND LastName = ?", + user.FirstName, user.LastName); + + if (userQuery.Any()) + { + userId = userQuery.First().Id; + } + else + { + var userInsert = new UserIdentity + { + FirstName = user.FirstName, + LastName = user.LastName, + HoursPerWeek = user.HoursPerWeek + }; + _connection.Insert(userInsert); + userId = userInsert.Id; + } + foreach (var cardId in cardIds) + { + _connection.Query( + "update CardUniqueId set UserId_FK=? where Id=?", + userId, cardId); + } + return ResponseMessage(new HttpResponseMessage(HttpStatusCode.OK) {Content = new StringContent(userId.ToString())}); + } + + [HttpPost] + [Route("edit")] + public IHttpActionResult EditUser([FromBody] UserObject user) + { + var users = _connection.Query( + "select * from UserIdentity where Id = ?", + user.Id); + + int userId; + if (!users.Any()) + { + //create the new user in the DB. + var userI = ChangeToUserIdentity(user); + userId = _connection.Insert(userI); + } + else + { + userId = users.First().Id; + _connection.Query( + "update UserIdentity set FirstName=?, LastName=?, HoursPerWeek=? where Id=?", + user.FirstName, user.LastName, user.HoursPerWeek, user.Id); + } + + return + ResponseMessage(new HttpResponseMessage(HttpStatusCode.Created) + { + Content = new StringContent(userId.ToString()) + }); + } + + private UserIdentity ChangeToUserIdentity(UserObject user) + { + return new UserIdentity + { + Id = user.Id, + FirstName = user.FirstName, + LastName = user.LastName, + HoursPerWeek = user.HoursPerWeek + }; + } + + private UserObject ChangeToUserObject(UserIdentity user) + { + return new UserObject + { + Id = user.Id, + FirstName = user.FirstName, + LastName = user.LastName, + HoursPerWeek = user.HoursPerWeek + }; + } + } + //TODO: desperately need to create a Repository/Unit Of Work to handle the db stuff. +} \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj index e3aad17..e6a2aff 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj @@ -12,6 +12,8 @@ v4.5.2 512 true + + AnyCPU @@ -33,32 +35,68 @@ 4 - - ..\packages\Microsoft.Owin.2.0.2\lib\net45\Microsoft.Owin.dll + + ..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll True - - ..\packages\Microsoft.Owin.Host.HttpListener.2.0.2\lib\net45\Microsoft.Owin.Host.HttpListener.dll + + ..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll True - - ..\packages\Microsoft.Owin.Hosting.2.0.2\lib\net45\Microsoft.Owin.Hosting.dll + + ..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll + True + + + ..\packages\Microsoft.Owin.Host.HttpListener.3.0.1\lib\net45\Microsoft.Owin.Host.HttpListener.dll + True + + + ..\packages\Microsoft.Owin.Hosting.3.0.1\lib\net45\Microsoft.Owin.Hosting.dll + True + + + ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll True - - ..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll True ..\packages\Owin.1.0\lib\net40\Owin.dll True + + ..\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 + + + + ..\packages\System.Data.SQLite.Core.1.0.104.0\lib\net451\System.Data.SQLite.dll + True + + + ..\packages\System.Data.SQLite.EF6.1.0.104.0\lib\net451\System.Data.SQLite.EF6.dll + True + + + ..\packages\System.Data.SQLite.Linq.1.0.104.0\lib\net451\System.Data.SQLite.Linq.dll + True + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll True + ..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll True @@ -76,6 +114,9 @@ + + + @@ -86,12 +127,27 @@ + + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + +