@using NetinaShop.Common.Models.Exception
@using NetinaShop.Domain.CommandQueries.Commands
@using NetinaShop.Domain.Entities.Brands
@inject ISnackbar Snackbar
@inject IRestWrapper RestWrapper
@inject IUserUtility UserUtility
@inject IDialogService DialogService
اطلاعات کلی
اطلاعات کلی دسته بندی محصول را به دقت وارد کنید
تصاویر برند
می توانید برای برند چند تصویر اپلود کنید
@foreach (var item in Files)
{
@if (item.IsHeader)
{
هدر
}
@if (item.IsPrimary)
{
اصلی
}
}
@if (_isEditing)
{
}
else
{
}
بستن
@code {
[CascadingParameter] MudDialogInstance MudDialog { get; set; }
[Parameter]
public BrandSDto? Brand { get; set; }
void Cancel() => MudDialog.Cancel();
public readonly ObservableCollection Files = new ObservableCollection();
private bool _isProcessing = false;
private string _persianName = string.Empty;
private string _englishName = string.Empty;
private string _description = string.Empty;
private bool _hasSpecialPage;
private bool _isEditing;
private string _pageUrl = string.Empty;
protected override async Task OnParametersSetAsync()
{
if (Brand != null)
{
_isEditing = true;
try
{
_isProcessing = true;
var response = await RestWrapper.CrudDtoApiRest(Address.BrandController).ReadOne(Brand.Id);
var brandLDto = response;
brandLDto.Files.ForEach(f => Files.Add(f));
_hasSpecialPage = brandLDto.HasSpecialPage;
_description = brandLDto.Description;
_englishName = brandLDto.EnglishName;
_persianName = brandLDto.PersianName;
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync();
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;
}
}
await base.OnParametersSetAsync();
}
private async Task SubmitCreateAsync()
{
try
{
if (_englishName.IsNullOrEmpty())
throw new AppException("لطفا نام برند را وارد کنید");
_isProcessing = true;
var token = await UserUtility.GetBearerTokenAsync();
if (token == null)
throw new AppException("Token is null");
var request = new CreateBrandCommand(_persianName, _englishName, _description, _hasSpecialPage, _pageUrl, Files.ToList());
await RestWrapper.CrudApiRest(Address.BrandController).Create(request, token);
MudDialog.Close(DialogResult.Ok(true));
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync();
Snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
}
catch (Exception e)
{
Snackbar.Add(e.Message, Severity.Error);
}
finally
{
_isProcessing = false;
}
}
private async Task SubmitEditAsync()
{
try
{
if (Brand == null)
throw new AppException("برند به درستی ارسال نشده است");
if (_englishName.IsNullOrEmpty())
throw new AppException("لطفا نام برند را وارد کنید");
_isProcessing = true;
var token = await UserUtility.GetBearerTokenAsync();
if (token == null)
throw new AppException("Token is null");
var request = new UpdateBrandCommand(Brand.Id, _persianName, _englishName, _description, _hasSpecialPage, _pageUrl, Files.ToList());
await RestWrapper.CrudApiRest(Address.BrandController).Update(request, token);
MudDialog.Close();
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync();
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;
}
}
public async Task SelectFileAsync()
{
DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true };
var dialog = await DialogService.ShowAsync("انتخاب عکس برند", maxWidth);
var result = await dialog.Result;
var file = result.Data;
if (file is StorageFileSDto storageFile)
Files.Add(storageFile);
}
public void RemoveFile(StorageFileSDto file)
{
Files.Remove(file);
}
}