From 1e8d56ab82eb049566532ebe7e54ec71ac1fe1a6 Mon Sep 17 00:00:00 2001 From: "chris.watts90@outlook.com" Date: Tue, 31 Jan 2017 22:09:40 +0000 Subject: [PATCH] csproj update to reference Interfaces and SQLiteRepository implementation. Complete IRepository migration into the ApiControllers. remove any nuget packages from the packages.config which refer to sqlite and entity framework. missing csproj update for SQLiteRepository project. Sorry.. --- .../SQLiteRepository/SQLiteRepository.csproj | 2 +- .../WindowsDataCenter/App.config | 9 +- .../WindowsDataCenter/CardsController.cs | 51 ++---- .../WindowsDataCenter/Service1.cs | 65 ------- .../WindowsDataCenter/SwipeDataController.cs | 66 ++++---- .../WindowsDataCenter/UsersController.cs | 158 ++---------------- .../WindowsDataCenter.csproj | 49 ++---- .../WindowsDataCenter/packages.config | 7 - 8 files changed, 75 insertions(+), 332 deletions(-) diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj index e4f7672..00858cc 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj @@ -53,7 +53,7 @@ - + diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/App.config b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/App.config index 689d99e..cf33c40 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/App.config +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/App.config @@ -1,7 +1,7 @@  - +
@@ -34,10 +34,5 @@ - - - - - - + \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/CardsController.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/CardsController.cs index 699c447..b8094d0 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/CardsController.cs +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/CardsController.cs @@ -1,58 +1,27 @@ -using System.Linq; -using System.Net; -using System.Net.Http; +using System; using System.Web.Http; -using SQLite.Net; -using SQLite.Net.Platform.Win32; +using Interfaces; 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() + private readonly IRepository _repo; + public CardsController(IRepository repo) { - if (_connection == null) - { - _connection = new SQLiteConnection(new SQLitePlatformWin32(), path); - } + if(repo == null) throw new ArgumentNullException(nameof(repo)); + + _repo = repo; } [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})); + var unassignedCards = _repo.GetUnassignedIdentifierList(); + + return Ok(unassignedCards); } - - [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 f68b97e..f9fac59 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Service1.cs +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Service1.cs @@ -14,9 +14,6 @@ using Microsoft.Owin.Hosting; using Ninject; using Ninject.Syntax; using Ninject.Web.Common; -using SQLite.Net; -using SQLite.Net.Attributes; -using SQLite.Net.Platform.Win32; namespace WindowsDataCenter { @@ -30,10 +27,6 @@ namespace WindowsDataCenter private IDisposable _webApp; private bool _stopMainWorkerThread; private Thread _mainWorkerThread; - - public IKernel _kernel; - - private SQLiteConnection _dbConnection; public void Start() { @@ -47,14 +40,10 @@ 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) { @@ -86,58 +75,4 @@ 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 index 0403697..5bf1d76 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/SwipeDataController.cs +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/SwipeDataController.cs @@ -1,58 +1,60 @@ using System; -using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; -using System.Web.Http.Results; -using SQLite.Net; +using Interfaces; namespace WindowsDataCenter { [RoutePrefix("api/swipedata")] public class SwipeDataController : ApiController { - private SQLiteConnection _connection; - - public SwipeDataController(SQLiteConnection conn) + //private SQLiteConnection _connection; + private readonly IRepository _repo; + public SwipeDataController(IRepository repo) { - _connection = conn; + if(repo == null) throw new ArgumentNullException(); + _repo = repo; + //_connection = conn; } [HttpPost] [Route("")] public IHttpActionResult PostData([FromBody] CardData cData) { - var cardIdQuery = _connection.Query( - "select * from CardUniqueIds where CardUId = ?", - cData.CardUId); + //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 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 timeLog = new TimeLog + //{ + // SwipeEventDateTime = DateTime.UtcNow, + // UserId_FK = userId + //}; - var tLogId = _connection.Insert(timeLog); + //var tLogId = _connection.Insert(timeLog); + //TODO: TEST + _repo.LogEventTime(new Identifier {UniqueId = cData.CardUId}); return ResponseMessage(new HttpResponseMessage(HttpStatusCode.OK) { - Content = new StringContent(tLogId.ToString()) + Content = new StringContent("TODO: return ID") }); } } diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/UsersController.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/UsersController.cs index bf79c65..472d8eb 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/UsersController.cs +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/UsersController.cs @@ -1,183 +1,61 @@ -using System.Collections.Generic; -using System.Linq; +using System; using System.Net; using System.Net.Http; using System.Web.Http; -using SQLite.Net; -using SQLite.Net.Platform.Win32; +using Interfaces; namespace WindowsDataCenter { [RoutePrefix("api/users")] public class UsersController : ApiController { - private readonly SQLiteConnection _connection; - private string path = "flexitimedb.db"; - public UsersController() + private readonly IRepository _repo; + + public UsersController(IRepository repo) { - if (_connection == null) - { - _connection = new SQLiteConnection(new SQLitePlatformWin32(), path); - } + if(repo == null) { throw new ArgumentNullException(nameof(repo));} + _repo = repo; } [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"); + var userList = _repo.GetUsers(); - if (!users.Any()) return Ok(ret); + var msg = Request.CreateResponse(HttpStatusCode.OK, userList); - 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); + return ResponseMessage(msg); } [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 }); - } + var ret = _repo.GetUser(id); + return Ok(ret); } [HttpPost] [Route("create")] - public IHttpActionResult CreateUser([FromBody] UserObject user) + public IHttpActionResult CreateUser([FromBody] User 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())}); + _repo.UpdateUser(user); + return ResponseMessage(new HttpResponseMessage(HttpStatusCode.OK) {Content = new StringContent("unknownIdTODO")}); } [HttpPost] [Route("edit")] - public IHttpActionResult EditUser([FromBody] UserObject user) + public IHttpActionResult EditUser([FromBody] User 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); - } + _repo.UpdateUser(user); return ResponseMessage(new HttpResponseMessage(HttpStatusCode.Created) { - Content = new StringContent(userId.ToString()) + Content = new StringContent("TODO:return UserID") }); } - - 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 5e76f42..9527a4e 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj @@ -35,14 +35,6 @@ 4 - - ..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll - True - - - ..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll - True - ..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll True @@ -75,33 +67,9 @@ ..\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 @@ -154,14 +122,17 @@ + + + {B7347B72-E208-423A-9D99-723B558EA3D7} + Interfaces + + + {B3510C81-F069-48A2-B826-EBE0CE7AB0B2} + SQLiteRepository + + - - - - 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}. - - -