Started DataAccess

- Add placeholder User entity
- Add DataAccess class with interface and add it as a service
  to the app.
- Change background color of app as a test
This commit is contained in:
Eero Holmala 2024-03-12 16:00:19 +02:00
parent 03a6a87354
commit eb6b104f04
10 changed files with 91 additions and 7 deletions

View File

@ -1,6 +1,64 @@
namespace DataAccess;
using Dapper;
using WretchedMachines.DataAccess.Entities;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Npgsql;
public class DataAccess
namespace WretchedMachines.DataAccess;
public class DataAccess: IDataAccess, IDisposable
{
private readonly NpgsqlConnection connection;
private readonly IMemoryCache cache;
private bool disposedValue;
public DataAccess(IConfiguration configuration, IMemoryCache cache)
{
var connectionString = configuration.GetConnectionString("Default");
this.cache = cache;
connection = new NpgsqlConnection(connectionString);
connection.Open();
}
public IEnumerable<User>? GetUser(int id)
{
var users = GetFromCache<User>("select * from users");
return users?.Where(u => id == u.Id);
}
protected virtual void Dispose(bool disposing)
{
if(!disposedValue){
if(disposing){
connection?.Dispose();
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(true);
}
private IEnumerable<T> GetFromDb<T>(string sql)
{
return connection.Query<T>(sql);
}
private IEnumerable<T>? GetFromCache<T>(string sql)
{
var cacheKey = typeof(T).Name;
IEnumerable<T>? entities = null;
if(cache.TryGetValue(cacheKey, out entities))
{
return entities;
}
entities = GetFromDb<T>(sql);
cache.Set(cacheKey, entities);
return entities;
}
}

View File

@ -8,6 +8,8 @@
<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Npgsql" Version="8.0.2" />
</ItemGroup>

View File

@ -0,0 +1,14 @@
namespace WretchedMachines.DataAccess.Entities;
public class User : IEquatable<User>
{
public required string Name { get; set; }
public int Id { get; set; }
public bool Equals(User? other)
{
if(Object.ReferenceEquals(other, null)) return false;
if(Object.ReferenceEquals(this, other)) return true;
return Name.Equals(other.Name);
}
}

View File

@ -0,0 +1,8 @@
using WretchedMachines.DataAccess.Entities;
namespace WretchedMachines.DataAccess;
public interface IDataAccess
{
public IEnumerable<User>? GetUser(int id);
}

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Serilog;
using WretchedMachines.DataAccess;
namespace WretchedMachines.Pages;

View File

@ -14,7 +14,7 @@
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<nav class="navbar navbar-expand-sm navbar-toggleable-sm border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-page="/Index">WretchedMachines</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"

View File

@ -18,7 +18,7 @@ a {
}
.nav-pills .nav-link.active, .nav-pills .show > .nav-link {
color: #fff;
color: #802e2e;
background-color: #1b6ec2;
border-color: #1861ac;
}

View File

@ -1,5 +1,5 @@
using Serilog;
using WretchedMachines.DataAccess;
var builder = WebApplication.CreateBuilder(args);
@ -11,7 +11,7 @@ Log.Information("Starting");
builder.Host.UseSerilog();
// Add services to the container.
builder.Services.AddRazorPages();
// builder.Host.UseSerilog();
builder.Services.AddScoped<IDataAccess, DataAccess>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())

View File

@ -7,7 +7,7 @@
},
"AllowedHosts": "*",
"ConnectionStrings": {
"Default": ""
"Default": "Server=127.0.0.1;Port=5432;Database=postgres;User Id=postgres;Password=asd;"
},
"Serilog": {
"Using": ["Serilog.Sinks.Console", "Serilog.Sinks.File"],

View File

@ -19,4 +19,5 @@ html {
body {
margin-bottom: 60px;
background-color: blanchedalmond;
}