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..
This commit is contained in:
chris.watts90@outlook.com 2017-01-31 22:09:40 +00:00
parent dd8d91279f
commit 1e8d56ab82
8 changed files with 75 additions and 332 deletions

View File

@ -53,7 +53,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CardUniqueId.cs" />
<Compile Include="Class1.cs" />
<Compile Include="SQLiteRepository.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SQLiteProcedures.cs" />

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
@ -34,10 +34,5 @@
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
<remove invariant="System.Data.SQLite" /><add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /></DbProviderFactories>
</system.data>
</configuration>

View File

@ -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<CardUniqueId>(
"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<CardUniqueId>(
"select * from CardUniqueId");
return Json(cards);
}
}
}

View File

@ -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<CardUniqueId>();
_dbConnection.CreateTable<UserIdentity>();
_dbConnection.CreateTable<TimeLog>();
_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<CardData>();
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public float HoursPerWeek { get; set; }
public List<CardData> AssociatedCards { get; set; }
}
public sealed class UserList
{
public UserList()
{
Users = new List<UserObject>();
}
public int UserCount { get; set; }
public int PageSize { get; set; }
public int CurrentPageNumber { get; set; }
public List<UserObject> Users { get; set; }
}
}

View File

@ -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<CardUniqueId>(
"select * from CardUniqueIds where CardUId = ?",
cData.CardUId);
//var cardIdQuery = _connection.Query<CardUniqueId>(
// "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")
});
}
}

View File

@ -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<UserIdentity>(
"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<CardUniqueId>(
"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<UserIdentity>(
"select * from UserIdentity where Id=?",
id);
if (!users.Any()) return Ok(ret);
var user = users.First();
ret = ChangeToUserObject(user);
var cards = _connection.Query<CardUniqueId>(
"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<int> cardIds = new List<int>();
foreach (var card in user.AssociatedCards)
{
var existingCard = _connection.Query<CardUniqueId>(
"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<UserIdentity>(
"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<CardUniqueId>(
"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<UserIdentity>(
"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<UserIdentity>(
"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.
}

View File

@ -35,14 +35,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Owin, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
<Private>True</Private>
@ -75,33 +67,9 @@
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SQLite.Net, Version=3.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SQLite.Net.Core-PCL.3.1.1\lib\portable-win8+net45+wp8+wpa81+MonoAndroid1+MonoTouch1\SQLite.Net.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SQLite.Net.Platform.Generic, Version=3.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SQLite.Net-PCL.3.1.1\lib\net40\SQLite.Net.Platform.Generic.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SQLite.Net.Platform.Win32, Version=3.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SQLite.Net-PCL.3.1.1\lib\net4\SQLite.Net.Platform.Win32.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Data.SQLite, Version=1.0.104.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.SQLite.Core.1.0.104.0\lib\net451\System.Data.SQLite.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Data.SQLite.EF6, Version=1.0.104.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.SQLite.EF6.1.0.104.0\lib\net451\System.Data.SQLite.EF6.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Data.SQLite.Linq, Version=1.0.104.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.SQLite.Linq.1.0.104.0\lib\net451\System.Data.SQLite.Linq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
<Private>True</Private>
@ -154,14 +122,17 @@
<Content Include="sqlite3.def" />
<Content Include="sqlite3.dll" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Interfaces\Interfaces.csproj">
<Project>{B7347B72-E208-423A-9D99-723B558EA3D7}</Project>
<Name>Interfaces</Name>
</ProjectReference>
<ProjectReference Include="..\SQLiteRepository\SQLiteRepository.csproj">
<Project>{B3510C81-F069-48A2-B826-EBE0CE7AB0B2}</Project>
<Name>SQLiteRepository</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\System.Data.SQLite.Core.1.0.104.0\build\net451\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.104.0\build\net451\System.Data.SQLite.Core.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\System.Data.SQLite.Core.1.0.104.0\build\net451\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.Data.SQLite.Core.1.0.104.0\build\net451\System.Data.SQLite.Core.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.0.0" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net452" />
@ -13,10 +12,4 @@
<package id="Ninject.Web.Common" version="3.2.0.0" targetFramework="net452" />
<package id="Ninject.Web.WebApi" version="3.2.4.0" targetFramework="net452" />
<package id="Owin" version="1.0" targetFramework="net452" />
<package id="SQLite.Net.Core-PCL" version="3.1.1" targetFramework="net452" />
<package id="SQLite.Net-PCL" version="3.1.1" targetFramework="net452" />
<package id="System.Data.SQLite" version="1.0.104.0" targetFramework="net452" />
<package id="System.Data.SQLite.Core" version="1.0.104.0" targetFramework="net452" />
<package id="System.Data.SQLite.EF6" version="1.0.104.0" targetFramework="net452" />
<package id="System.Data.SQLite.Linq" version="1.0.104.0" targetFramework="net452" />
</packages>