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

209 lines
6.4 KiB
C#

using Netina.Domain.Entities.Brands;
namespace Netina.AdminPanel.PWA.Dialogs;
public class BrandActionDialogBoxViewModel : BaseViewModel<BrandLDto>
{
private readonly ISnackbar _snackbar;
private readonly IRestWrapper _restWrapper;
private readonly IUserUtility _userUtility;
private readonly IDialogService _dialogService;
private readonly MudDialogInstance _mudDialog;
public BrandActionDialogBoxViewModel(ISnackbar snackbar,
IRestWrapper restWrapper,
IUserUtility userUtility,
IDialogService dialogService,
MudDialogInstance mudDialog) : base(userUtility)
{
_snackbar = snackbar;
_restWrapper = restWrapper;
_userUtility = userUtility;
_dialogService = dialogService;
_mudDialog = mudDialog;
}
public BrandActionDialogBoxViewModel(ISnackbar snackbar,
IRestWrapper restWrapper,
IUserUtility userUtility,
IDialogService dialogService,
MudDialogInstance mudDialog,
BrandSDto brand) : base(userUtility)
{
_snackbar = snackbar;
_restWrapper = restWrapper;
_userUtility = userUtility;
_dialogService = dialogService;
_mudDialog = mudDialog;
Brand = brand;
}
private BrandSDto? _brand = null;
public BrandSDto? Brand
{
get => _brand;
set
{
_brand = value;
if (_brand != null)
{
IsEditing = true;
}
}
}
public void Cancel() => _mudDialog.Cancel();
public bool IsEditing = false;
public override async Task InitializeAsync()
{
if (Brand != null)
{
IsEditing = true;
try
{
IsProcessing = true;
var response = await _restWrapper.CrudDtoApiRest<Brand, BrandLDto, Guid>(Address.BrandController).ReadOne(Brand.Id);
var brandLDto = response;
PageDto = brandLDto;
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync<ApiResult>();
_snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
_mudDialog.Cancel();
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
_mudDialog.Cancel();
}
finally
{
IsProcessing = false;
}
}
await base.InitializeAsync();
}
public async Task SubmitCreateAsync()
{
try
{
if (PageDto.EnglishName.IsNullOrEmpty())
throw new AppException("لطفا نام برند را وارد کنید");
IsProcessing = true;
var token = await _userUtility.GetBearerTokenAsync();
if (token == null)
throw new AppException("Token is null");
var request = new CreateBrandCommand(PageDto.PersianName,
PageDto.EnglishName,
PageDto.Description,
PageDto.HasSpecialPage,
PageDto.PageUrl,
PageDto.Files,
Faqs,
new Dictionary<string, string>());
await _restWrapper.CrudApiRest<Brand, Guid>(Address.BrandController).Create<CreateBrandCommand>(request, token);
_mudDialog.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);
}
finally
{
IsProcessing = false;
}
}
public async Task SubmitEditAsync()
{
try
{
if (Brand == null)
throw new AppException("برند به درستی ارسال نشده است");
if (PageDto.EnglishName.IsNullOrEmpty())
throw new AppException("لطفا نام برند را وارد کنید");
IsProcessing = true;
var token = await _userUtility.GetBearerTokenAsync();
if (token == null)
throw new AppException("Token is null");
var request = new UpdateBrandCommand(
PageDto.Id,
PageDto.PersianName,
PageDto.EnglishName,
PageDto.Description,
PageDto.HasSpecialPage,
PageDto.PageUrl,
PageDto.Files,
Faqs,
new Dictionary<string, string>());
await _restWrapper.CrudApiRest<Brand, Guid>(Address.BrandController).Update<UpdateBrandCommand>(request, token);
_mudDialog.Close();
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync<ApiResult>();
_snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
_mudDialog.Cancel();
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
_mudDialog.Cancel();
}
finally
{
IsProcessing = false;
}
}
public string FaqQuestion { get; set; } = string.Empty;
public string FaqAnswer { get; set; } = string.Empty;
public Dictionary<string, string> Faqs = new();
public void AddFaq()
{
try
{
if (FaqAnswer.IsNullOrEmpty())
throw new Exception("لطفا پاسخ را وارد کنید");
if (FaqQuestion.IsNullOrEmpty())
throw new Exception("لطفا سوال را وارد کنید");
Faqs.Add(FaqQuestion, FaqAnswer);
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
}
}
public async Task SelectFileAsync()
{
DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true };
var dialog = await _dialogService.ShowAsync<StorageDialogBox>("انتخاب عکس برند", maxWidth);
var result = await dialog.Result;
var file = result.Data;
if (file is StorageFileSDto storageFile)
PageDto.Files.Add(storageFile);
}
public void RemoveFile(StorageFileSDto file)
{
PageDto.Files.Remove(file);
}
}