feat StayExport - Added delete for StayExports.

This commit is contained in:
2026-09-06 22:24:25 +02:00
parent 6fe5a5e5a3
commit 4b72e3b3d3
6 changed files with 84 additions and 11 deletions
@@ -154,24 +154,23 @@
private async Task BtnDeleteItem(StayExportFileDTO item)
{
userMessage.ShowMessage("Not Implemented!", MessageKind.Normal);
@* bool? confirmDelete = await _confirmDeleteMsgBox.MsgBox.ShowAsync();
bool? confirmDelete = await _confirmDeleteMsgBox.MsgBox.ShowAsync();
if (confirmDelete is null || confirmDelete.Value == false)
{
return;
}
Result deleteResult = await stayExportEntityService.Delete(item);
Result deleteResult = await ExportService.DeleteExportedFileAsync(item);
if (!deleteResult.IsSuccess)
{
string errorMsg = $"Error deleting Stay {item.Id}";
string errorMsg = $"Error deleting Export file {item.Id}";
userMessage.ShowResultWithMessage(deleteResult, errorMsg);
return;
}
userMessage.ShowMessage($"Stay {item.Id} deleted!", MessageKind.Success);
await LoadDataAsync(); *@
userMessage.ShowMessage($"Export file {item.Id} deleted!", MessageKind.Success);
await LoadDataAsync();
}
#endregion
@@ -7,6 +7,7 @@ public enum ResultCode
UnknownError,
FailedValidation,
NotFound,
FileStorageError,
//Specific Codes
GuestInStay,
@@ -195,6 +195,7 @@
<data name="enum.ResultCode.UnknownError" xml:space="preserve"><value>Unknown Error</value></data>
<data name="enum.ResultCode.FailedValidation" xml:space="preserve"><value>Failed entity validation</value></data>
<data name="enum.ResultCode.NotFound" xml:space="preserve"><value>Not found</value></data>
<data name="enum.ResultCode.FileStorageError" xml:space="preserve"><value>File Storage Error</value></data>
<!-- Specific codes -->
<data name="enum.ResultCode.GuestInStay" xml:space="preserve"><value>Guest (ID: '{0}') is in a stay.</value></data>
<data name="enum.ResultCode.GuestCurrentlyInStay" xml:space="preserve"><value>Guest (ID: '{0}') is already in one stay during the period: {1}</value></data>
@@ -7,6 +7,12 @@ public interface IStayExportFileEntityService
public Task<Result> CreateStayExportAsync(StayExportFileDTO dto);
public Task<Result> UpdateStayExportAsync(StayExportFileDTO dto);
/// <summary>
/// Delete StayExport entry from DB.
/// Exported File won't be deleted from Storage.
/// </summary>
public Task<Result> DeleteStayExportAsync(StayExportFileDTO dto);
public Task<StayExportFileDTO?> GetDtoByIdAsync(long id);
//public Task<IEnumerable<StayExportFileDTO>> GetDtoListByFilterAsync(StayExportFileFilterDTO filter, int limit = 0);
public Task<IEnumerable<StayExportFileDTO>> GetDtoListAsync(int limit = 0);
@@ -1,5 +1,6 @@
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using Somnia.GuestLog.Enums.Services;
using Somnia.GuestLog.Infrastructure;
using Somnia.GuestLog.Models;
using Somnia.GuestLog.Models.DTOs;
@@ -59,6 +60,33 @@ public class StayExportFileEntityService(
throw new NotImplementedException();
}
public async Task<Result> DeleteStayExportAsync(StayExportFileDTO dto)
{
try
{
using var dbContext = contextFactory.CreateDbContext();
int deletedCount = await dbContext.StayExportFiles
.Where(e => e.Id == dto.Id)
.ExecuteDeleteAsync();
if (deletedCount == 0)
{
logger.LogWarning("StayExportFileService - DeleteStayExportAsync: Error deleting StayExport. Entity not found with ID: '{ID}'", dto.Id);
return Result.Fail(ResultCode.NotFound);
}
logger.LogInformation(
"StayExportFileService - DeleteStayExportAsync: StayExport deleted with Id: '{id}'.", dto.Id);
return Result.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "StayExportFileService - DeleteStayExportAsync: Error creating GuestDocument");
return Result.FailUnknownError();
}
}
public async Task<StayExportFileDTO?> GetDtoByIdAsync(long id)
{
@@ -160,13 +160,26 @@ public class StayExportService(
return Result.Ok();
}
public Task<Result> DeleteExportedFileAsync(StayExportFileDTO item)
public async Task<Result> DeleteExportedFileAsync(StayExportFileDTO item)
{
throw new NotImplementedException();
//Try to delete existing file.
if (!string.IsNullOrEmpty(item.ExportedPath))
{
bool fileDeletedFromStore = await DeleteExportFileFromStoreAsync(item);
if (!fileDeletedFromStore)
{
return Result.Fail(ResultCode.FileStorageError);
}
}
//TODO:
// - Delete file from "Store" if exists.
// - Delete entry from DB.
//Delete entry from DB.
Result deleteResult = await stayExportEntityService.DeleteStayExportAsync(item);
if (!deleteResult.IsSuccess)
{
return Result.Fail(deleteResult.ResultCode);
}
return Result.Ok();
}
@@ -194,6 +207,31 @@ public class StayExportService(
throw new InvalidOperationException($"StayExportFile - Error getting stays using params: export type invalid: {exportParams.ExportType}");
}
private async Task<bool> DeleteExportFileFromStoreAsync(StayExportFileDTO item)
{
Debug.Assert(!string.IsNullOrEmpty(item.ExportedPath));
try
{
IStore store = GetExportStore();
bool objectExist = await store.ObjectExists(item.ExportedPath);
if (!objectExist)
{
logger.LogWarning("StayExportService - File for item {id} not found in store! Skip delete!", item.Id);
return true;
}
await store.DeleteObject(item.ExportedPath);
logger.LogInformation("StayExportService - File for item {id} deleted from path {path}", item.Id, item.ExportedPath);
return true;
}
catch (Exception ex)
{
logger.LogError(ex, "StayExportService - Error deleting export file of item {id}", item.Id);
return false;
}
}
private IStore GetExportStore()
{
string? storageConnString = config.ExportStorageConnectionString;