GuestLog - GuestDocument pages initial implementation

This commit is contained in:
2026-06-21 17:10:27 +02:00
parent 86fadf97b4
commit 76b2695c02
24 changed files with 832 additions and 54 deletions
+1
View File
@@ -6,6 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Extensions.MudBlazor.StaticInput" Version="4.1.0" />
<PackageVersion Include="FluentValidation" Version="12.1.1" />
<PackageVersion Include="Grpc.AspNetCore" Version="2.64.0" />
<PackageVersion Include="MudBlazor" Version="9.4.0" />
</ItemGroup>
@@ -0,0 +1,92 @@
@using Somnia.GuestLog.Models.DTOs;
@using Somnia.GuestLog.Validators;
<MudPaper Class="pa-4">
<MudForm @ref="@_form" Model="@Model" Validation="@(guestDocValidator.ValidateValue)" ValidationDelay="0">
<MudSelect @bind-Value="Model.DocumentType" For="@(() => Model.DocumentType)" Label="DocumentType" Required>
@foreach (string docType in GuestConsts.DocumentTypeList)
{
<MudSelectItem Value="docType">@docType</MudSelectItem>
}
</MudSelect>
<MudTextField @bind-Value="Model.DocumentNum" For="@(() => Model.DocumentNum)" Label="Document Num"
Immediate="true" Required />
<MudDatePicker @bind-Date="Model.IssueDate" For="@(() => Model.IssueDate)" Label="Issue Date" Clearable
OpenTo="OpenTo.Year" Editable />
<MudTextField @bind-Value="Model.SupportNum" For="@(() => Model.SupportNum)" Label="Support Num"
Immediate="true" Required="@isSuportNumberRequired" />
<MudSelect @bind-Value="Model.Nationality" For="@(() => Model.Nationality)" Label="Nationality">
@foreach (var countryPair in GuestConsts.CountryList)
{
<MudSelectItem Value="countryPair.Key">@countryPair.Value</MudSelectItem>
}
</MudSelect>
<MudDivider DividerType="DividerType.Middle" Class="mt-6" />
<MudText Typo="Typo.subtitle1" Align="Align.Center">Customer Details</MudText>
<MudDivider DividerType="DividerType.Middle" Class="mb-4" />
<MudTextField @bind-Value="Model.Name" For="@(() => Model.Name)" Immediate="true" Label="Name" />
<MudTextField @bind-Value="Model.Surname" For="@(() => Model.Surname)" Immediate="true" Label="Surname" />
<MudTextField @bind-Value="Model.Surname2" For="@(() => Model.Surname2)" Immediate="true" Label="Surname2" />
<MudDatePicker @bind-Date="Model.BirthDate" For="@(() => Model.BirthDate)" Label="BirthDate" Clearable
OpenTo="OpenTo.Year" Editable />
<MudSelect @bind-Value="Model.Gender" For="@(() => Model.Gender)" Label="Gender">
@foreach (var genderPair in GuestConsts.GenderList)
{
<MudSelectItem Value="genderPair.Key">@genderPair.Value</MudSelectItem>
}
</MudSelect>
<MudDivider DividerType="DividerType.Middle" Class="mt-6" />
<MudText Typo="Typo.subtitle1" Align="Align.Center">Contact Details</MudText>
<MudDivider DividerType="DividerType.Middle" Class="mb-4" />
<MudTextField @bind-Value="Model.PhoneNumber" For="@(() => Model.PhoneNumber)" Immediate="true"
Label="Phone Number" />
<MudTextField @bind-Value="Model.EmailAddress" For="@(() => Model.EmailAddress)" Immediate="true"
Label="Email" />
<MudDivider DividerType="DividerType.Middle" Class="mt-6" />
<MudText Typo="Typo.subtitle1" Align="Align.Center">Address Details</MudText>
<MudDivider DividerType="DividerType.Middle" Class="mb-4" />
<MudTextField @bind-Value="Model.Address" For="@(() => Model.Address)" Immediate="true" Label="Address" />
<MudTextField @bind-Value="Model.AddressCity" For="@(() => Model.AddressCity)" Immediate="true" Label="City" />
<MudTextField @bind-Value="Model.AddressProvince" For="@(() => Model.AddressProvince)" Immediate="true"
Label="Province" />
<MudTextField @bind-Value="Model.AddressState" For="@(() => Model.AddressState)" Immediate="true"
Label="State" />
<MudSelect @bind-Value="Model.AddressCountry" For="@(() => Model.AddressCountry)" Label="Country">
@foreach (var countryPair in GuestConsts.CountryList)
{
<MudSelectItem Value="countryPair.Key">@countryPair.Value</MudSelectItem>
}
</MudSelect>
<MudTextField @bind-Value="Model.ZipCode" For="@(() => Model.ZipCode)" Immediate="true" Label="ZipCode" />
</MudForm>
</MudPaper>
@code {
[Parameter]
public GuestDocumentDTO Model { get; set; } = new();
private MudForm _form = null!;
private readonly GuestDocumentDTOValidator guestDocValidator = new();
private bool isSuportNumberRequired { get => Model?.Nationality == "ESP"; }
public async Task<bool> ValidateForm()
{
await _form.ValidateAsync();
return _form.IsValid;
}
}
@@ -5,6 +5,7 @@
<MudNavLink Href="weather" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">Weather</MudNavLink>
<MudNavLink Href="@GuestConsts.Page_GuestDocs_List" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.DocumentScanner">Guests</MudNavLink>
</MudNavMenu>
@@ -0,0 +1,141 @@
@attribute [Route($"{GuestConsts.Page_GuestDocs_List}")]
@using Microsoft.EntityFrameworkCore;
@using Somnia.GuestLog.Models.Entities;
@using Somnia.GuestLog.Infrastructure;
@inject IDbContextFactory<GuestLogContext> contextFactory
@inject NavigationManager Navigation
@inject ISnackbar Snackbar
<PageTitle>Somnia - Guest Docs</PageTitle>
<MudStack Row="true" Class="my-4">
<MudText Typo="Typo.h4" GutterBottom="true">Guest Documents</MudText>
<MudSpacer />
<MudButton Variant="Variant.Filled" Color="Color.Success" Size="Size.Small" StartIcon="@Icons.Material.Filled.Add"
OnClick="() => BtnNewGuest()">
New Guest
</MudButton>
</MudStack>
<MudSelect T="string" MultiSelection="true" FullWidth="true" Dense="true" Clearable="true" Margin="@Margin.Dense"
Class="filter-input category-filter" Label="Module" Placeholder="Select Modules"
SelectedValuesChanged="@FilterModuleValuesChanged">
@foreach (var module in _moduleList)
{
<MudSelectItem T="string" Value="@module">@module</MudSelectItem>
}
</MudSelect>
<MudDataGrid @ref="_dataGrid" T="GuestDocument" Items="@guestDocList" Loading="@_dataLoading" ReadOnly="true"
Hideable="true" ColumnsPanelReordering="true">
<ToolBarContent>
<MudText Typo="Typo.subtitle1">Documents List</MudText>
<MudStack Class="ml-auto" Row="true" AlignItems="AlignItems.End">
<MudButton StartIcon="@Icons.Material.Outlined.Refresh" Variant="Variant.Outlined" Size="Size.Small"
OnClick="() => LoadDataAsync()">
Reload Data
</MudButton>
<MudButton StartIcon="@Icons.Material.Outlined.ViewColumn" Variant="Variant.Outlined" Size="Size.Small"
OnClick="(args) => BtnConfigureColumnsClick(args)">
Configure Columns
</MudButton>
</MudStack>
</ToolBarContent>
<Columns>
<PropertyColumn Property="x => x.DocumentType" Hideable="false" />
<PropertyColumn Property="x => x.DocumentNum" Hideable="false" />
<PropertyColumn Property="x => x.Name" />
<PropertyColumn Property="x => x.Surname" />
<PropertyColumn Property="x => x.Nationality" />
<PropertyColumn Property="x => x.UpdateTime" />
<TemplateColumn CellClass="d-flex justify-between" Title="Options" StickyRight Hideable="false"
ShowColumnOptions="false">
<CellTemplate>
<MudIconButton Size="@Size.Small" Icon="@Icons.Material.Outlined.Info"
OnClick="() => BtnViewDetails(context.Item)" />
</CellTemplate>
</TemplateColumn>
</Columns>
</MudDataGrid>
@code {
private IEnumerable<GuestDocument> guestDocList = [];
private bool _dataLoading = true;
private HashSet<string> _moduleList = [];
private HashSet<string> _selectedModuleList = [];
private MudDataGrid<GuestDocument>? _dataGrid;
protected override async Task OnInitializedAsync()
{
await LoadDataAsync(showMsg: false);
@* _moduleList = guestDocList.Select(e => e.Module).ToHashSet(); *@
}
#region Data Loading
private async Task LoadDataAsync(bool showMsg = true)
{
_dataLoading = true;
using var dbContext = await contextFactory.CreateDbContextAsync();
IQueryable<GuestDocument> query = dbContext.GuestDocuments.AsQueryable();
@* if (_selectedModuleList.Count > 0)
{
query = query.Where(c => _selectedModuleList.Contains(c.Module));
} *@
guestDocList = await query
.OrderByDescending(e => e.UpdateTime)
.ToListAsync();
if (showMsg)
{
Snackbar.Add("Data loaded from DB", Severity.Info, o => o.VisibleStateDuration = 1000);
}
_dataLoading = false;
}
#endregion
#region Event Handlers
private async Task FilterModuleValuesChanged(IReadOnlyCollection<string> newValues)
{
_selectedModuleList = [.. newValues];
await LoadDataAsync();
}
private async Task BtnConfigureColumnsClick(MouseEventArgs args)
{
if (_dataGrid is null)
{
Snackbar.Add("No ref to grid");
return;
}
_dataGrid.ShowColumnsPanel(args);
}
private Task BtnViewDetails(GuestDocument item)
{
Snackbar.Add($"Show details of {item.DocumentNum}", Severity.Info);
return Task.CompletedTask;
}
private void BtnNewGuest()
{
Navigation.NavigateTo(GuestConsts.Page_GuestDocs_New);
}
#endregion
}
@@ -0,0 +1,68 @@
@attribute [Route($"{GuestConsts.Page_GuestDocs_New}")]
@using Microsoft.EntityFrameworkCore;
@using Somnia.GuestLog.Models.DTOs;
@using Somnia.GuestLog.Infrastructure;
@using Somnia.GuestLog.Services;
@inject IDbContextFactory<GuestLogContext> contextFactory
@inject ISnackbar Snackbar
@inject NavigationManager Navigation
@inject IGuestDocumentService guestDocService
<PageTitle>Somnia - Guest Docs - New</PageTitle>
<MudStack Row="true" Class="my-4">
<MudText Typo="Typo.h4" GutterBottom="true">New Guest Document</MudText>
<MudSpacer />
<MudButton Variant="Variant.Filled" Color="Color.Success" Size="Size.Small" StartIcon="@Icons.Material.Filled.Add"
OnClick="() => BtnSaveNewGuest()">
Create
</MudButton>
<MudButton Variant="Variant.Filled" Color="Color.Default" Size="Size.Small"
StartIcon="@Icons.Material.Filled.ArrowBack" OnClick="() => BtnReturn()">
Return
</MudButton>
</MudStack>
<GuestDocForm @ref="_form" />
@code {
GuestDocForm _form = null!;
#region Event Handlers
private void BtnReturn()
{
Navigation.NavigateTo(GuestConsts.Page_GuestDocs_List);
}
private async Task BtnSaveNewGuest()
{
bool isFormValid = await _form.ValidateForm();
if (!isFormValid)
{
Snackbar.Add("Invalid Form!", Severity.Error);
return;
}
GuestDocumentDTO model = _form.Model;
Snackbar.Add($"Document '{model.DocumentNum}' creating...", Severity.Info);
bool isCreated = await guestDocService.CreateDocumentAsync(model);
if (!isCreated)
{
Snackbar.Add($"Error creating Document '{model.DocumentNum}'", Severity.Error);
return;
}
Navigation.NavigateTo(GuestConsts.Page_GuestDocs_List);
}
#endregion
}
@@ -0,0 +1 @@
@using Somnia.GuestLog.Components.Controls
+27
View File
@@ -3,4 +3,31 @@ namespace Somnia.GuestLog;
public static class GuestConsts
{
public const string Page_GuestDocs_List = "/guest-documents";
public const string Page_GuestDocs_New = "/guest-documents/new";
public static HashSet<string> DocumentTypeList { get; private set; } = [
"DNI",
"NIE",
"Passport",
"ID Card",
"Other",
];
public static Dictionary<string, string> CountryList { get; private set; } = new([
new KeyValuePair<string, string>("ESP", "Spain"),
new KeyValuePair<string, string>("ITA", "Italy"),
new KeyValuePair<string, string>("USA", "United States")
]);
public static Dictionary<string, string> GenderList { get; private set; } = new([
new KeyValuePair<string, string>("M", "Male"),
new KeyValuePair<string, string>("F", "Female"),
new KeyValuePair<string, string>("O", "Other")
]);
public const string regexPattern_phoneNum = @"\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}";
}
@@ -1,30 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Somnia.GuestLog.Models;
namespace Somnia.GuestLog.Infrastructure.EntityConfigs;
public class GuestAddressEntityConfig : IEntityTypeConfiguration<GuestAddress>
{
public void Configure(EntityTypeBuilder<GuestAddress> builder)
{
builder.HasNoKey();
builder.Property(e => e.Address)
.HasMaxLength(128);
builder.Property(e => e.AddressCity)
.HasMaxLength(128);
builder.Property(e => e.AddressProvince)
.HasMaxLength(128);
builder.Property(e => e.AddressState)
.HasMaxLength(128);
//3 letter code: ISO 3166-1 Alfa-3
builder.Property(e => e.AddressCountry)
.IsRequired()
.HasMaxLength(3);
builder.Property(e => e.ZipCode)
.HasMaxLength(3);
}
}
@@ -1,6 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Somnia.GuestLog.Models;
using Somnia.GuestLog.Models.Entities;
namespace Somnia.GuestLog.Infrastructure.EntityConfigs;
@@ -49,12 +49,6 @@ public class GuestDocumentEntityConfig : IEntityTypeConfiguration<GuestDocument>
.HasMaxLength(256);
//Set to now on creation.
builder.Property(e => e.CreateTime)
.HasDefaultValueSql("current_timestamp");
builder.Property(e => e.UpdateTime)
.HasDefaultValueSql("current_timestamp");
var addressBuilder = builder.ComplexProperty(e => e.AddressDetails);
addressBuilder.Property(e => e.Address)
@@ -72,6 +66,6 @@ public class GuestDocumentEntityConfig : IEntityTypeConfiguration<GuestDocument>
.HasMaxLength(3);
addressBuilder.Property(e => e.ZipCode)
.HasMaxLength(3);
.HasMaxLength(12);
}
}
@@ -1,6 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Somnia.GuestLog.Infrastructure.EntityConfigs;
using Somnia.GuestLog.Models;
using Somnia.GuestLog.Models.Entities;
namespace Somnia.GuestLog.Infrastructure;
@@ -0,0 +1,121 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Somnia.GuestLog.Infrastructure;
#nullable disable
namespace Somnia.GuestLog.Infrastructure.Migrations
{
[DbContext(typeof(GuestLogContext))]
[Migration("20260621150802_GuestDocumentFixes")]
partial class GuestDocumentFixes
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "10.0.7")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Somnia.GuestLog.Models.Entities.GuestDocument", b =>
{
b.Property<string>("DocumentType")
.HasMaxLength(8)
.HasColumnType("character varying(8)");
b.Property<string>("DocumentNum")
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<DateOnly>("BirthDate")
.HasColumnType("date");
b.Property<DateTime>("CreateTime")
.HasColumnType("timestamp with time zone");
b.Property<string>("EmailAddress")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<string>("Gender")
.HasMaxLength(1)
.HasColumnType("character varying(1)");
b.Property<DateOnly>("IssueDate")
.HasColumnType("date");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<string>("Nationality")
.IsRequired()
.HasMaxLength(3)
.HasColumnType("character varying(3)");
b.Property<string>("PhoneNumber")
.HasMaxLength(32)
.HasColumnType("character varying(32)");
b.Property<string>("SupportNum")
.HasMaxLength(10)
.HasColumnType("character varying(10)");
b.Property<string>("Surname")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<string>("Surname2")
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<DateTime>("UpdateTime")
.HasColumnType("timestamp with time zone");
b.ComplexProperty(typeof(Dictionary<string, object>), "AddressDetails", "Somnia.GuestLog.Models.Entities.GuestDocument.AddressDetails#GuestAddress", b1 =>
{
b1.Property<string>("Address")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b1.Property<string>("AddressCity")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b1.Property<string>("AddressCountry")
.IsRequired()
.HasMaxLength(3)
.HasColumnType("character varying(3)");
b1.Property<string>("AddressProvince")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b1.Property<string>("AddressState")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b1.Property<string>("ZipCode")
.HasMaxLength(12)
.HasColumnType("character varying(12)");
});
b.HasKey("DocumentType", "DocumentNum");
b.ToTable("GuestDocument", (string)null);
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,77 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Somnia.GuestLog.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class GuestDocumentFixes : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<DateTime>(
name: "UpdateTime",
table: "GuestDocument",
type: "timestamp with time zone",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "timestamp with time zone",
oldDefaultValueSql: "current_timestamp");
migrationBuilder.AlterColumn<DateTime>(
name: "CreateTime",
table: "GuestDocument",
type: "timestamp with time zone",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "timestamp with time zone",
oldDefaultValueSql: "current_timestamp");
migrationBuilder.AlterColumn<string>(
name: "AddressDetails_ZipCode",
table: "GuestDocument",
type: "character varying(12)",
maxLength: 12,
nullable: true,
oldClrType: typeof(string),
oldType: "character varying(3)",
oldMaxLength: 3,
oldNullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<DateTime>(
name: "UpdateTime",
table: "GuestDocument",
type: "timestamp with time zone",
nullable: false,
defaultValueSql: "current_timestamp",
oldClrType: typeof(DateTime),
oldType: "timestamp with time zone");
migrationBuilder.AlterColumn<DateTime>(
name: "CreateTime",
table: "GuestDocument",
type: "timestamp with time zone",
nullable: false,
defaultValueSql: "current_timestamp",
oldClrType: typeof(DateTime),
oldType: "timestamp with time zone");
migrationBuilder.AlterColumn<string>(
name: "AddressDetails_ZipCode",
table: "GuestDocument",
type: "character varying(3)",
maxLength: 3,
nullable: true,
oldClrType: typeof(string),
oldType: "character varying(12)",
oldMaxLength: 12,
oldNullable: true);
}
}
}
@@ -23,7 +23,7 @@ namespace Somnia.GuestLog.Infrastructure.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Somnia.GuestLog.Models.GuestDocument", b =>
modelBuilder.Entity("Somnia.GuestLog.Models.Entities.GuestDocument", b =>
{
b.Property<string>("DocumentType")
.HasMaxLength(8)
@@ -37,9 +37,7 @@ namespace Somnia.GuestLog.Infrastructure.Migrations
.HasColumnType("date");
b.Property<DateTime>("CreateTime")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasDefaultValueSql("current_timestamp");
.HasColumnType("timestamp with time zone");
b.Property<string>("EmailAddress")
.HasMaxLength(256)
@@ -80,11 +78,9 @@ namespace Somnia.GuestLog.Infrastructure.Migrations
.HasColumnType("character varying(64)");
b.Property<DateTime>("UpdateTime")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasDefaultValueSql("current_timestamp");
.HasColumnType("timestamp with time zone");
b.ComplexProperty(typeof(Dictionary<string, object>), "AddressDetails", "Somnia.GuestLog.Models.GuestDocument.AddressDetails#GuestAddress", b1 =>
b.ComplexProperty(typeof(Dictionary<string, object>), "AddressDetails", "Somnia.GuestLog.Models.Entities.GuestDocument.AddressDetails#GuestAddress", b1 =>
{
b1.Property<string>("Address")
.HasMaxLength(128)
@@ -108,8 +104,8 @@ namespace Somnia.GuestLog.Infrastructure.Migrations
.HasColumnType("character varying(128)");
b1.Property<string>("ZipCode")
.HasMaxLength(3)
.HasColumnType("character varying(3)");
.HasMaxLength(12)
.HasColumnType("character varying(12)");
});
b.HasKey("DocumentType", "DocumentNum");
@@ -0,0 +1,30 @@
namespace Somnia.GuestLog.Models.DTOs;
public class GuestDocumentDTO
{
public string? DocumentType { get; set; }
public string? DocumentNum { get; set; }
public DateTime? IssueDate { get; set; }
public string? SupportNum { get; set; }
public string? Nationality { get; set; }
public string? Name { get; set; }
public string? Surname { get; set; }
public string? Surname2 { get; set; }
public DateTime? BirthDate { get; set; }
public string? Gender { get; set; }
public string? PhoneNumber { get; set; }
public string? EmailAddress { get; set; }
public string? Address { get; set; }
public string? AddressCity { get; set; }
public string? AddressProvince { get; set; }
public string? AddressState { get; set; }
public string? AddressCountry { get; set; }
public string? ZipCode { get; set; }
}
@@ -1,4 +1,4 @@
namespace Somnia.GuestLog.Models;
namespace Somnia.GuestLog.Models.Entities;
public abstract class BaseEntity
{
@@ -1,4 +1,4 @@
namespace Somnia.GuestLog.Models;
namespace Somnia.GuestLog.Models.Entities;
public class GuestAddress : BaseEntity
{
@@ -1,4 +1,4 @@
namespace Somnia.GuestLog.Models;
namespace Somnia.GuestLog.Models.Entities;
public class GuestDocument : BaseEntity
{
@@ -22,8 +22,10 @@ public class GuestDocument : BaseEntity
public string? PhoneNumber { get; set; }
public string? EmailAddress { get; set; }
//Generated Properties.
public required DateTime CreateTime { get; set; }
public required DateTime UpdateTime { get; set; }
public override string GetFullKey() => $"{DocumentType}-{DocumentNum}";
}
+10
View File
@@ -1,7 +1,11 @@
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using MudBlazor.Services;
using Somnia.GuestLog.Components;
using Somnia.GuestLog.Infrastructure;
using Somnia.GuestLog.Models.DTOs;
using Somnia.GuestLog.Services;
using Somnia.GuestLog.Validators;
var builder = WebApplication.CreateBuilder(args);
@@ -21,6 +25,12 @@ builder.Services.AddMudServices();
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
// Add Validation Services
builder.Services.AddSingleton<IValidator<GuestDocumentDTO>, GuestDocumentDTOValidator>();
// Add Services
builder.Services.AddSingleton<IGuestDocumentService, GuestDocumentService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -4,7 +4,7 @@
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5240",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
@@ -13,7 +13,7 @@
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7120;http://localhost:5240",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
@@ -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
}
@@ -8,6 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentValidation" />
<PackageReference Include="MudBlazor" />
</ItemGroup>
@@ -0,0 +1,26 @@
using FluentValidation;
namespace Somnia.GuestLog.Utils;
/// <summary>
/// A glue class to make it easy to define validation rules for single values using FluentValidation
/// You can reuse this class for all your fields, like for the credit card rules above.
/// </summary>
/// <typeparam name="T"></typeparam>
public class FluentValueValidator<T> : AbstractValidator<T>
{
public FluentValueValidator(Action<IRuleBuilderInitial<T, T>> rule)
{
rule(RuleFor(x => x));
}
private IEnumerable<string> ValidateValue(T arg)
{
var result = Validate(arg);
if (result.IsValid)
return new string[0];
return result.Errors.Select(e => e.ErrorMessage);
}
public Func<T, IEnumerable<string>> Validation => ValidateValue;
}
@@ -0,0 +1,88 @@
using FluentValidation;
using Somnia.GuestLog.Models.DTOs;
namespace Somnia.GuestLog.Validators;
/// <summary>
/// Validator for GuestDocumentDTO which contains multiple rules and can be shared with the back end API
/// </summary>
public class GuestDocumentDTOValidator : AbstractValidator<GuestDocumentDTO>
{
public GuestDocumentDTOValidator()
{
RuleFor(x => x.DocumentType)
.NotEmpty()
.Must(val => val != null && GuestConsts.DocumentTypeList.Contains(val));
RuleFor(x => x.DocumentNum)
.NotEmpty()
.Length(6, 32);
RuleFor(x => x.IssueDate)
.NotEmpty()
.LessThanOrEqualTo(DateTime.Today);
RuleFor(x => x.Name)
.NotEmpty()
.Length(1, 100);
RuleFor(x => x.Surname)
.NotEmpty()
.Length(1, 100);
RuleFor(x => x.BirthDate)
.NotEmpty()
.LessThanOrEqualTo(DateTime.Today);
RuleFor(x => x.Nationality)
.NotEmpty()
.Must(val => val != null && GuestConsts.CountryList.ContainsKey(val));
//Contact Fields
RuleFor(x => x.EmailAddress)
.EmailAddress()
.When(x => !string.IsNullOrEmpty(x.EmailAddress));
RuleFor(x => x.PhoneNumber)
.Matches(GuestConsts.regexPattern_phoneNum)
.When(x => !string.IsNullOrEmpty(x.PhoneNumber));
RuleFor(x => x.AddressCountry)
.NotEmpty()
.Must(val => val != null && GuestConsts.CountryList.ContainsKey(val));
// RuleFor(x => x.Email)
// .Cascade(CascadeMode.Stop)
// .NotEmpty()
// .EmailAddress()
// .MustAsync(async (value, _) => await IsUniqueAsync(value));
// RuleFor(x => x.CCNumber)
// .NotEmpty()
// .Length(1, 100)
// .CreditCard();
// RuleFor(x => x.Address.Address)
// .NotEmpty()
// .Length(1, 100);
// RuleFor(x => x.Address.City)
// .NotEmpty()
// .Length(1, 100);
// RuleFor(x => x.Address.Country)
// .NotEmpty()
// .Length(1, 100);
// RuleForEach(x => x.OrderDetails)
// .SetValidator(new OrderDetailsModelFluentValidator());
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<GuestDocumentDTO>.CreateWithOptions((GuestDocumentDTO)model, x =>
x.IncludeProperties(propertyName)));
return result.IsValid ? [] : result.Errors.Select(e => e.ErrorMessage);
};
}