From eb6b104f046360141a020ca5b7f8771d4b97795e Mon Sep 17 00:00:00 2001 From: Eero Holmala Date: Tue, 12 Mar 2024 16:00:19 +0200 Subject: [PATCH] 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 --- DataAccess/DataAccess.cs | 62 ++++++++++++++++++- DataAccess/DataAccess.csproj | 2 + DataAccess/Entities/User.cs | 14 +++++ DataAccess/IDataAccess.cs | 8 +++ WretchedMachines/Pages/Index.cshtml.cs | 1 + WretchedMachines/Pages/Shared/_Layout.cshtml | 2 +- .../Pages/Shared/_Layout.cshtml.css | 2 +- WretchedMachines/Program.cs | 4 +- WretchedMachines/appsettings.json | 2 +- WretchedMachines/wwwroot/css/site.css | 1 + 10 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 DataAccess/Entities/User.cs create mode 100644 DataAccess/IDataAccess.cs diff --git a/DataAccess/DataAccess.cs b/DataAccess/DataAccess.cs index a37644d..d8cef63 100644 --- a/DataAccess/DataAccess.cs +++ b/DataAccess/DataAccess.cs @@ -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? GetUser(int id) + { + var users = GetFromCache("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 GetFromDb(string sql) + { + return connection.Query(sql); + } + + private IEnumerable? GetFromCache(string sql) + { + var cacheKey = typeof(T).Name; + IEnumerable? entities = null; + if(cache.TryGetValue(cacheKey, out entities)) + { + return entities; + } + + entities = GetFromDb(sql); + cache.Set(cacheKey, entities); + return entities; + } } diff --git a/DataAccess/DataAccess.csproj b/DataAccess/DataAccess.csproj index 153db05..7c4eb0a 100644 --- a/DataAccess/DataAccess.csproj +++ b/DataAccess/DataAccess.csproj @@ -8,6 +8,8 @@ + + diff --git a/DataAccess/Entities/User.cs b/DataAccess/Entities/User.cs new file mode 100644 index 0000000..dfef7a6 --- /dev/null +++ b/DataAccess/Entities/User.cs @@ -0,0 +1,14 @@ +namespace WretchedMachines.DataAccess.Entities; + +public class User : IEquatable +{ + 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); + } +} \ No newline at end of file diff --git a/DataAccess/IDataAccess.cs b/DataAccess/IDataAccess.cs new file mode 100644 index 0000000..7bd54ec --- /dev/null +++ b/DataAccess/IDataAccess.cs @@ -0,0 +1,8 @@ +using WretchedMachines.DataAccess.Entities; + +namespace WretchedMachines.DataAccess; + +public interface IDataAccess +{ + public IEnumerable? GetUser(int id); +} \ No newline at end of file diff --git a/WretchedMachines/Pages/Index.cshtml.cs b/WretchedMachines/Pages/Index.cshtml.cs index dcc751f..fb996b5 100644 --- a/WretchedMachines/Pages/Index.cshtml.cs +++ b/WretchedMachines/Pages/Index.cshtml.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Serilog; +using WretchedMachines.DataAccess; namespace WretchedMachines.Pages; diff --git a/WretchedMachines/Pages/Shared/_Layout.cshtml b/WretchedMachines/Pages/Shared/_Layout.cshtml index a053274..5e137ac 100644 --- a/WretchedMachines/Pages/Shared/_Layout.cshtml +++ b/WretchedMachines/Pages/Shared/_Layout.cshtml @@ -14,7 +14,7 @@
-