231 lines
7.3 KiB
C#
231 lines
7.3 KiB
C#
using Netina.Domain.Dtos.LargDtos;
|
|
using Netina.Domain.Entities.Brands;
|
|
|
|
namespace Netina.AdminPanel.PWA.Dialogs;
|
|
|
|
public class BrandActionDialogBoxViewModel : BaseViewModel<BrandLDto>
|
|
{
|
|
private readonly ISnackbar _snackbar;
|
|
private readonly IRestWrapper _restWrapper;
|
|
private readonly IUserUtility _userUtility;
|
|
private readonly IDialogService _dialogService;
|
|
private readonly MudDialogInstance _mudDialog;
|
|
|
|
public BrandActionDialogBoxViewModel(ISnackbar snackbar,
|
|
IRestWrapper restWrapper,
|
|
IUserUtility userUtility,
|
|
IDialogService dialogService,
|
|
MudDialogInstance mudDialog) : base(userUtility)
|
|
{
|
|
_snackbar = snackbar;
|
|
_restWrapper = restWrapper;
|
|
_userUtility = userUtility;
|
|
_dialogService = dialogService;
|
|
_mudDialog = mudDialog;
|
|
}
|
|
public BrandActionDialogBoxViewModel(ISnackbar snackbar,
|
|
IRestWrapper restWrapper,
|
|
IUserUtility userUtility,
|
|
IDialogService dialogService,
|
|
MudDialogInstance mudDialog,
|
|
BrandSDto brand) : base(userUtility)
|
|
{
|
|
_snackbar = snackbar;
|
|
_restWrapper = restWrapper;
|
|
_userUtility = userUtility;
|
|
_dialogService = dialogService;
|
|
_mudDialog = mudDialog;
|
|
Brand = brand;
|
|
}
|
|
|
|
private BrandSDto? _brand = null;
|
|
public BrandSDto? Brand
|
|
{
|
|
get => _brand;
|
|
set
|
|
{
|
|
_brand = value;
|
|
if (_brand != null)
|
|
{
|
|
IsEditing = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Cancel() => _mudDialog.Cancel();
|
|
|
|
public bool IsEditing = false;
|
|
|
|
public override async Task InitializeAsync()
|
|
{
|
|
if (Brand != null)
|
|
{
|
|
IsEditing = true;
|
|
try
|
|
{
|
|
IsProcessing = true;
|
|
var response = await _restWrapper.CrudDtoApiRest<Brand, BrandLDto, Guid>(Address.BrandController).ReadOne(Brand.Id);
|
|
var brandLDto = response;
|
|
brandLDto.MetaTags.ForEach(m => MetaTags.Add(m));
|
|
PageDto = brandLDto;
|
|
}
|
|
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;
|
|
}
|
|
|
|
}
|
|
await base.InitializeAsync();
|
|
}
|
|
|
|
|
|
public async Task SubmitCreateAsync()
|
|
{
|
|
try
|
|
{
|
|
if (PageDto.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(PageDto.PersianName,
|
|
PageDto.EnglishName,
|
|
PageDto.Description,
|
|
PageDto.HasSpecialPage,
|
|
PageDto.PageUrl,
|
|
PageDto.Files,
|
|
Faqs,
|
|
MetaTags.ToDictionary(x => x.Type, x => x.Value));
|
|
await _restWrapper.CrudApiRest<Brand, Guid>(Address.BrandController).Create<CreateBrandCommand>(request, token);
|
|
_mudDialog.Close(DialogResult.Ok(true));
|
|
}
|
|
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 SubmitEditAsync()
|
|
{
|
|
try
|
|
{
|
|
if (Brand == null)
|
|
throw new AppException("برند به درستی ارسال نشده است");
|
|
if (PageDto.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(
|
|
PageDto.Id,
|
|
PageDto.PersianName,
|
|
PageDto.EnglishName,
|
|
PageDto.Description,
|
|
PageDto.HasSpecialPage,
|
|
PageDto.PageUrl,
|
|
PageDto.Files,
|
|
Faqs,
|
|
MetaTags.ToDictionary(x => x.Type, x => x.Value));
|
|
|
|
await _restWrapper.CrudApiRest<Brand, Guid>(Address.BrandController).Update<UpdateBrandCommand>(request, token);
|
|
_mudDialog.Close();
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
|
|
public string FaqQuestion { get; set; } = string.Empty;
|
|
public string FaqAnswer { get; set; } = string.Empty;
|
|
public Dictionary<string, string> Faqs = new();
|
|
public void AddFaq()
|
|
{
|
|
try
|
|
{
|
|
if (FaqAnswer.IsNullOrEmpty())
|
|
throw new Exception("لطفا پاسخ را وارد کنید");
|
|
|
|
if (FaqQuestion.IsNullOrEmpty())
|
|
throw new Exception("لطفا سوال را وارد کنید");
|
|
|
|
Faqs.Add(FaqQuestion, FaqAnswer);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_snackbar.Add(e.Message, Severity.Error);
|
|
}
|
|
}
|
|
|
|
public readonly ObservableCollection<MetaTagSDto> MetaTags = new();
|
|
public string MetaTagType { get; set; } = string.Empty;
|
|
public string MetaTagValue { get; set; } = string.Empty;
|
|
public void AddMetaTag()
|
|
{
|
|
try
|
|
{
|
|
if (MetaTagType.IsNullOrEmpty())
|
|
throw new Exception("لطفا نوع متا مورد نظر را وارد کنید");
|
|
if (MetaTagValue.IsNullOrEmpty())
|
|
throw new Exception("لطفا مقدار متا مورد نظر را وارد کنید");
|
|
|
|
MetaTags.Add(new MetaTagSDto() { Type = MetaTagType, Value = MetaTagValue });
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_snackbar.Add(e.Message, Severity.Error);
|
|
}
|
|
}
|
|
|
|
|
|
public async Task SelectFileAsync()
|
|
{
|
|
DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true };
|
|
var dialog = await _dialogService.ShowAsync<StorageDialogBox>("انتخاب عکس برند", maxWidth);
|
|
var result = await dialog.Result;
|
|
var file = result.Data;
|
|
if (file is StorageFileSDto storageFile)
|
|
PageDto.Files.Add(storageFile);
|
|
}
|
|
|
|
public void RemoveFile(StorageFileSDto file)
|
|
{
|
|
PageDto.Files.Remove(file);
|
|
}
|
|
} |