SaveProgress

This commit is contained in:
2026-03-03 22:17:38 +01:00
parent 9fab942181
commit e7f8e8c331
6 changed files with 201 additions and 439 deletions
@@ -0,0 +1,146 @@
@using CouchLog.Data
@using Microsoft.EntityFrameworkCore
@using Microsoft.AspNetCore.Authorization
@using CouchLog.Data
@using Microsoft.AspNetCore.Identity
@using Microsoft.EntityFrameworkCore
@inject ApplicationDbContext CouchLogDB
@inject UserManager<ApplicationUser> UserManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager
<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">
Add Global Entity
</MudText>
</TitleContent>
<DialogContent>
<EditForm Model="@newGlobalEntity" OnSubmit="CreateNewGlobalEntity">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="mb-3">
<label for="Title" class="form-label">Title</label>
<InputText id="Title" class="form-control" @bind-Value="newGlobalEntity.Title"></InputText>
<ValidationMessage For="@(() => newGlobalEntity.Title)"></ValidationMessage>
</div>
<div class="mb-3">
<label for="Type" class="form-label">Type</label>
<select id="Type" class="form-select" @bind="SelectedMediaTypeId">
@foreach(MediaType Type in MediaTypes)
{
<option value="@Type.Id">@Type.Name</option>
}
</select>
</div>
@*<div class="mb-3">
<label for="Picture" class="form-label">Picture</label>
<InputFile OnChange="@LoadImage" accept="image/*" />
@if (!string.IsNullOrEmpty(ImageString))
{
<img src="@ImageString" style="max-width: 256px; max-height: 256px;" />
}
</div>*@
<div class="mb-3">
<label for="Genres" class="form-label">Genres</label>
@foreach(Genre Genre in Genres)
{
<button class="btn-primary btn me-2" value="@Genre.Id" type="button" @onclick="() => AddGenreIdToGenreIdList(Genre.Id)">@Genre.Name</button>
}
</div>
<div class="mb-3">
<InputCheckbox DisplayName="isPrivate" @bind-Value="IsPrivate" ></InputCheckbox>
</div>
<button type="submit" class="btn-success btn">Create new Global Entity</button>
</EditForm>
</DialogContent>
@*Dialog Footer*@
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
</DialogActions>
</MudDialog>
@code {
private ApplicationUser AppUser = null!;
[CascadingParameter]
private IMudDialogInstance MudDialog { get; set; }
private void Cancel() => MudDialog.Cancel();
private List<MediaType> MediaTypes = new();
private List<Genre> Genres = new();
#region ForNewGlobalEntity
private GlobalEntity newGlobalEntity = new();
private IBrowserFile? Picture;
private int SelectedMediaTypeId;
private bool IsPrivate;
private List<int> GenreIds = new ();
#endregion
protected override async Task OnInitializedAsync()
{
var AuthState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var User = AuthState.User;
AppUser = (await UserManager.GetUserAsync(User))!;
if(AppUser == null)
{
throw new Exception("Not Authenticated");
}
MediaTypes = await CouchLogDB.MediaType.OrderBy(Type => Type.Id).ToListAsync();
Genres = await CouchLogDB.Genres.OrderBy(Type => Type.Id).ToListAsync();
}
private async Task CreateNewGlobalEntity()
{
if (Picture is null)
{
throw new InvalidOperationException("No Picture selected.");
}
if (AppUser is null)
{
throw new InvalidOperationException("User not loaded or not logged in.");
}
//Save Picture and Name it
string NewFileName = $"{newGlobalEntity.Title.Replace(" ", "-")}-{Guid.NewGuid()}{Path.GetExtension(Picture.Name)}";
string PicturePath = Path.Combine("wwwroot", "Pictures", NewFileName);
using FileStream FileStream = File.Create(PicturePath);
await Picture.OpenReadStream().CopyToAsync(FileStream);
//await Picture.OpenReadStream(maxAllowedSize: 10_000_000).CopyToAsync(FileStream);
newGlobalEntity.PicturePath = $"Pictures/{NewFileName}";
newGlobalEntity.CreatorId = AppUser.Id;
newGlobalEntity.TypeId = SelectedMediaTypeId;
newGlobalEntity.CreationTime = DateTime.Now;
newGlobalEntity.IsPrivate = IsPrivate;
CouchLogDB.Add(newGlobalEntity);
await CouchLogDB.SaveChangesAsync();
foreach(int GenreId in GenreIds)
{
LinkTableGlobalGenre LinkTableGlobalGenre = new() { GenreId = GenreId, GlobalEntityId = newGlobalEntity.Id };
CouchLogDB.Add(LinkTableGlobalGenre);
}
await CouchLogDB.SaveChangesAsync();
NavigationManager.NavigateTo(NavigationManager.Uri, true);
}
private void AddGenreIdToGenreIdList(int GenreId)
{
if (!GenreIds.Contains(GenreId))
{
GenreIds.Add(GenreId);
}
}
}