Skip to content

Commit 2aba478

Browse files
authored
Merge pull request #1 from danmaddock265/master
PatientOptOut Logging
2 parents 0140579 + 478260a commit 2aba478

File tree

130 files changed

+254249
-49
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+254249
-49
lines changed

PatientOptOutAPI/Controllers/PatientOptOutController.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.AspNetCore.Authorization;
1010
using Microsoft.AspNetCore.Cors;
1111
using Microsoft.Extensions.Options;
12+
using System;
1213

1314
namespace PatientOptOutAPI.Controllers
1415
{
@@ -19,12 +20,14 @@ namespace PatientOptOutAPI.Controllers
1920
public class PatientOptOutController : ControllerBase
2021
{
2122
private readonly DataWarehouseContext _context;
23+
private readonly LogService _logService;
2224
public readonly IOptions<ApplicationSettings> _config;
2325

2426
//Injects controller with the controller context
25-
public PatientOptOutController(DataWarehouseContext context, IOptions<ApplicationSettings> config)
27+
public PatientOptOutController(DataWarehouseContext context, LogService logService, IOptions<ApplicationSettings> config)
2628
{
2729
_context = context;
30+
_logService = logService;
2831
_config = config;
2932
}
3033

@@ -43,6 +46,12 @@ public async Task<IActionResult>PostNumbers([FromBody] List<string> numbers)
4346
if (!ModelState.IsValid)
4447
return BadRequest(ModelState);
4548

49+
if(_config.Value.LoggingEnabled)
50+
{
51+
var message = "Patient Identifiers Checked: " + string.Join(", ", numbers.ToArray());
52+
_logService.LogMessage(message, User.GetUsernameWithoutDomain());
53+
}
54+
4655
//Fetches database columns and checks to see if they contain the input(s). If so, it is add to a list
4756
var listOfNHSMatches = _context.vw_PatientOptOut.Where(row => numbers.Contains(row.NHSNumber)).Select(itemInList => itemInList.NHSNumber).ToList();
4857
var listOfHospitalMatches = _context.vw_PatientOptOut.Where(row => numbers.Contains(row.HospitalNumber)).Select(itemInList => itemInList.HospitalNumber).ToList();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using PatientOptOutAPI.Models;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace PatientOptOutAPI.Data
5+
{
6+
public class LogContext : DbContext
7+
{
8+
public LogContext(DbContextOptions<LogContext> options) : base(options) { }
9+
10+
public DbSet<Log> Logs { get; set; }
11+
12+
protected override void OnModelCreating(ModelBuilder modelBuilder)
13+
{
14+
modelBuilder.HasDefaultSchema("PatientOptOut");
15+
modelBuilder.Entity<Log>()
16+
.Property(b => b.Username)
17+
.IsRequired();
18+
}
19+
}
20+
}

PatientOptOutAPI/Models/ApplicationSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public class ApplicationSettings
44
{
55
public string ActiveDirectoryGroupName { get; set; }
6+
public bool LoggingEnabled { get; set; }
67
}
78
}

PatientOptOutAPI/Models/Log.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace PatientOptOutAPI.Models
5+
{
6+
public class Log
7+
{
8+
[Key]
9+
public int LogId { get; set; }
10+
11+
public string Username { get; set; }
12+
13+
public string Message { get; set; }
14+
15+
public DateTime DateTime { get; set; }
16+
}
17+
}

PatientOptOutAPI/PatientOptOutAPI.csproj

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,31 @@
1212
<PackageReference Include="Microsoft.AspNet.WebApi.Cors" Version="5.2.7" />
1313
<PackageReference Include="Microsoft.AspNetCore.App" />
1414
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
15-
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.2.6" />
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.6" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="3.1.2" />
17+
<PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="3.1.2" />
18+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.2">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.2" />
23+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.2" />
24+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.2">
25+
<PrivateAssets>all</PrivateAssets>
26+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
27+
</PackageReference>
28+
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="3.1.2" />
29+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.2" />
30+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.2" />
31+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.2" />
32+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.2" />
33+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" />
34+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.2" />
35+
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.2" />
36+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.2" />
37+
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="3.1.2" />
38+
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.2" />
39+
<PackageReference Include="Microsoft.Extensions.Primitives" Version="3.1.2" />
1740
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration" Version="2.2.3" />
1841
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Contracts" Version="2.2.3" />
1942
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Core" Version="2.2.3" />

PatientOptOutAPI/Properties/PublishProfiles/FolderProfile1.pubxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ by editing this MSBuild file. In order to learn more about this please visit htt
1313
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
1414
<ExcludeApp_Data>False</ExcludeApp_Data>
1515
<ProjectGuid>b0a1d955-52ad-4eeb-becc-dd79c25e1d43</ProjectGuid>
16-
<publishUrl>D:\PatientOptOut\PatientOptOutAPI - Published Code</publishUrl>
16+
<publishUrl>D:\PatientOptOut\Patient-OptOut-Checker\PublishedFiles</publishUrl>
1717
<DeleteExistingFiles>True</DeleteExistingFiles>
1818
<TargetFramework>netcoreapp2.2</TargetFramework>
1919
<SelfContained>false</SelfContained>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using PatientOptOutAPI.Data;
2+
using PatientOptOutAPI.Models;
3+
using System;
4+
5+
6+
namespace PatientOptOutAPI.Services
7+
{
8+
public class LogService
9+
{
10+
private readonly LogContext _logContext;
11+
12+
public LogService(LogContext logContext)
13+
{
14+
_logContext = logContext;
15+
}
16+
17+
public void LogMessage(string message, string username)
18+
{
19+
var logs = _logContext;
20+
var log = new Log()
21+
{
22+
Username = username,
23+
Message = message,
24+
DateTime = DateTime.Now
25+
};
26+
27+
logs.Logs.Add(log);
28+
logs.SaveChanges();
29+
}
30+
}
31+
}

PatientOptOutAPI/Startup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ public void ConfigureServices(IServiceCollection services)
3131

3232
services.AddCors(options => options.AddPolicy("CorsPolicy", builder => builder.WithOrigins("http://localhost:4200", frontEndUrl).AllowAnyHeader().AllowAnyMethod().AllowCredentials()));
3333
services.AddDbContext<DataWarehouseContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DataWarehouseContext")));
34+
services.AddDbContext<LogContext>(options => options.UseSqlServer(Configuration.GetConnectionString("LogContext")));
3435

3536
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
3637

38+
services.AddScoped<LogService, LogService>();
39+
3740
//Setup Windows Authentication and Policy to restrict access to specific Active Directory group (Specified in appsettings.json)
3841
services.AddTransient<IClaimsTransformation, WindowsClaimsTransformer>();
3942
services.AddAuthentication(IISDefaults.AuthenticationScheme);

PatientOptOutAPI/appsettings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
22
"ConnectionStrings": {
3-
"DataWarehouseContext": "Server=DATABASE_CLUSTER; Database=DATABASE_NAME; Trusted_Connection=True; MultipleActiveResultSets=true"
3+
"DataWarehouseContext": "Server=DATABASE_CLUSTER; Database=DATABASE_NAME; Trusted_Connection=True; MultipleActiveResultSets=true",
4+
"LogContext": "Server=DATABASE_CLUSTER; Database=DATABASE_NAME; Trusted_Connection=True; MultipleActiveResultSets=true"
45
},
56
"ApplicationSettings": {
67
"FrontEndUrl": "https://optoutdev.plymouth.nhs.uk",
7-
"ActiveDirectoryGroupName": "OptOutChecker"
8+
"ActiveDirectoryGroupName": "OptOutChecker",
9+
"LoggingEnabled": true
810
}
911
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
// Use IntelliSense to find out which attributes exist for C# debugging
3+
// Use hover for the description of the existing attributes
4+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (web)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.2/PatientOptOutFrontEnd.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}",
16+
"stopAtEntry": false,
17+
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
18+
"serverReadyAction": {
19+
"action": "openExternally",
20+
"pattern": "^\\s*Now listening on:\\s+(https?://\\S+)"
21+
},
22+
"env": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
},
25+
"sourceFileMap": {
26+
"/Views": "${workspaceFolder}/Views"
27+
}
28+
},
29+
{
30+
"name": ".NET Core Attach",
31+
"type": "coreclr",
32+
"request": "attach",
33+
"processId": "${command:pickProcess}"
34+
}
35+
]
36+
}

0 commit comments

Comments
 (0)