AdminPanel/Netina.AdminPanel.PWA/Dialogs/ShippingActionDialogBox.raz...

125 lines
4.0 KiB
C#

using Netina.Domain.Entities.Warehouses;
namespace Netina.AdminPanel.PWA.Dialogs;
public class ShippingActionDialogBoxViewModel : BaseViewModel<ShippingSDto>
{
private readonly ISnackbar _snackbar;
private readonly IRestWrapper _restWrapper;
private readonly IUserUtility _userUtility;
private readonly IDialogService _dialogService;
private readonly MudDialogInstance _mudDialog;
public ShippingActionDialogBoxViewModel(ISnackbar snackbar,
IRestWrapper restWrapper,
IUserUtility userUtility,
IDialogService dialogService,
MudDialogInstance mudDialog)
{
_snackbar = snackbar;
_restWrapper = restWrapper;
_userUtility = userUtility;
_dialogService = dialogService;
_mudDialog = mudDialog;
}
public ShippingActionDialogBoxViewModel(ISnackbar snackbar,
IRestWrapper restWrapper,
IUserUtility userUtility,
IDialogService dialogService,
MudDialogInstance mudDialog,
ShippingSDto shipping)
{
_snackbar = snackbar;
_restWrapper = restWrapper;
_userUtility = userUtility;
_dialogService = dialogService;
_mudDialog = mudDialog;
PageDto = shipping;
IsEditing = true;
}
public bool IsEditing = false;
public void Cancel() => _mudDialog.Cancel();
public async Task SubmitEditAsync()
{
try
{
IsProcessing = true;
if (PageDto.Id == default)
throw new Exception("Id is null !");
var token = await _userUtility.GetBearerTokenAsync();
if (token == null)
throw new Exception("Token is null");
if (PageDto.Name.IsNullOrEmpty())
throw new Exception("نام را وارد کنید");
if (PageDto.WarehouseName.IsNullOrEmpty())
throw new Exception("نام انبار را وارد کنید");
var request = PageDto.Adapt<UpdateShippingCommand>();
await _restWrapper.CrudApiRest<Shipping, Guid>(Address.ShippingController).Update<UpdateShippingCommand>(request, token);
_snackbar.Add($"ویرایش {PageDto.Name} با موفقیت انجام شد", Severity.Success);
_mudDialog.Close(DialogResult.Ok(true));
}
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 async Task SubmitCreateAsync()
{
try
{
IsProcessing = true;
var token = await _userUtility.GetBearerTokenAsync();
if (token == null)
throw new Exception("Token is null");
if (PageDto.Name.IsNullOrEmpty())
throw new Exception("نام را وارد کنید");
if (PageDto.WarehouseName.IsNullOrEmpty())
throw new Exception("نام انبار را وارد کنید");
var request = PageDto.Adapt<CreateShippingCommand>();
await _restWrapper.CrudApiRest<Shipping, Guid>(Address.ShippingController).Create<CreateShippingCommand>(request, token);
_snackbar.Add($"ساخت {PageDto.Name} با موفقیت انجام شد", Severity.Success);
_mudDialog.Close(DialogResult.Ok(true));
}
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;
}
}
}