Fixed data fetching from postgres

This commit is contained in:
Eero Holmala 2024-03-12 16:17:47 +02:00
parent eb6b104f04
commit 313b79a63b
6 changed files with 38 additions and 9 deletions

View File

@ -21,10 +21,9 @@ public class DataAccess: IDataAccess, IDisposable
connection.Open(); connection.Open();
} }
public IEnumerable<User>? GetUser(int id) public User? GetUser(int id)
{ {
var users = GetFromCache<User>("select * from users"); return GetFromCache<User>("select * from dbo\"users\"")?.FirstOrDefault();
return users?.Where(u => id == u.Id);
} }
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
@ -45,6 +44,7 @@ public class DataAccess: IDataAccess, IDisposable
private IEnumerable<T> GetFromDb<T>(string sql) private IEnumerable<T> GetFromDb<T>(string sql)
{ {
// TODO(Eero): Add params.
return connection.Query<T>(sql); return connection.Query<T>(sql);
} }
@ -61,4 +61,9 @@ public class DataAccess: IDataAccess, IDisposable
cache.Set(cacheKey, entities); cache.Set(cacheKey, entities);
return entities; return entities;
} }
public IEnumerable<Item>? GetItems()
{
return GetFromCache<Item>("select * from dbo.\"Items\";");
}
} }

View File

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

View File

@ -3,6 +3,11 @@ namespace WretchedMachines.DataAccess.Entities;
public class User : IEquatable<User> public class User : IEquatable<User>
{ {
public required string Name { get; set; } public required string Name { get; set; }
public required string Username { get; set; }
public required string Password { get; set; }
public required string Email { get; set; }
public int Role { get; set; }
public DateTime Created { get; set; }
public int Id { get; set; } public int Id { get; set; }
public bool Equals(User? other) public bool Equals(User? other)

View File

@ -4,5 +4,6 @@ namespace WretchedMachines.DataAccess;
public interface IDataAccess public interface IDataAccess
{ {
public IEnumerable<User>? GetUser(int id); public User? GetUser(int id);
public IEnumerable<Item>? GetItems();
} }

View File

@ -1,21 +1,24 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using Serilog; using Serilog;
using WretchedMachines.DataAccess;
using WretchedMachines.DataAccess.Entities;
namespace WretchedMachines.Pages; namespace WretchedMachines.Pages;
public class PrivacyModel : PageModel public class PrivacyModel : PageModel
{ {
private readonly ILogger<PrivacyModel> _logger; private readonly IDataAccess dataAccess;
public PrivacyModel(ILogger<PrivacyModel> logger) public PrivacyModel(IDataAccess dataAccess)
{ {
_logger = logger; this.dataAccess = dataAccess;
} }
public void OnGet() public void OnGet()
{ {
Log.Information("Test"); var item = dataAccess.GetItems()?.FirstOrDefault();
Log.Information(item?.Name!);
} }
} }

View File

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