AdminPanel/Netina.AdminPanel.PWA/Dialogs/FaqActionDialogBox.razor.cs

105 lines
3.7 KiB
C#

using Netina.Domain.MartenEntities.Faqs;
namespace Netina.AdminPanel.PWA.Dialogs;
public class FaqActionDialogBoxViewModel : BaseViewModel<BaseFaq>
{
private readonly IRestWrapper _restWrapper;
private readonly ISnackbar _snackbar;
private readonly MudDialogInstance _dialogInstance;
public bool IsEditing = false;
public FaqActionDialogBoxViewModel(IUserUtility userUtility, ISnackbar snackbar, IRestWrapper restWrapper, MudDialogInstance dialogInstance) : base(userUtility)
{
_snackbar = snackbar;
_restWrapper = restWrapper;
_dialogInstance = dialogInstance;
}
public FaqActionDialogBoxViewModel(BaseFaq faq,IUserUtility userUtility, ISnackbar snackbar, IRestWrapper restWrapper, MudDialogInstance dialogInstance) : base(userUtility)
{
PageDto = faq;
IsEditing = true;
_snackbar = snackbar;
_restWrapper = restWrapper;
_dialogInstance = dialogInstance;
}
public string Question { get; set; } = string.Empty;
public string Answer { get; set; } = string.Empty;
public void AddFaq()
{
try
{
if (Question.IsNullOrEmpty())
throw new Exception("سوال را وارد کنید");
if (Answer.IsNullOrEmpty())
throw new Exception("پاسخ را وارد کنید");
PageDto.Faqs.Add(Question,Answer);
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
}
}
public async Task SubmitAsync()
{
try
{
var token = await _userUtility.GetBearerTokenAsync();
if (token == null)
throw new Exception("token is null");
if (PageDto.Slug.IsNullOrEmpty())
throw new Exception("اسلاگ صفحه سوال متداول را وارد کنید");
if (PageDto.Title.IsNullOrEmpty())
throw new Exception("عنوان صفحه سوالات متداول را وارد کنید");
await _restWrapper.FaqApiRest.Create(PageDto, token);
_snackbar.Add("تغییر سوالات متداول با موفقیت انجام شد", Severity.Success);
_dialogInstance.Close(DialogResult.Ok(true));
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync<ApiResult>();
_snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
}
}
public async Task SubmitUpdateAsync()
{
try
{
var token = await _userUtility.GetBearerTokenAsync();
if (token == null)
throw new Exception("token is null");
if (PageDto.Slug.IsNullOrEmpty())
throw new Exception("اسلاگ صفحه سوال متداول را وارد کنید");
if (PageDto.Title.IsNullOrEmpty())
throw new Exception("عنوان صفحه سوالات متداول را وارد کنید");
if (PageDto.Id == default)
throw new Exception("ای دی معتبر نمی باشد");
await _restWrapper.FaqApiRest.Update(PageDto, token);
_snackbar.Add("تغییر سوالات متداول با موفقیت انجام شد", Severity.Success);
_dialogInstance.Close(DialogResult.Ok(true));
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync<ApiResult>();
_snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
}
}
}