SHA256
GuestLog - GuestDocument pages initial implementation
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
using FluentValidation;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Somnia.GuestLog.Infrastructure;
|
||||
using Somnia.GuestLog.Models.DTOs;
|
||||
using Somnia.GuestLog.Models.Entities;
|
||||
|
||||
namespace Somnia.GuestLog.Services;
|
||||
|
||||
public class GuestDocumentService(
|
||||
ILogger<GuestDocumentService> logger,
|
||||
IDbContextFactory<GuestLogContext> contextFactory,
|
||||
IValidator<GuestDocumentDTO> dtoValidator
|
||||
) : IGuestDocumentService
|
||||
{
|
||||
public async Task<bool> CreateDocumentAsync(GuestDocumentDTO dto)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(dto);
|
||||
|
||||
var valResult = dtoValidator.Validate(dto);
|
||||
if (!valResult.IsValid)
|
||||
{
|
||||
logger.LogWarning("GuestDocumentService - CreateDocument: Dto validation failed. Fix the errors: {errorList}", valResult.Errors);
|
||||
return false;
|
||||
}
|
||||
|
||||
//Dto must be valid, so null warings shouldn't matter.
|
||||
GuestAddress addressItem = new()
|
||||
{
|
||||
Address = dto.Address,
|
||||
AddressCity = dto.AddressCity,
|
||||
AddressProvince = dto.AddressProvince,
|
||||
AddressState = dto.AddressState,
|
||||
AddressCountry = dto.AddressCountry!,
|
||||
ZipCode = dto.ZipCode,
|
||||
};
|
||||
|
||||
GuestDocument dbItem = new()
|
||||
{
|
||||
DocumentType = dto.DocumentType!,
|
||||
DocumentNum = dto.DocumentNum!,
|
||||
IssueDate = DateOnly.FromDateTime(dto.IssueDate!.Value),
|
||||
SupportNum = dto.SupportNum,
|
||||
Nationality = dto.Nationality!,
|
||||
Name = dto.Name!,
|
||||
Surname = dto.Surname!,
|
||||
Surname2 = dto.Surname2,
|
||||
BirthDate = DateOnly.FromDateTime(dto.BirthDate!.Value),
|
||||
Gender = dto.Gender,
|
||||
PhoneNumber = dto.PhoneNumber,
|
||||
EmailAddress = dto.EmailAddress,
|
||||
AddressDetails = addressItem,
|
||||
CreateTime = DateTime.UtcNow,
|
||||
UpdateTime = DateTime.UtcNow,
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
using var dbContext = contextFactory.CreateDbContext();
|
||||
await dbContext.AddAsync(dbItem);
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
logger.LogInformation("GuestDocumentService - CreateDocument: GuestDocument Created: '{id}'", dbItem.GetFullKey());
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "GuestDocumentService - CreateDocument: Error creating GuestDocument: '{id}'", dbItem.GetFullKey());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool UpdateDocument(GuestDocumentDTO dto)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
public async Task<GuestDocumentDTO?> GetDtoByIdAsync(string docType, string docNum)
|
||||
{
|
||||
using var dbContext = contextFactory.CreateDbContext();
|
||||
GuestDocument? dbItem = await dbContext.GuestDocuments.FindAsync(new { docType, docNum });
|
||||
return dbItem is not null ? GetDtoFromDb(dbItem) : null;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<GuestDocumentDTO>> GetDtoListAsync()
|
||||
{
|
||||
using var dbContext = contextFactory.CreateDbContext();
|
||||
return await dbContext.GuestDocuments.Select(x => GetDtoFromDb(x)).ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
public GuestDocumentDTO GetDtoFromDb(GuestDocument dbItem)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
DocumentType = dbItem.DocumentType,
|
||||
DocumentNum = dbItem.DocumentNum,
|
||||
IssueDate = dbItem.IssueDate.ToDateTime(TimeOnly.MinValue),
|
||||
SupportNum = dbItem.SupportNum,
|
||||
Nationality = dbItem.Nationality,
|
||||
Name = dbItem.Name,
|
||||
Surname = dbItem.Surname,
|
||||
Surname2 = dbItem.Surname2,
|
||||
BirthDate = dbItem.BirthDate.ToDateTime(TimeOnly.MinValue),
|
||||
Gender = dbItem.Gender,
|
||||
PhoneNumber = dbItem.PhoneNumber,
|
||||
EmailAddress = dbItem.EmailAddress,
|
||||
Address = dbItem.AddressDetails?.Address ?? "",
|
||||
AddressCity = dbItem.AddressDetails?.AddressCity ?? "",
|
||||
AddressProvince = dbItem.AddressDetails?.AddressProvince ?? "",
|
||||
AddressState = dbItem.AddressDetails?.AddressState ?? "",
|
||||
AddressCountry = dbItem.AddressDetails?.AddressCountry! ?? "",
|
||||
ZipCode = dbItem.AddressDetails?.ZipCode ?? "",
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using Somnia.GuestLog.Models.DTOs;
|
||||
|
||||
namespace Somnia.GuestLog.Services;
|
||||
|
||||
public interface IGuestDocumentService
|
||||
{
|
||||
public Task<bool> CreateDocumentAsync(GuestDocumentDTO dto);
|
||||
public bool UpdateDocument(GuestDocumentDTO dto);
|
||||
|
||||
public Task<GuestDocumentDTO?> GetDtoByIdAsync(string docType, string docNum);
|
||||
public Task<IEnumerable<GuestDocumentDTO>> GetDtoListAsync();
|
||||
|
||||
//TODO: Add Filter
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user