feat StayExport - Moved Entity Services.

This commit is contained in:
2026-09-01 18:14:27 +02:00
parent c24faedee5
commit b318f2415c
4 changed files with 8 additions and 8 deletions
@@ -0,0 +1,107 @@
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using Somnia.GuestLog.Infrastructure;
using Somnia.GuestLog.Models;
using Somnia.GuestLog.Models.DTOs;
using Somnia.GuestLog.Models.Entities;
namespace Somnia.GuestLog.Services.Entity;
public class StayExportFileEntityService(
ILogger<StayExportFileEntityService> logger,
IDbContextFactory<GuestLogContext> contextFactory
) : IStayExportFileEntityService
{
public async Task<Result> CreateStayExportAsync(StayExportFileDTO dto)
{
ArgumentNullException.ThrowIfNull(dto, nameof(dto));
//Check for null values in the DTO.
if (dto.ExportFormat == null || dto.ExportResult == null || dto.ExportTime == null)
{
throw new InvalidOperationException("StayExportFile dto contains null fields");
}
if (dto.ExportParams == null)
{
throw new InvalidOperationException("StayExportFile dto contains null ExportParams");
}
StayExportParams exportParams = GetExportParamsFromDTO(dto.ExportParams);
StayExportFile dbItem = new()
{
ExportFormat = dto.ExportFormat.Value,
ExportParams = exportParams,
ExportResult = dto.ExportResult.Value,
ExportedPath = dto.ExportedPath,
ExportTime = dto.ExportTime?.ToUniversalTime() ?? DateTime.UtcNow,
};
try
{
using var dbContext = contextFactory.CreateDbContext();
await dbContext.AddAsync(dbItem);
await dbContext.SaveChangesAsync();
logger.LogInformation("StayExportFileService - CreateStayExportAsync: GuestDocument Created: '{id}'", dbItem.GetFullKey());
return Result.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "StayExportFileService - CreateStayExportAsync: Error creating GuestDocument");
return Result.FailUnknownError();
}
}
public Task<Result> UpdateStayExportAsync(StayExportFileDTO dto)
{
throw new NotImplementedException();
}
public async Task<StayExportFileDTO?> GetDtoByIdAsync(long id)
{
using var dbContext = contextFactory.CreateDbContext();
StayExportFile? dbItem = await dbContext.StayExportFiles
.Where(e => e.Id == id)
.FirstOrDefaultAsync();
return dbItem is not null ? GetDtoFromDb(dbItem) : null;
}
public async Task<IEnumerable<StayExportFileDTO>> GetDtoListAsync()
{
using var dbContext = contextFactory.CreateDbContext();
var dbList = await dbContext.StayExportFiles
.OrderByDescending(e => e.ExportTime)
.ToListAsync();
return dbList.Select(e => GetDtoFromDb(e));
}
#region Internal Methods
private static StayExportFileDTO GetDtoFromDb(StayExportFile dbItem)
{
return EntityMapping.MapStayExportFileEntityToDTO(dbItem);
}
private static StayExportParams GetExportParamsFromDTO(StayExportParamsDTO dto)
{
if (dto.ExportType == null)
{
throw new InvalidOperationException("Dto with invalid data");
}
return new()
{
ExportType = dto.ExportType.Value,
ExportPeriodStart = dto.ExportPeriodStart?.ToUniversalTime(),
ExportPeriodEnd = dto.ExportPeriodEnd?.ToUniversalTime()
};
}
#endregion
}