163 lines
5.6 KiB
C#
163 lines
5.6 KiB
C#
using Netina.AdminPanel.PWA.Models;
|
|
using Netina.AdminPanel.PWA.Models.Api;
|
|
using Netina.AdminPanel.PWA.Services.RestServices;
|
|
using Netina.AdminPanel.PWA.Utilities;
|
|
using Netina.AdminPanel.PWA.Utilities.Models;
|
|
using Netina.Domain.Entities.Warehouses;
|
|
|
|
namespace Netina.AdminPanel.PWA.Pages;
|
|
|
|
public class ShippingPageViewModel : BaseViewModel<ObservableCollection<ShippingSDto>>
|
|
{
|
|
private readonly NavigationManager _navigationManager;
|
|
private readonly ISnackbar _snackbar;
|
|
private readonly IUserUtility _userUtility;
|
|
private readonly IDialogService _dialogService;
|
|
private readonly IRestWrapper _restWrapper;
|
|
|
|
public string Search = string.Empty;
|
|
public int CurrentPage = 0;
|
|
public int PageCount = 1;
|
|
|
|
public ShippingPageViewModel(NavigationManager navigationManager, ISnackbar snackbar, IUserUtility userUtility, IRestWrapper restWrapper, IDialogService dialogService)
|
|
{
|
|
_navigationManager = navigationManager;
|
|
_snackbar = snackbar;
|
|
_userUtility = userUtility;
|
|
_restWrapper = restWrapper;
|
|
_dialogService = dialogService;
|
|
}
|
|
|
|
public override async Task InitializeAsync()
|
|
{
|
|
try
|
|
{
|
|
var token = await _userUtility.GetBearerTokenAsync();
|
|
if (token == null)
|
|
throw new Exception("Token is null");
|
|
IsProcessing = true;
|
|
PageDto.Clear();
|
|
var dto = await _restWrapper.CrudDtoApiRest<Shipping, ShippingSDto, Guid>(Address.ShippingController)
|
|
.ReadAll(CurrentPage, token);
|
|
dto.ForEach(d => PageDto.Add(d));
|
|
if (PageDto.Count == 20)
|
|
PageCount = 2;
|
|
}
|
|
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;
|
|
}
|
|
await base.InitializeAsync();
|
|
}
|
|
|
|
public async Task ChangePageAsync(int page)
|
|
{
|
|
CurrentPage = page - 1;
|
|
if (CurrentPage > PageCount - 2)
|
|
{
|
|
|
|
try
|
|
{
|
|
IsProcessing = true;
|
|
var token = await _userUtility.GetBearerTokenAsync();
|
|
if (token == null)
|
|
throw new Exception("Token is null");
|
|
|
|
List<ShippingSDto> dto = new List<ShippingSDto>();
|
|
dto = await _restWrapper.CrudDtoApiRest<Shipping, ShippingSDto, Guid>(Address.ShippingController)
|
|
.ReadAll(CurrentPage,token);
|
|
|
|
|
|
dto.ForEach(d => PageDto.Add(d));
|
|
if (PageDto.Count % 20 == 0)
|
|
PageCount = CurrentPage + 2;
|
|
|
|
}
|
|
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 AddClicked()
|
|
{
|
|
DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true };
|
|
var dialogResult = await _dialogService.ShowAsync<ShippingActionDialogBox>("افزودن روش ارسال جدید", maxWidth);
|
|
var result = await dialogResult.Result;
|
|
if (!result.Canceled && result.Data is bool and true)
|
|
{
|
|
await InitializeAsync();
|
|
}
|
|
}
|
|
|
|
public async Task EditClicked(ShippingSDto shipping)
|
|
{
|
|
DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true };
|
|
var parameters = new DialogParameters<ShippingActionDialogBox>();
|
|
parameters.Add(x => x.Shipping, shipping);
|
|
var dialogResult = await _dialogService.ShowAsync<ShippingActionDialogBox>($"ویرایش روش ارسال {shipping.Name}", parameters, maxWidth);
|
|
var result = await dialogResult.Result;
|
|
if (!result.Canceled && result.Data is bool and true)
|
|
{
|
|
await InitializeAsync();
|
|
}
|
|
}
|
|
|
|
public async Task DeleteAsync(Guid selectedCategoryId)
|
|
{
|
|
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.CrudDtoApiRest<Shipping, ShippingSDto, Guid>(Address.ShippingController)
|
|
.Delete(selectedCategoryId, token);
|
|
_snackbar.Add("حذف روش ارسال با موفقیت انجام شد", Severity.Success);
|
|
await InitializeAsync();
|
|
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
} |