AdminPanel/Netina.AdminPanel.PWA/Dialogs/DiscountActionDialogBox.raz...

381 lines
12 KiB
C#
Raw Normal View History

2024-04-17 16:19:43 +03:30
using Netina.Domain.Entities.Discounts;
2024-04-17 16:19:43 +03:30
namespace Netina.AdminPanel.PWA.Dialogs;
public class DiscountActionDialogBoxViewModel : BaseViewModel<DiscountLDto>
{
private readonly ISnackbar _snackbar;
private readonly IRestWrapper _restWrapper;
private readonly IUserUtility _userUtility;
private readonly IDialogService _dialogService;
private readonly MudDialogInstance _mudDialog;
public DiscountActionDialogBoxViewModel(
ISnackbar snackbar,
IRestWrapper restWrapper,
IUserUtility userUtility,
IDialogService dialogService,
2024-05-29 12:55:05 +03:30
MudDialogInstance mudDialog) : base(userUtility)
{
_snackbar = snackbar;
_restWrapper = restWrapper;
_userUtility = userUtility;
_dialogService = dialogService;
_mudDialog = mudDialog;
}
public DiscountActionDialogBoxViewModel(ISnackbar snackbar,
IRestWrapper restWrapper,
IUserUtility userUtility,
IDialogService dialogService,
MudDialogInstance mudDialog,
2024-05-29 12:55:05 +03:30
DiscountSDto discount) : base(userUtility)
{
_snackbar = snackbar;
_restWrapper = restWrapper;
_userUtility = userUtility;
_dialogService = dialogService;
_mudDialog = mudDialog;
_discountId = discount.Id;
IsEditing = true;
}
private bool _isProductEnable;
public bool IsProductEnable
{
get => _isProductEnable;
set
{
_isProductEnable = value;
if (_isProductEnable)
{
IsCategoryEnable = false;
IsAllEnable = false;
}
}
}
private bool _isCategoryEnable;
public bool IsCategoryEnable
{
get => _isCategoryEnable;
set
{
_isCategoryEnable = value;
if (_isCategoryEnable)
{
IsProductEnable = false;
IsAllEnable = false;
}
}
}
private bool _isAllEnable;
public bool IsAllEnable
{
get => _isAllEnable;
set
{
_isAllEnable = value;
if (_isAllEnable)
{
IsCategoryEnable = false;
IsProductEnable = false;
}
}
}
public bool IsPercentType { get; set; } = true;
public bool IsAmountType { get; set; }
public DateTime? ExpireDate { get; set; }
public DateTime? StartDate { get; set; }
private Guid _discountId;
public bool IsEditing = false;
public void AmountTypeChanged(DiscountAmountType type)
{
switch (type)
{
case DiscountAmountType.Amount:
IsAmountType = true;
IsPercentType = false;
break;
case DiscountAmountType.Percent:
IsAmountType = false;
IsPercentType = true;
break;
}
}
public override async Task InitializeAsync()
{
if (IsEditing && _discountId != default)
{
try
{
var token = await _userUtility.GetBearerTokenAsync();
if (token == null)
throw new Exception("Token is null");
IsProcessing = true;
var discount = await _restWrapper.DiscountRest.ReadOne(_discountId, token);
PageDto = discount;
if (PageDto.CategoryId != default)
SelectedCategory = new ProductCategorySDto { Id = PageDto.CategoryId, Name = PageDto.CategoryName };
if (PageDto.ProductId != default)
SelectedProduct = new ProductSDto { Id = PageDto.ProductId, PersianName = PageDto.ProductName };
ExpireDate = PageDto.ExpireDate;
StartDate = PageDto.StartDate;
}
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;
}
}
else
{
StartDate = DateTime.Today;
ExpireDate = DateTime.Today.AddDays(7);
PageDto.IsInfinity = false;
PageDto.HasCode = false;
IsAllEnable = true;
}
await base.InitializeAsync();
}
public void Cancel() => _mudDialog.Cancel();
public async Task SubmitCreateAsync()
{
try
{
IsProcessing = true;
if (IsAllEnable)
{
PageDto.Type = DiscountType.All;
}
else if (IsCategoryEnable)
{
PageDto.Type = DiscountType.Category;
if (SelectedCategory == null)
throw new Exception("دسته بندی را برای تخفیف انتخاب نمایید");
PageDto.CategoryId = SelectedCategory.Id;
}
else if (IsProductEnable)
{
PageDto.Type = DiscountType.Product;
if (SelectedProduct == null)
throw new Exception("کالا مورد نظر را برای تخفیف انتخاب نمایید");
PageDto.ProductId = SelectedProduct.Id;
}
var token = await _userUtility.GetBearerTokenAsync();
if (token == null)
throw new Exception("Token is null");
if (ExpireDate != null)
PageDto.ExpireDate = ExpireDate.Value;
if(StartDate != null)
PageDto.StartDate = StartDate.Value;
Refactor and enhance product and order handling - Updated `DiscountActionDialogBoxViewModel` and `FastProductCreateDialogBoxViewModel` to create command objects directly from properties and improved error handling. - Added meta tag management UI and logic in `ProductActionDialogBox.razor` and `ProductActionDialogBoxViewModel`. - Increased max file read stream size to 8 MB in `StorageDialogBoxViewModel`. - Incremented `AssemblyVersion` and `FileVersion` to `1.7.20.34` in `Netina.AdminPanel.PWA.csproj`. - Updated `BrandsPage.razor` and `BrandsPageViewModel` for pagination and service injection. - Updated `CategoriesPageViewModel` to create command objects directly from properties. - Updated `ProductsPage.razor` for service injection and added a button for product details. - Updated `ICrudApiRest` and `ICrudDtoApiRest` interfaces to use generic `Create` methods. - Updated `appsettings.Development.json` for `StorageBaseUrl` and commented out `IsShop`. - Added new project `AppHost.csproj` targeting .NET 8.0 with Aspire hosting. - Added new `appsettings.Development.json` and `appsettings.json` for logging. - Added new `Program.cs` to create and run a distributed application. - Added new `launchSettings.json` for application launch settings. - Added `Extensions.cs` for common .NET Aspire services. - Added new project `ServiceDefaults.csproj` for shared service defaults. - Introduced `ProductMetaTag` class and related migration for meta tag handling. - Updated `OrderController.cs` for additional authorization requirements. - Updated target frameworks to `net8.0` in various projects. - Enhanced `SiteMapService.cs` to include brand site maps. - Added new properties to DTOs for customer and meta tag handling. - Enhanced `Product` class with meta tag management methods. - Refactored `OrderMapper.g.cs` and `ProductMapper.g.cs` for improved mapping logic. - Enhanced command handlers to manage meta tags. - Added `ICurrentUserService` for user permissions in query handlers. - Refactored `StorageService.cs` for paginated storage file fetching.
2024-12-06 17:37:40 +03:30
var request = new CreateDiscountCommand(PageDto.Code,
PageDto.Description,
PageDto.DiscountPercent,
PageDto.DiscountAmount,
PageDto.HasCode,
PageDto.AmountType,
PageDto.Type,
PageDto.Count,
PageDto.StartDate,
PageDto.ExpireDate,
PageDto.Immortal,
PageDto.PriceFloor,
PageDto.HasPriceFloor,
PageDto.PriceCeiling,
PageDto.HasPriceCeiling,
PageDto.IsInfinity,
PageDto.UseCount,
PageDto.IsForInvitation,
PageDto.IsSpecialOffer,
PageDto.IsForFirstPurchase,
PageDto.ProductId,
PageDto.CategoryId);
await _restWrapper.CrudDtoApiRest<Discount,DiscountLDto, Guid>(Address.DiscountController).Create<CreateDiscountCommand>(request, token);
_snackbar.Add($"ساخت تخفیف با موفقیت انجام شد", Severity.Success);
2024-04-17 16:19:43 +03:30
_mudDialog.Close(true);
}
catch (ApiException ex)
{
Refactor and enhance product and order handling - Updated `DiscountActionDialogBoxViewModel` and `FastProductCreateDialogBoxViewModel` to create command objects directly from properties and improved error handling. - Added meta tag management UI and logic in `ProductActionDialogBox.razor` and `ProductActionDialogBoxViewModel`. - Increased max file read stream size to 8 MB in `StorageDialogBoxViewModel`. - Incremented `AssemblyVersion` and `FileVersion` to `1.7.20.34` in `Netina.AdminPanel.PWA.csproj`. - Updated `BrandsPage.razor` and `BrandsPageViewModel` for pagination and service injection. - Updated `CategoriesPageViewModel` to create command objects directly from properties. - Updated `ProductsPage.razor` for service injection and added a button for product details. - Updated `ICrudApiRest` and `ICrudDtoApiRest` interfaces to use generic `Create` methods. - Updated `appsettings.Development.json` for `StorageBaseUrl` and commented out `IsShop`. - Added new project `AppHost.csproj` targeting .NET 8.0 with Aspire hosting. - Added new `appsettings.Development.json` and `appsettings.json` for logging. - Added new `Program.cs` to create and run a distributed application. - Added new `launchSettings.json` for application launch settings. - Added `Extensions.cs` for common .NET Aspire services. - Added new project `ServiceDefaults.csproj` for shared service defaults. - Introduced `ProductMetaTag` class and related migration for meta tag handling. - Updated `OrderController.cs` for additional authorization requirements. - Updated target frameworks to `net8.0` in various projects. - Enhanced `SiteMapService.cs` to include brand site maps. - Added new properties to DTOs for customer and meta tag handling. - Enhanced `Product` class with meta tag management methods. - Refactored `OrderMapper.g.cs` and `ProductMapper.g.cs` for improved mapping logic. - Enhanced command handlers to manage meta tags. - Added `ICurrentUserService` for user permissions in query handlers. - Refactored `StorageService.cs` for paginated storage file fetching.
2024-12-06 17:37:40 +03:30
if (ex.StatusCode == HttpStatusCode.BadRequest)
{
var exe = await ex.GetContentAsAsync<ApiResult>();
_snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
}
else
_snackbar.Add(ex.Content, Severity.Error);
_mudDialog.Cancel();
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
_mudDialog.Cancel();
}
finally
{
IsProcessing = false;
}
}
public async Task SubmitEditAsync()
{
try
{
IsProcessing = true;
if (_discountId == default)
throw new Exception("Id is null");
if (IsAllEnable)
{
PageDto.Type = DiscountType.All;
}
else if (IsCategoryEnable)
{
PageDto.Type = DiscountType.Category;
if (SelectedCategory == null)
throw new Exception("دسته بندی را برای تخفیف انتخاب نمایید");
PageDto.CategoryId = SelectedCategory.Id;
}
else if (IsProductEnable)
{
PageDto.Type = DiscountType.Product;
if (SelectedProduct == null)
throw new Exception("کالا مورد نظر را برای تخفیف انتخاب نمایید");
PageDto.ProductId = SelectedProduct.Id;
}
var token = await _userUtility.GetBearerTokenAsync();
if (token == null)
throw new Exception("Token is null");
PageDto.Id = _discountId;
Refactor and enhance product and order handling - Updated `DiscountActionDialogBoxViewModel` and `FastProductCreateDialogBoxViewModel` to create command objects directly from properties and improved error handling. - Added meta tag management UI and logic in `ProductActionDialogBox.razor` and `ProductActionDialogBoxViewModel`. - Increased max file read stream size to 8 MB in `StorageDialogBoxViewModel`. - Incremented `AssemblyVersion` and `FileVersion` to `1.7.20.34` in `Netina.AdminPanel.PWA.csproj`. - Updated `BrandsPage.razor` and `BrandsPageViewModel` for pagination and service injection. - Updated `CategoriesPageViewModel` to create command objects directly from properties. - Updated `ProductsPage.razor` for service injection and added a button for product details. - Updated `ICrudApiRest` and `ICrudDtoApiRest` interfaces to use generic `Create` methods. - Updated `appsettings.Development.json` for `StorageBaseUrl` and commented out `IsShop`. - Added new project `AppHost.csproj` targeting .NET 8.0 with Aspire hosting. - Added new `appsettings.Development.json` and `appsettings.json` for logging. - Added new `Program.cs` to create and run a distributed application. - Added new `launchSettings.json` for application launch settings. - Added `Extensions.cs` for common .NET Aspire services. - Added new project `ServiceDefaults.csproj` for shared service defaults. - Introduced `ProductMetaTag` class and related migration for meta tag handling. - Updated `OrderController.cs` for additional authorization requirements. - Updated target frameworks to `net8.0` in various projects. - Enhanced `SiteMapService.cs` to include brand site maps. - Added new properties to DTOs for customer and meta tag handling. - Enhanced `Product` class with meta tag management methods. - Refactored `OrderMapper.g.cs` and `ProductMapper.g.cs` for improved mapping logic. - Enhanced command handlers to manage meta tags. - Added `ICurrentUserService` for user permissions in query handlers. - Refactored `StorageService.cs` for paginated storage file fetching.
2024-12-06 17:37:40 +03:30
var request = new UpdateDiscountCommand(
PageDto.Id,
PageDto.Code,
PageDto.Description,
PageDto.DiscountPercent,
PageDto.DiscountAmount,
PageDto.HasCode,
PageDto.AmountType,
PageDto.Type,
PageDto.Count,
PageDto.StartDate,
PageDto.ExpireDate,
PageDto.Immortal,
PageDto.PriceFloor,
PageDto.HasPriceFloor,
PageDto.PriceCeiling,
PageDto.HasPriceCeiling,
PageDto.IsInfinity,
PageDto.UseCount,
PageDto.IsForInvitation,
PageDto.IsSpecialOffer,
PageDto.IsForFirstPurchase,
PageDto.ProductId,
PageDto.CategoryId);
await _restWrapper.CrudApiRest<Discount, Guid>(Address.DiscountController).Update<UpdateDiscountCommand>(request, token);
_snackbar.Add($"ویرایش تخفیف با موفقیت انجام شد", Severity.Success);
2024-04-17 16:19:43 +03:30
_mudDialog.Close(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;
}
}
private List<ProductCategorySDto> _productCategories = new List<ProductCategorySDto>();
public ProductCategorySDto? SelectedCategory;
public async Task<IEnumerable<ProductCategorySDto>> SearchCategory(string category)
{
try
{
if (category.IsNullOrEmpty())
_productCategories = await _restWrapper.ProductCategoryRestApi.ReadAll(0);
else
_productCategories = await _restWrapper.ProductCategoryRestApi.ReadAll(category);
return _productCategories;
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync<ApiResult>();
_snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
return _productCategories;
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
return _productCategories;
}
}
private List<ProductSDto> _products = new List<ProductSDto>();
public ProductSDto? SelectedProduct;
public async Task<IEnumerable<ProductSDto>> SearchProduct(string product)
{
try
{
GetProductsResponseDto response = new GetProductsResponseDto();
var token = await _userUtility.GetBearerTokenAsync();
if (token == null)
throw new Exception("Token is null");
if (product.IsNullOrEmpty())
2024-08-10 20:35:38 +03:30
response = await _restWrapper.ProductRestApi.ReadAll(0,null,null,null,token);
else
2024-08-10 20:35:38 +03:30
response = await _restWrapper.ProductRestApi.ReadAll(product,token);
_products = response.Products;
return _products;
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync<ApiResult>();
_snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
return _products;
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
return _products;
}
}
}