AdminPanel/Netina.AdminPanel.PWA/Pages/ShopManagementPage.razor.cs

181 lines
6.0 KiB
C#

using Netina.Domain.MartenEntities.Settings;
namespace Netina.AdminPanel.PWA.Pages;
public class ShopManagementPageViewModel : BaseViewModel
{
private readonly NavigationManager _navigationManager;
private readonly ISnackbar _snackbar;
private readonly IUserUtility _userUtility;
private readonly IDialogService _dialogService;
private readonly IRestWrapper _restWrapper;
public ShopSetting ShopSetting { get; set; } = new ShopSetting();
public PaymentSetting PaymentSetting { get; set; } = new PaymentSetting();
public List<City> Cities = new List<City>();
public City SelectedCity = new City();
public List<Province> Provinces = new List<Province>();
public Province SelectedProvince = new Province();
public ShopManagementPageViewModel(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
{
IsProcessing = true;
var token = await _userUtility.GetBearerTokenAsync();
if (token == null)
throw new Exception("Token is null");
ShopSetting = await _restWrapper.SettingRestApi.GetSettingAsync<ShopSetting>("ShopSetting", token);
if (ShopSetting.CityId != 0)
SelectedCity = new City { Id = ShopSetting.CityId, Name = ShopSetting.City };
if (ShopSetting.ProvinceId != 0)
SelectedProvince = new Province { Id = ShopSetting.ProvinceId, Name = ShopSetting.Province };
PaymentSetting = await _restWrapper.SettingRestApi.GetSettingAsync<PaymentSetting>("PaymentSetting", token);
}
catch (ApiException e)
{
var exe = await e.GetContentAsAsync<ApiResult>();
_snackbar.Add(exe != null ? exe.Message : e.Content, Severity.Error);
}
catch (Exception ex)
{
_snackbar.Add(ex.Message, Severity.Error);
}
finally
{
IsProcessing = false;
}
await base.InitializeAsync();
}
public async Task<IEnumerable<City>> SearchCityAsync(string city)
{
try
{
if (SelectedProvince.Id == default)
throw new Exception("لطفا استان را انتخاب کنید");
Cities = await _restWrapper.DistrictApiRest.GetCitiesAsync(SelectedProvince.Id);
if (city.IsNullOrEmpty())
return Cities;
return Cities.Where(c => c.Name.Contains(city));
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync<ApiResult>();
if (exe != null)
_snackbar.Add(exe.Message, Severity.Error);
_snackbar.Add(ex.Content, Severity.Error);
return Cities;
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
return Cities;
}
}
public async Task<IEnumerable<Province>> SearchProvinceAsync(string province)
{
try
{
Provinces = await _restWrapper.DistrictApiRest.GetProvincesAsync();
if (province.IsNullOrEmpty())
return Provinces;
return Provinces.Where(c => c.Name.Contains(province));
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync<ApiResult>();
if (exe != null)
_snackbar.Add(exe.Message, Severity.Error);
_snackbar.Add(ex.Content, Severity.Error);
return Provinces;
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
return Provinces;
}
}
public async Task SubmitShopSettingAsync()
{
try
{
if (SelectedProvince.Id == default)
throw new Exception("لطفا استان را انتخاب کنید");
if (SelectedCity.Id == default)
throw new Exception("لطفا شهر را انتخاب نمایید");
ShopSetting.CityId = SelectedCity.Id;
ShopSetting.ProvinceId = SelectedProvince.Id;
ShopSetting.City = SelectedCity.Name;
ShopSetting.Province = SelectedProvince.Name;
var token = await _userUtility.GetBearerTokenAsync();
if (token == null) throw new Exception("Token is null");
await _restWrapper.SettingRestApi.PostSettingAsync("ShopSetting", ShopSetting, token);
_snackbar.Add("تنظیمات فروشگاه با موفقیت به روز شد", Severity.Success);
}
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 SubmitPaymentSettingAsync()
{
try
{
var token = await _userUtility.GetBearerTokenAsync();
if (token == null) throw new Exception("Token is null");
await _restWrapper.SettingRestApi.PostSettingAsync("PaymentSetting", PaymentSetting, token);
_snackbar.Add("تنظیمات فروشگاه با موفقیت به روز شد", Severity.Success);
}
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;
}
}
}