174 lines
5.4 KiB
C#
174 lines
5.4 KiB
C#
using Netina.Domain.Entities.Brands;
|
|
|
|
namespace Netina.AdminPanel.PWA.Pages;
|
|
|
|
public class BrandsPageViewModel(
|
|
NavigationManager navigationManager,
|
|
ISnackbar snackbar,
|
|
IUserUtility userUtility,
|
|
IRestWrapper restWrapper,
|
|
IDialogService dialogService) : BaseViewModel<List<BrandSDto>>(userUtility)
|
|
{
|
|
private readonly NavigationManager _navigationManager = navigationManager;
|
|
private readonly IUserUtility _userUtility = userUtility;
|
|
public string Search = string.Empty;
|
|
public int CurrentPage = 0;
|
|
public int PageCount = 1;
|
|
|
|
public override async Task InitializeAsync()
|
|
{
|
|
try
|
|
{
|
|
IsProcessing = true;
|
|
var dto = await restWrapper.CrudDtoApiRest<Brand, BrandSDto, Guid>(Address.BrandController)
|
|
.ReadAll(0);
|
|
PageDto = dto;
|
|
if (PageDto.Count == 10)
|
|
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 AddBrandClicked()
|
|
{
|
|
DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true };
|
|
await dialogService.ShowAsync<BrandActionDialogBox>("افزودن برند جدید", maxWidth);
|
|
}
|
|
|
|
public async Task EditBrandAsync(BrandSDto brand)
|
|
{
|
|
DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true };
|
|
var parameters = new DialogParameters<BrandActionDialogBox> { { x => x.Brand, brand } };
|
|
await dialogService.ShowAsync<BrandActionDialogBox>($"ویرایش برند {brand.PersianName}", parameters, maxWidth);
|
|
}
|
|
public async Task DeleteBrandAsync(Guid selectedCategoryId)
|
|
{
|
|
var reference = await dialogService.ShowQuestionDialog($"آیا از حذف برند اطمینان دارید ?");
|
|
var result = await reference.Result;
|
|
if (!result.Canceled)
|
|
{
|
|
|
|
try
|
|
{
|
|
|
|
IsProcessing = true;
|
|
var token = await _userUtility.GetBearerTokenAsync();
|
|
await restWrapper.CrudDtoApiRest<Brand, BrandSDto, Guid>(Address.BrandController)
|
|
.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;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public async Task SearchChanged(string search)
|
|
{
|
|
if (search.IsNullOrEmpty() && !Search.IsNullOrEmpty())
|
|
await InitializeAsync();
|
|
Search = search;
|
|
}
|
|
|
|
public async Task SearchAsync()
|
|
{
|
|
try
|
|
{
|
|
if (Search.IsNullOrEmpty())
|
|
throw new AppException("نام برند برای جست جو وارد نشده است");
|
|
IsProcessing = true;
|
|
CurrentPage = 0;
|
|
PageCount = 1;
|
|
PageDto.Clear();
|
|
var dto = await restWrapper.BrandRestApi.ReadAll(CurrentPage, Search);
|
|
dto.ForEach(d => PageDto.Add(d));
|
|
if (PageDto.Count == 10)
|
|
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;
|
|
}
|
|
}
|
|
|
|
public async Task ChangePageAsync(int page)
|
|
{
|
|
CurrentPage = page - 1;
|
|
if (CurrentPage > PageCount - 2)
|
|
{
|
|
|
|
try
|
|
{
|
|
IsProcessing = true;
|
|
|
|
List<BrandSDto> dto = new List<BrandSDto>();
|
|
if (Search.IsNullOrEmpty())
|
|
{
|
|
dto = await restWrapper.CrudDtoApiRest<Brand, BrandSDto, Guid>(Address.BrandController)
|
|
.ReadAll(CurrentPage);
|
|
}
|
|
else
|
|
{
|
|
dto = await restWrapper.BrandRestApi.ReadAll(CurrentPage, Search);
|
|
}
|
|
|
|
dto.ForEach(d => PageDto.Add(d));
|
|
if (PageDto.Count % 10 == 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;
|
|
}
|
|
}
|
|
}
|
|
} |