SHA256
138 lines
4.5 KiB
C#
138 lines
4.5 KiB
C#
using FluentValidation;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Somnia.GuestLog.Enums.Services;
|
|
using Somnia.GuestLog.Infrastructure;
|
|
using Somnia.GuestLog.Models;
|
|
using Somnia.GuestLog.Models.DTOs;
|
|
using Somnia.GuestLog.Models.Entities;
|
|
using Somnia.GuestLog.Utils;
|
|
|
|
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<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)
|
|
{
|
|
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(int limit = 0)
|
|
{
|
|
using var dbContext = contextFactory.CreateDbContext();
|
|
|
|
IQueryable<StayExportFile> query = dbContext.StayExportFiles
|
|
.OrderByDescending(e => e.ExportTime)
|
|
.WithLimit(limit);
|
|
|
|
var dbList = await query.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
|
|
} |