Added database loading logic

This commit is contained in:
2025-01-14 14:05:15 +01:00
parent 313f6e046a
commit 6115dcf8e1
13 changed files with 139 additions and 35 deletions

View File

@@ -8,11 +8,22 @@
Welcome to your new Fluent Blazor app.
@inject IContextExplorer Explorer
@inject DatabaseContext Context
@code {
protected override void OnInitialized() {
Console.WriteLine(string.Join(", ", Explorer.GetTableNames()));
protected override async Task OnInitializedAsync() {
for (int i = 0; i < 10; i++) {
Context.Users.Add(new() {
Email = "leon@ladenbau-hoppe.de",
Id = Guid.CreateVersion7()
});
}
await Context.SaveChangesAsync();
var manager = Explorer.GetTableManager("Users");
var page = await manager!.LoadPage(0);
Console.WriteLine(string.Join(", ", page));
}
}

View File

@@ -6,5 +6,13 @@ namespace HopFrame.Testing;
public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbContext(options) {
public DbSet<User> Users { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Post>()
.HasOne<User>()
.WithMany();
}
}

View File

@@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace HopFrame.Testing.Models;
public class Post {
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[MaxLength(255)]
public required string Caption { get; set; }
public required string Content { get; set; }
[ForeignKey("author")]
public User? Author { get; set; }
}

View File

@@ -7,4 +7,8 @@ public class User {
public string? Password { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public override string ToString() {
return Id.ToString();
}
}

View File

@@ -1,6 +1,8 @@
using HopFrame.Core.Services;
using HopFrame.Testing;
using Microsoft.FluentUI.AspNetCore.Components;
using HopFrame.Testing.Components;
using HopFrame.Testing.Services;
using HopFrame.Web;
using HopFrame.Web.Components.Pages;
using Microsoft.EntityFrameworkCore;
@@ -17,10 +19,12 @@ builder.Services.AddDbContext<DatabaseContext>(options => {
});
builder.Services.AddHopFrame(options => {
options.DisplayUserInfo(false);
options.SetAuthHandler<AuthService>();
options.AddDbContext<DatabaseContext>();
});
builder.Services.AddTransient<IHopFrameAuthHandler, AuthService>();
var app = builder.Build();
// Configure the HTTP request pipeline.

View File

@@ -0,0 +1,12 @@
using HopFrame.Core.Services;
namespace HopFrame.Testing.Services;
public class AuthService : IHopFrameAuthHandler {
public Task<bool> IsAuthenticatedAsync(string? policy) {
return Task.FromResult(true);
}
public Task<string> GetCurrentUserDisplayNameAsync() {
return Task.FromResult("Leon Hoppe");
}
}