Edits Global/GlobalEntity.cs and adds a test Create

This commit is contained in:
Henry Trumme
2025-04-25 22:33:35 +02:00
parent 1b40ea2cea
commit cca0f6c6a4
4 changed files with 85 additions and 2 deletions

View File

@@ -0,0 +1,38 @@
@rendermode InteractiveServer
@page "/"
@using Microsoft.EntityFrameworkCore
@using WatchLog.Data
@inject IDbContextFactory<WatchLogDataContext> WatchLogDataContextFactory;
<PageTitle>GlobalEntities</PageTitle>
@if (ShowCreate)
{
<h3>Add a New GlobalEntity</h3>
<div class="row">
<label for="Title" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="Title" name="Title" type="text" class="form-control" @bind="@NewGlobalEntity.Title" />
</div>
</div>
<div class="row">
<label for="Password" class="col-4 col-form-label">Password</label>
<div class="col-8">
<input id="Password" name="Password" type="text" class="form-control"/>
</div>
</div>
<div class="form-group row">
<div class="offset-4 col-8">
<button name="submit" type="submit" class="btn btn-primary" @onclick="CreateNewGlobalEntity">Submit</button>
</div>
</div>
}
else
{
<div class="form-group row">
<div class="offset-4 col-8">
<button name="submit" type="submit" class="btn btn-primary" @onclick="ShowCreateForm">Add a new Global Entity</button>
</div>
</div>
<p>SHOW THE LIST</p>
}

View File

@@ -0,0 +1,45 @@
using Microsoft.AspNetCore.Components;
using WatchLog.Data;
namespace WatchLog.Components.Pages
{
public partial class GlobalEntities
{
public bool ShowCreate { get; set; }
private WatchLogDataContext? _context;
public required GlobalEntity NewGlobalEntity { get; set; }
protected override Task OnInitializedAsync()
{
ShowCreate = false;
return Task.CompletedTask;
}
public void ShowCreateForm()
{
ShowCreate = true;
NewGlobalEntity = new GlobalEntity
{
Title = "",
CreationTime = DateTime.Now,
CreatorId = 1,
TypeId = 1,
};
}
public async Task CreateNewGlobalEntity()
{
_context ??= await WatchLogDataContextFactory.CreateDbContextAsync();
if (NewGlobalEntity is not null)
{
_context?.GlobalEntities.Add(NewGlobalEntity);
_context?.SaveChangesAsync();
}
ShowCreate = false;
}
}
}

View File

@@ -23,10 +23,10 @@ namespace WatchLog.Data
// --- Foreign Keys ---
[Required]
public int TypeId { get; set; }
public required int TypeId { get; set; }
[Required]
public int CreatorId { get; set; }
public required int CreatorId { get; set; }
// --- Navigation Properties ---