Add Api Endpoint to save/retrieve the active policy.Policy

Add SQLite code to save the policy html/markdown scripts from the user
#94
This commit is contained in:
chris.watts90@outlook.com 2019-10-18 09:30:57 +01:00
parent f286498540
commit dc2210b5b3
9 changed files with 376 additions and 234 deletions

View File

@ -112,5 +112,9 @@ namespace Interfaces
OperationResponse DeleteLog(TimeLog log);
OperationResponse CreateLog(TimeLog log);
OperationResponse UpdateLog(TimeLog log);
Policy GetPolicy();
void SavePolicy(Policy policy);
}
}

View File

@ -70,6 +70,7 @@
<Compile Include="IdentifierList.cs" />
<Compile Include="ManualLog.cs" />
<Compile Include="OperationResponse.cs" />
<Compile Include="Policy.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TimeLog.cs" />
<Compile Include="TimeLogList.cs" />

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Interfaces
{
public class Policy
{
public DateTime ChangeDate { get; set; }
public string ChangeDescription { get; set; }
public string ChangeAuthor { get; set; }
public string Version { get; set; }
public string Markdown { get; set; }
public string Html { get; set; }
}
}

View File

@ -0,0 +1,35 @@
using Interfaces;
namespace SQLiteRepository.Converters
{
static class PolicyConverter
{
public static Policy ConvertToPolicyDto(PolicyDb policyDb)
{
if (policyDb == null) return null;
return new Policy
{
ChangeDescription = policyDb.ChangeDescription,
ChangeDate = policyDb.ChangeDate.UtcDateTime,
Html = policyDb.Html,
Version = policyDb.Version,
ChangeAuthor = policyDb.ChangeAuthor,
Markdown = policyDb.Markdown
};
}
public static PolicyDb ConvertFromPolicyDto(Policy policy)
{
if (policy == null) return null;
return new PolicyDb
{
ChangeDescription = policy.ChangeDescription,
ChangeDate = policy.ChangeDate,
Html = policy.Html,
Version = policy.Version,
ChangeAuthor = policy.ChangeAuthor,
Markdown = policy.Markdown
};
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLite.Net.Attributes;
namespace SQLiteRepository
{
internal class PolicyDb
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public DateTimeOffset ChangeDate { get; set; }
public string ChangeDescription { get; set; }
public string ChangeAuthor { get; set; }
public string Version { get; set; }
public string Markdown { get; set; }
public string Html { get; set; }
}
}

View File

@ -38,6 +38,8 @@ namespace SQLiteRepository
_connection.CreateTable<GroupDb>();
_connection.CreateTable<UserGroupJoinDb>();
_connection.CreateTable<DbVersion>();
_connection.CreateTable<PolicyDb>();
_logger.Trace("Initialised SQLite Repository");
_logger.Trace("Checking For Upgrades");
CheckForDbUpgrade();
@ -520,6 +522,34 @@ namespace SQLiteRepository
return OperationResponse.UPDATED;
}
public Policy GetPolicy()
{
var query = $"select * from {nameof(PolicyDb)}";
var policies = _connection.Query<PolicyDb>(query);
return PolicyConverter.ConvertToPolicyDto(policies.OrderByDescending(x => x.Version).FirstOrDefault());
}
public void SavePolicy(Policy policy)
{
if (string.IsNullOrEmpty(policy.Version))
{
policy.Version = (GetNextPolicyVersion()+1).ToString();
}
var policyDb = PolicyConverter.ConvertFromPolicyDto(policy);
_connection.Insert(policyDb);
}
private int GetNextPolicyVersion()
{
var query = $"select Max({nameof(PolicyDb.Id)}) from {nameof(PolicyDb)}";
var id = _connection.ExecuteScalar<int>(query);
return id;
}
private int GetUserCount()
{
return _connection.ExecuteScalar<int>(SQLiteProcedures.GET_TOTAL_USER_COUNT);

View File

@ -80,12 +80,14 @@
<Compile Include="CardUniqueId.cs" />
<Compile Include="Converters\LogDirectionConverter.cs" />
<Compile Include="Converters\LogSourceConverter.cs" />
<Compile Include="Converters\PolicyConverter.cs" />
<Compile Include="Converters\TimeLogConverter.cs" />
<Compile Include="Converters\UserConverter.cs" />
<Compile Include="DBVersion.cs" />
<Compile Include="Extensions\Extensions.cs" />
<Compile Include="GroupDb.cs" />
<Compile Include="LogSourceDb.cs" />
<Compile Include="PolicyDb.cs" />
<Compile Include="SQLiteRepository.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@ -106,10 +108,10 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<EmbeddedResource Include="UpgradeScripts\0.2.sql" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="UpgradeScripts\0.2.sql" />
<None Include="packages.config" />
</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')" />

View File

@ -1,5 +1,6 @@
using System.Reflection;
using System.Web.Http;
using WindowsDataCenter.Helpers;
using Interfaces;
namespace WindowsDataCenter
@ -7,6 +8,12 @@ namespace WindowsDataCenter
[RoutePrefix("api/app")]
public class ApplicationController:ApiController
{
private IRepository _repo;
public ApplicationController(IRepository repo)
{
_repo = repo;
}
[Route("")]
public IHttpActionResult GetAppDetails()
{
@ -22,5 +29,23 @@ namespace WindowsDataCenter
return Ok(appDetails);
}
[Route("policy")]
[HttpGet]
[CacheControl(MaxAge = 0)]
public IHttpActionResult GetPolicy()
{
Policy policy = _repo.GetPolicy();
return Json(policy);
}
[Route("policy")]
[HttpPost]
[CacheControl(MaxAge = 0)]
public IHttpActionResult SavePolicy(Policy policy)
{
_repo.SavePolicy(policy);
return Ok(new {}); //empty to ensure ajax triggers Success..
}
}
}