GuestLog - Added delete to GuestDocument list.

This commit is contained in:
2026-06-22 18:21:06 +02:00
parent 9f44ff6e3d
commit d93ba3d9ee
3 changed files with 75 additions and 10 deletions
@@ -57,11 +57,23 @@
<CellTemplate>
<MudIconButton Size="@Size.Small" Icon="@Icons.Material.Outlined.Info"
OnClick="() => BtnViewDetails(context.Item)" />
<MudIconButton Size="@Size.Small" Icon="@Icons.Material.Outlined.Delete"
OnClick="() => BtnDeleteItem(context.Item)" />
</CellTemplate>
</TemplateColumn>
</Columns>
</MudDataGrid>
<MudMessageBox @ref="_msgBoxConfirmDelete" Title="Warning" CancelText="Cancel">
<MessageContent>
Deleting can <b><i>not</i></b> be undone!
</MessageContent>
<YesButton>
<MudButton Variant="Variant.Filled" Color="Color.Error" StartIcon="@Icons.Material.Filled.DeleteForever">Delete!
</MudButton>
</YesButton>
</MudMessageBox>
@code {
private IEnumerable<GuestDocumentDTO> guestDocList = [];
@@ -70,7 +82,8 @@
private HashSet<string> _moduleList = [];
private HashSet<string> _selectedModuleList = [];
private MudDataGrid<GuestDocumentDTO>? _dataGrid;
private MudDataGrid<GuestDocumentDTO> _dataGrid = null!;
private MudMessageBox _msgBoxConfirmDelete = null!;
protected override async Task OnInitializedAsync()
@@ -108,11 +121,6 @@
private async Task BtnConfigureColumnsClick(MouseEventArgs args)
{
if (_dataGrid is null)
{
Snackbar.Add("No ref to grid");
return;
}
_dataGrid.ShowColumnsPanel(args);
}
@@ -130,6 +138,26 @@
await DialogService.ShowAsync<GuestDocDetailsDialog>("GuestDoc Details", parameters, options);
}
private async Task BtnDeleteItem(GuestDocumentDTO item)
{
bool? confirmDelete = await _msgBoxConfirmDelete.ShowAsync();
if (confirmDelete is null || confirmDelete.Value == false)
{
return;
}
bool deleted = await GuestDocService.DeleteDocumentAsync(item);
if (!deleted)
{
Snackbar.Add($"Error deleting document {item.DocumentType}-{item.DocumentNum}", Severity.Error);
return;
}
Snackbar.Add($"Document {item.DocumentType}-{item.DocumentNum} deleted!", Severity.Success);
await LoadDataAsync();
}
private void BtnNewGuest()
{
Navigation.NavigateTo(GuestConsts.Page_GuestDocs_New);
@@ -69,16 +69,51 @@ public class GuestDocumentService(
}
}
public bool UpdateDocument(GuestDocumentDTO dto)
public Task<bool> UpdateDocumentAsync(GuestDocumentDTO dto)
{
throw new NotImplementedException();
}
public Task<bool> DeleteDocumentAsync(GuestDocumentDTO dto)
{
if (dto.DocumentType is null || dto.DocumentNum is null)
{
throw new InvalidOperationException("GuestDocumentService - DeleteDocument: A DTO without key can't be deleted!");
}
return DeleteDocumentAsync(dto.DocumentType, dto.DocumentNum);
}
public async Task<bool> DeleteDocumentAsync(string docType, string docNum)
{
using var dbContext = contextFactory.CreateDbContext();
GuestDocument? dbItem = await dbContext.GuestDocuments.FindAsync(docType, docNum);
if (dbItem is null)
{
logger.LogWarning("GuestDocumentService - DeleteDocument: Error deleting GuestDocument. Entity not found with key: '{key}-{key2}'", docType, docNum);
return false;
}
try
{
dbContext.GuestDocuments.Remove(dbItem);
await dbContext.SaveChangesAsync();
logger.LogInformation("GuestDocumentService - DeleteDocument: GuestDocument deleted: '{id}'", dbItem.GetFullKey());
return true;
}
catch (Exception ex)
{
logger.LogError(ex, "GuestDocumentService - CreateDocument: Error deleting GuestDocument: '{id}'", dbItem.GetFullKey());
return false;
}
}
public async Task<GuestDocumentDTO?> GetDtoByIdAsync(string docType, string docNum)
{
using var dbContext = contextFactory.CreateDbContext();
GuestDocument? dbItem = await dbContext.GuestDocuments.FindAsync(new { docType, docNum });
GuestDocument? dbItem = await dbContext.GuestDocuments.FindAsync(docType, docNum);
return dbItem is not null ? GetDtoFromDb(dbItem) : null;
}
@@ -5,7 +5,9 @@ namespace Somnia.GuestLog.Services;
public interface IGuestDocumentService
{
public Task<bool> CreateDocumentAsync(GuestDocumentDTO dto);
public bool UpdateDocument(GuestDocumentDTO dto);
public Task<bool> UpdateDocumentAsync(GuestDocumentDTO dto);
public Task<bool> DeleteDocumentAsync(GuestDocumentDTO dto);
public Task<bool> DeleteDocumentAsync(string docType, string docNum);
public Task<GuestDocumentDTO?> GetDtoByIdAsync(string docType, string docNum);
public Task<IEnumerable<GuestDocumentDTO>> GetDtoListAsync();