From 491ecc4cd102bbcc79b8aaf272b6820dee1272d26d913ea03b883a625f65ceeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa?= <93217193+Daniel-Garmig@users.noreply.github.com> Date: Thu, 10 Sep 2026 19:34:46 +0200 Subject: [PATCH] GuestDocument - Add edit mode. Added Edit mode for GuestDocument in GuestDocument details page. --- .../Pages/GuestDocuments/GuestDocDetail.razor | 107 ++++++++++++++++-- .../Pages/GuestDocuments/GuestDocNew.razor | 4 +- .../Resources/LanguageLocale.resx | 8 ++ .../Entity/GuestDocumentEntityService.cs | 72 +++++++++++- 4 files changed, 175 insertions(+), 16 deletions(-) diff --git a/src/Somnia.GuestLog/Components/Pages/GuestDocuments/GuestDocDetail.razor b/src/Somnia.GuestLog/Components/Pages/GuestDocuments/GuestDocDetail.razor index 1b769d8..1d5872a 100644 --- a/src/Somnia.GuestLog/Components/Pages/GuestDocuments/GuestDocDetail.razor +++ b/src/Somnia.GuestLog/Components/Pages/GuestDocuments/GuestDocDetail.razor @@ -9,6 +9,7 @@ @inject NavigationManager Navigation @inject IGuestDocumentEntityService guestDocService @inject IStayEntryEntityService stayService +@inject IDialogService DialogService Somnia - Guest Docs - Details @@ -16,14 +17,28 @@ Guest Document: @docId - @documentModel?.FullDocument - - @LanguageLocale.general_btnEdit - - - @LanguageLocale.general_btnReturn - + @if (_isEditMode) + { + + @LanguageLocale.general_btnCancel + + + @LanguageLocale.general_btnSave + + } + @if (!_isEditMode) + { + + @LanguageLocale.general_btnEdit + + + @LanguageLocale.general_btnReturn + + } @if (documentModel is not null) @@ -32,10 +47,11 @@ Centered> - + - + Total Stays: @(guestStaysLoading ? "Loading..." : guestStayList.Count()) @@ -87,8 +103,16 @@ IEnumerable guestStayList = []; bool guestStaysLoading = true; + private bool _isEditMode = false; + protected override async Task OnInitializedAsync() + { + //Get model from db. + await LoadGuestDocumentAsync(); + } + + private async Task LoadGuestDocumentAsync() { //Get model from db. documentModel = await guestDocService.GetLastVersionDtoByIdAsync(docId); @@ -121,9 +145,68 @@ #region Event Handlers - private void BtnEdit() + private void BtnEnableEdit() { - userMessage.ShowMessage("Not implemented yet!", MessageKind.Info); + Debug.Assert(_isEditMode == false); + + userMessage.ShowMessage(LanguageLocale.page_GuestDocDetail_msgEditEnabled, MessageKind.Normal); + _isEditMode = true; + } + + private async Task BtnCancelEdit() + { + Debug.Assert(_isEditMode == true); + + var result = await DialogService.ShowMessageBoxAsync( + LanguageLocale.general_confirmTitle, LanguageLocale.page_GuestDocDetail_msgConfirmCancelEdit, + yesText: "OK", cancelText: LanguageLocale.general_btnCancel); + + if (result == null || result.Value == false) + { + // Cancel cancel -> Continue with edit. + return; + } + + //Cancel edit -> Reload data from DB. + await LoadGuestDocumentAsync(); + + userMessage.ShowMessage(LanguageLocale.general_msgEditCanceled, MessageKind.Normal); + _isEditMode = false; + } + + private async Task BtnSaveEdit() + { + Debug.Assert(_isEditMode == true && documentModel is not null); + + var confirmResult = await DialogService.ShowMessageBoxAsync( + LanguageLocale.general_confirmTitle, LanguageLocale.page_GuestDocDetail_msgConfirmSaveEdit, + yesText: "OK", cancelText: LanguageLocale.general_btnCancel); + + if (confirmResult == null || confirmResult.Value == false) + { + // Cancel save -> Continue with edit. + return; + } + + //Show Loading dialog... + var loadingDialog = await DialogService.ShowAsync(); + + //Save edit. + Result updateResult = await guestDocService.UpdateDocumentAsync(documentModel); + if (!updateResult.IsSuccess) + { + string errorMsg = string.Format(LanguageLocale.page_GuestDocDetail_msgEditError, documentModel.FullDocument); + userMessage.ShowResultWithMessage(updateResult, errorMsg); + loadingDialog.Close(); + return; + } + + //Reload data from DB. + await LoadGuestDocumentAsync(); + loadingDialog.Close(); + userMessage.ShowMessage(LanguageLocale.general_msgEditSaved, MessageKind.Success); + + _isEditMode = false; } private void BtnReturn() diff --git a/src/Somnia.GuestLog/Components/Pages/GuestDocuments/GuestDocNew.razor b/src/Somnia.GuestLog/Components/Pages/GuestDocuments/GuestDocNew.razor index 1f7bf65..7d0b106 100644 --- a/src/Somnia.GuestLog/Components/Pages/GuestDocuments/GuestDocNew.razor +++ b/src/Somnia.GuestLog/Components/Pages/GuestDocuments/GuestDocNew.razor @@ -144,13 +144,13 @@ Result createResult = await guestDocService.CreateDocumentAsync(model); if (!createResult.IsSuccess) { - string errorMsg = $"Error creating Document '{model.DocumentNum}'"; + string errorMsg = $"Error creating Document '{model.FullDocument}'"; userMessage.ShowResultWithMessage(createResult, errorMsg); loadingDialog.Close(); return; } - userMessage.ShowMessage($"Document '{model.DocumentNum}' Created!", MessageKind.Success); + userMessage.ShowMessage($"Document '{model.FullDocument}' Created!", MessageKind.Success); Navigation.NavigateTo(GuestConsts.Page_GuestDocs_List); } diff --git a/src/Somnia.GuestLog/Resources/LanguageLocale.resx b/src/Somnia.GuestLog/Resources/LanguageLocale.resx index 73211ec..1ccfb45 100644 --- a/src/Somnia.GuestLog/Resources/LanguageLocale.resx +++ b/src/Somnia.GuestLog/Resources/LanguageLocale.resx @@ -182,6 +182,9 @@ Use '%' as wildcard Date Start Date End + Loading... + Changes saved... + Edit Canceled! Address City @@ -224,6 +227,11 @@ This Guest Document Already exists!! Go to existing document + Entered Edit mode + Save changes to GuestDocument + Cancel edit. Changes will be lost! + Error saving Document '{0}' + Stay Entries Entry List New Stay diff --git a/src/Somnia.GuestLog/Services/Entity/GuestDocumentEntityService.cs b/src/Somnia.GuestLog/Services/Entity/GuestDocumentEntityService.cs index 50eb131..8d11d29 100644 --- a/src/Somnia.GuestLog/Services/Entity/GuestDocumentEntityService.cs +++ b/src/Somnia.GuestLog/Services/Entity/GuestDocumentEntityService.cs @@ -76,9 +76,77 @@ public class GuestDocumentEntityService( } } - public Task UpdateDocumentAsync(GuestDocumentDTO dto) + public async Task UpdateDocumentAsync(GuestDocumentDTO dto) { - throw new NotImplementedException(); + var valResult = dtoValidator.Validate(dto); + if (!valResult.IsValid) + { + logger.LogWarning("GuestDocumentService - UpdateDocument: Dto validation failed. Fix the errors: {errorList}", valResult.Errors); + return Result.Fail(ResultCode.FailedValidation); + } + + try + { + using var dbContext = contextFactory.CreateDbContext(); + //Get current entity from DB. + GuestDocument? dbItem = await dbContext.FindAsync(dto.Id, dto.Version); + + if (dbItem == null) + { + logger.LogWarning("GuestDocumentService - UpdateDocument: Error updating GuestDocument (ID: '{id}'): GuestDocument not found!", dto.FullKey); + return Result.Fail(ResultCode.NotFound); + } + + //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, + }; + + DateTime updateTime = DateTime.UtcNow; + GuestDocument updatedItem = new() + { + Id = dbItem.Id, + Version = dbItem.Version, + DocumentType = dbItem.DocumentType, + DocumentNum = dbItem.DocumentNum, + IssueDate = dto.IssueDate != null ? DateOnly.FromDateTime(dto.IssueDate!.Value) : null, + 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 = dbItem.CreateTime, + UpdateTime = updateTime, + IsLastVersion = dbItem.IsLastVersion, + }; + + dbContext.Entry(dbItem).CurrentValues.SetValues(updatedItem); + var result = await dbContext.SaveChangesAsync(); + + if (result != 1) + { + logger.LogWarning("GuestDocumentService - UpdateDocument: Unexpected updated entity count: {count}", result); + } + + logger.LogInformation("GuestDocumentService - UpdateDocument: GuestDocument Updated OK {id}", dbItem.GetFullKey()); + return Result.Ok(); + } + catch (Exception ex) + { + logger.LogError(ex, "GuestDocumentService - UpdateDocument: Error updating GuestDocument: '{id}'", dto.FullKey); + return Result.FailUnknownError(); + } } public Task DeleteDocumentAsync(GuestDocumentDTO dto)