SHA256
feat StayExport - Moved Entity Services.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
using Somnia.GuestLog.Models.DTOs;
|
||||
|
||||
namespace Somnia.GuestLog.Services.Entity;
|
||||
|
||||
public interface IStayExportFileEntityService
|
||||
{
|
||||
public Task<Result> CreateStayExportAsync(StayExportFileDTO dto);
|
||||
public Task<Result> UpdateStayExportAsync(StayExportFileDTO dto);
|
||||
|
||||
public Task<StayExportFileDTO?> GetDtoByIdAsync(long id);
|
||||
//public Task<IEnumerable<StayExportFileDTO>> GetDtoListByFilterAsync(StayExportFileFilterDTO filter, int limit = 0);
|
||||
public Task<IEnumerable<StayExportFileDTO>> GetDtoListAsync();
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user