Adds a solid start Foundation with a GlobalList.razor and OnStartUp.cs

This commit is contained in:
2025-10-27 02:01:55 +01:00
parent d78958f7c6
commit 70587b2af6
32 changed files with 627 additions and 286 deletions

View File

@@ -0,0 +1,147 @@
@page "/GlobalList"
@rendermode InteractiveServer
@using Microsoft.AspNetCore.Authorization
@using CouchLog.Data
@using Microsoft.AspNetCore.Identity
@using Microsoft.EntityFrameworkCore
@inject ApplicationDbContext CouchLogDB
@inject UserManager<ApplicationUser> UserManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@attribute [Authorize]
<PageTitle>GlobalList</PageTitle>
<div class="container-fluid mt-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2>Global List</h2>
<button class="btn btn-primary" type="button" @onclick="ToogleCollapseNewGlobalEntity">@(isCollapseNewGlobalEntityOpen ? "X" : "Add Entity")</button>
</div>
<div class="collapse @(isCollapseNewGlobalEntityOpen ? "show" : "")" id="CollapseCreateNewGlobalEntity">
<div class="mb-4 align-items-center CreateNewGlobalEntity-Container">
<EditForm Model="@GlobalEntity" OnSubmit="CreateNewGlobalEntity" FormName="CreateNewGlobalEntityForm">
<div class="mb-3">
<label for="Title" class="form-label">Title</label>
<InputText id="Title" class="form-control" @bind-Value="GlobalEntity.Title"></InputText>
<ValidationMessage For="@(() => GlobalEntity.Title)"></ValidationMessage>
</div>
<div class="mb-3">
<label for="Type" class="form-label">Type</label>
<select>
@foreach(MediaType Type in MediaTypes)
{
<option value="@Type.Id">@Type.Name</option>
}
</select>
</div>
<button type="submit">Create new Global Entity</button>
</EditForm>
</div>
</div>
<div class="row">
@foreach (var Entity in GlobalEntities)
{
<div name="Enity-Container" class="col-12 col-md-6 col-lg-3 mb-4 Entity-Container">
<div name="Entity-Container-Card" class="Entity-Container-Card">
<div name="Enity-Container-Image" class="">
<a href="javascript:void(0)" class="Entity-Container-Image">
<img src="@Entity.PicturePath" alt="" class="Entity-Container-Image" />
</a>
</div>
<div name="Entity-Container-Data" class="">
<h3 class="">@Entity.Title</h3>
</div>
<div name="Entity-Container-Button" class="d-flex Entity-Container-Button" style="gap: 10px;">
<button class="btn btn-primary" type="button" @onclick="() => AddToPrivateList(Entity)">
@(IsInPrivateList(Entity.Id) ? "Added" : "Add to Private List")
</button>
<button class="btn btn-primary" type="button">Add to Shared List</button>
</div>
</div>
</div>
}
</div>
</div>
@code
{
private List<MediaType> MediaTypes = new List<MediaType>();
public List<GlobalEntity> GlobalEntities = new List<GlobalEntity>();
private HashSet<int> UserPrivateEntityIds = new();
protected override async Task OnInitializedAsync()
{
GlobalEntities = await CouchLogDB.GlobalEntities.OrderByDescending(Entity => Entity.Id).ToListAsync();
MediaTypes = await CouchLogDB.MediaType.OrderByDescending(Type => Type.Id).ToListAsync();
var AuthState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var User = AuthState.User;
var AppUser = await UserManager.GetUserAsync(User);
if (AppUser == null)
{
throw new NotImplementedException();
}
var TempUserPrivateEntityIds = await CouchLogDB.PrivateEntities
.Where(p => p.UserId == AppUser.Id)
.Select(p => p.GlobalEntityId)
.ToListAsync();
UserPrivateEntityIds = TempUserPrivateEntityIds.ToHashSet();
}
private bool isCollapseNewGlobalEntityOpen = false;
private void ToogleCollapseNewGlobalEntity()
{
isCollapseNewGlobalEntityOpen = !isCollapseNewGlobalEntityOpen;
}
private GlobalEntity GlobalEntity = new();
private void CreateNewGlobalEntity()
{
}
private bool IsInPrivateList(int GolbalEntityId)
{
return UserPrivateEntityIds.Contains(GolbalEntityId);
}
private async Task AddToPrivateList(GlobalEntity GlobalEntity)
{
var AuthState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var User = AuthState.User;
if(User.Identity?.IsAuthenticated == true)
{
//var AppUser = await UserManager.GetUserAsync(User);
if(!IsInPrivateList(GlobalEntity.Id))
{
PrivateEntity PrivateEntity = new()
{
UserId = (await UserManager.GetUserAsync(User))!.Id,
CreationTime = DateTime.Now,
GlobalEntityId = GlobalEntity.Id,
};
CouchLogDB.PrivateEntities.Add(PrivateEntity);
await CouchLogDB.SaveChangesAsync();
UserPrivateEntityIds.Add(GlobalEntity.Id);
StateHasChanged();
}
}
}
}