using Netina.Domain.MartenEntities.Faqs; namespace Netina.AdminPanel.PWA.Pages; public class FaqManagementPageViewModel( IDialogService dialogService, NavigationManager navigationManager, IRestWrapper restWrapper, ISnackbar snackbar, IUserUtility userUtility) : BaseViewModel> (userUtility) { public int CurrentPage = 0; public int PageCount = 2; public override async Task InitializeAsync() { await GetEntitiesAsync(); await base.InitializeAsync(); } public async Task GetEntitiesAsync() { try { var token = await _userUtility.GetBearerTokenAsync(); if (token == null) throw new Exception("Token is null"); IsProcessing = true; var faqs = await restWrapper.FaqApiRest.GetFaqs(CurrentPage, token); PageDto.Clear(); faqs.ForEach(f => PageDto.Add(f)); if (PageDto.Count % 20 == 0) PageCount = CurrentPage + 2; } catch (ApiException ex) { var exe = await ex.GetContentAsAsync(); if (ex.StatusCode == HttpStatusCode.Unauthorized) { await userUtility.LogoutAsync(); navigationManager.NavigateTo("login", true, true); } snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error); } catch (Exception e) { snackbar.Add(e.Message, Severity.Error); } finally { IsProcessing = false; } } public async Task ChangePageAsync(int page) { CurrentPage = page - 1; await GetEntitiesAsync(); } public async Task AddClicked() { DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true }; var reference = await dialogService.ShowAsync("افزودن سوالات متداول جدید", maxWidth); var result = await reference.Result; if (result.Data is bool and true) await InitializeAsync(); } public async Task EditClicked(BaseFaq item) { DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true }; var parameters = new DialogParameters(); parameters.Add(x => x.Faq, item); var reference = await dialogService.ShowAsync($"ویرایش سوالات متداول {item.Title}", parameters, maxWidth); var result = await reference.Result; if (result.Data is bool and true) await InitializeAsync(); } public async Task DeleteAsync(Guid selectedId) { var reference = await dialogService.ShowQuestionDialog($"آیا از حذف سوالات متداول مورد نظر اطمینان دارید ?"); var result = await reference.Result; if (!result.Canceled) { try { IsProcessing = true; var token = await _userUtility.GetBearerTokenAsync(); if (token == null) throw new Exception("Token is null"); await restWrapper.FaqApiRest.Delete(selectedId, token); await InitializeAsync(); snackbar.Add("حذف سوالات متداول با موفقیت انجام شد", Severity.Success); } catch (ApiException ex) { var exe = await ex.GetContentAsAsync(); snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error); } catch (Exception e) { snackbar.Add(e.Message, Severity.Error); } finally { IsProcessing = false; } } } }