AdminPanel/Netina.AdminPanel.PWA/Dialogs/FastProductCreateDialogBox....

159 lines
5.8 KiB
C#

using Microsoft.AspNetCore.Components.Forms;
using Netina.Domain.Entities.Brands;
namespace Netina.AdminPanel.PWA.Dialogs;
public class FastProductCreateDialogBoxViewModel(ISnackbar snackbar, IRestWrapper restWrapper, IUserUtility userUtility, IDialogService dialogService, MudDialogInstance dialogInstance)
: BaseViewModel<ProductLDto>(userUtility)
{
public HashSet<ProductCategorySDto> Categories { get; set; } = new();
public bool FormEnable = false;
private ProductCategorySDto? _selectedCategory;
public ProductCategorySDto? SelectedCategory
{
get => _selectedCategory;
set
{
_selectedCategory = value;
FormEnable = _selectedCategory != null;
}
}
public BrandSDto? SelectedBrand { get; set; }
public override async Task InitializeAsync()
{
var categories = await restWrapper.ProductCategoryRestApi.ReadAll(true);
categories.ForEach(c => Categories.Add(c));
PageDto.Stock = 1;
await base.InitializeAsync();
}
public async Task<HashSet<ProductCategorySDto>> LoadCategories(ProductCategorySDto arg)
{
return arg.Children.ToHashSet();
}
private readonly List<IBrowserFile> Files = new();
public readonly List<string> FileNames = new();
private const string DefaultDragClass = "relative rounded-lg border-2 border-dashed pa-4 mt-4 mud-width-full mud-height-full z-10";
public string DragClass = DefaultDragClass;
public void SetDragClass()
=> DragClass = $"{DefaultDragClass} mud-border-primary";
public void ClearDragClass()
=> DragClass = DefaultDragClass;
public void OnInputFileChanged(InputFileChangeEventArgs e)
{
ClearDragClass();
var files = e.GetMultipleFiles();
foreach (var file in files)
{
FileNames.Add(file.Name);
Files.Add(file);
}
}
private List<BrandSDto> brands = new();
public async Task<IEnumerable<BrandSDto>> SearchBrand(string brand)
{
try
{
if (brands.Count == 0)
brands = await restWrapper.BrandRestApi.ReadAll();
if (brand.IsNullOrEmpty().Not())
return brands.Where(b => b.PersianName.Trim().ToUpper().Contains(brand.Trim().ToUpper()));
return brands;
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync<ApiResult>();
snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
return brands;
}
catch (Exception e)
{
snackbar.Add(e.Message, Severity.Error);
return brands;
}
}
public void AddBrand(string brandName)
{
if (brandName.IsNullOrEmpty())
return;
var brand = new BrandSDto { PersianName = brandName };
brands.Add(brand);
SelectedBrand = brand;
Task.Run(async () =>
{
var token = await userUtility.GetBearerTokenAsync();
if (token == null)
return;
var command = brand.Adapt<CreateBrandCommand>() with {Files = new()};
var response = await restWrapper.CrudApiRest<Brand, Guid>(Address.BrandController)
.Create(command, token);
brand.Id = response;
snackbar.Add($"افزودن برند {brand.PersianName} با موفقیت انجام شد");
});
}
public void SubmitCreateProduct()
{
try
{
if (SelectedCategory == null)
throw new Exception("دسته بندی انتخاب نشده است");
if (SelectedBrand == null || SelectedBrand.Id == default)
throw new Exception("برند انتخاب نشده است");
if (PageDto.PersianName.IsNullOrEmpty())
throw new Exception("نام فارسی را وارد کنید");
var product = PageDto.Clone();
var brand = SelectedBrand.Clone();
var browserFiles = Files.ToList();
Task.Run(async () =>
{
var token = await userUtility.GetBearerTokenAsync();
if (token == null)
return;
var files = new List<StorageFileSDto>();
foreach (var file in browserFiles)
{
using var memoryStream = new MemoryStream();
var stream = file.OpenReadStream();
await stream.CopyToAsync(memoryStream);
var fileUpload = new FileUploadRequest
{
ContentType = file.ContentType,
FileName = file.Name,
FileUploadType = FileUploadType.Image,
StringBaseFile = Convert.ToBase64String(memoryStream.ToArray())
};
var rest = await restWrapper.FileRestApi.UploadFileAsync(fileUpload, token);
files.Add(new StorageFileSDto
{
FileLocation = rest.FileLocation,
FileName = rest.FileName,
FileType = StorageFileType.Image
});
}
var command = product.Adapt<CreateProductCommand>() with
{
BeDisplayed = true,
BrandId = brand.Id,
CategoryId = SelectedCategory.Id,
Files = files
};
await restWrapper.CrudApiRest<Product, Guid>(Address.ProductController)
.Create(command, token);
});
PageDto = new ProductLDto{Stock = 1};
FileNames.Clear();
Files.Clear();
}
catch (Exception e)
{
snackbar.Add(e.Message, Severity.Error);
}
}
}