216 lines
6.9 KiB
C#
216 lines
6.9 KiB
C#
using Netina.Domain.Entities.Blogs;
|
|
using Netina.Domain.Entities.Warehouses;
|
|
using System.Reflection.Metadata;
|
|
|
|
namespace Netina.AdminPanel.PWA.Dialogs;
|
|
|
|
public class PageActionDialogBoxViewModel : BaseViewModel<BasePageLDto>
|
|
{
|
|
private readonly ISnackbar _snackbar;
|
|
private readonly IRestWrapper _restWrapper;
|
|
private readonly IUserUtility _userUtility;
|
|
private readonly IDialogService _dialogService;
|
|
private readonly MudDialogInstance _mudDialog;
|
|
|
|
|
|
public PageActionDialogBoxViewModel(ISnackbar snackbar,
|
|
IRestWrapper restWrapper,
|
|
IUserUtility userUtility,
|
|
IDialogService dialogService,
|
|
MudDialogInstance mudDialog) : base(userUtility)
|
|
{
|
|
_snackbar = snackbar;
|
|
_restWrapper = restWrapper;
|
|
_userUtility = userUtility;
|
|
_dialogService = dialogService;
|
|
_mudDialog = mudDialog;
|
|
}
|
|
|
|
public PageActionDialogBoxViewModel(ISnackbar snackbar,
|
|
IRestWrapper restWrapper,
|
|
IUserUtility userUtility,
|
|
IDialogService dialogService,
|
|
MudDialogInstance mudDialog,
|
|
BasePageSDto page) : base(userUtility)
|
|
{
|
|
_snackbar = snackbar;
|
|
_restWrapper = restWrapper;
|
|
_userUtility = userUtility;
|
|
_dialogService = dialogService;
|
|
_mudDialog = mudDialog;
|
|
IsEditing = true;
|
|
PageId = page.Id;
|
|
}
|
|
public override async Task InitializeAsync()
|
|
{
|
|
try
|
|
{
|
|
IsProcessing = true;
|
|
var token = await _userUtility.GetBearerTokenAsync();
|
|
if (token == null)
|
|
throw new Exception("Token is null");
|
|
var pages = await _restWrapper.PageRestApi.ReadById(PageId, token);
|
|
PageDto = pages;
|
|
}
|
|
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 bool IsEditing = false;
|
|
public Guid PageId { get; set; }
|
|
|
|
public void Cancel() => _mudDialog.Cancel();
|
|
|
|
public async Task AddSection()
|
|
{
|
|
try
|
|
{
|
|
|
|
DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true };
|
|
var dialog = await _dialogService.ShowAsync<PageSectionActionDialogBox>("افزودن سکشن", maxWidth);
|
|
var result = await dialog.Result;
|
|
var file = result.Data;
|
|
if (file is BasePageSection section)
|
|
PageDto.Sections.Add(section);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_snackbar.Add(ex.Message, Severity.Error);
|
|
}
|
|
}
|
|
|
|
public async Task EditSection(BasePageSection sec)
|
|
{
|
|
try
|
|
{
|
|
|
|
DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true };
|
|
var parameters = new DialogParameters<PageSectionActionDialogBox>();
|
|
parameters.Add(x => x.Page, sec);
|
|
var dialog = await _dialogService.ShowAsync<PageSectionActionDialogBox>("ویرایش سکشن",parameters, maxWidth);
|
|
var result = await dialog.Result;
|
|
var file = result.Data;
|
|
if (file is BasePageSection section)
|
|
{
|
|
PageDto.Sections.Remove(sec);
|
|
PageDto.Sections.Add(section);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_snackbar.Add(ex.Message, Severity.Error);
|
|
}
|
|
}
|
|
|
|
public async Task SubmitEditAsync()
|
|
{
|
|
try
|
|
{
|
|
IsProcessing = true;
|
|
if (PageDto.Id == default)
|
|
throw new Exception("Id is null !");
|
|
|
|
var token = await _userUtility.GetBearerTokenAsync();
|
|
if (token == null)
|
|
throw new Exception("Token is null");
|
|
|
|
if (PageDto.Title.IsNullOrEmpty())
|
|
throw new Exception("نام را وارد کنید");
|
|
|
|
var request = new PageActionRequestDto
|
|
{
|
|
Id = PageDto.Id,
|
|
Title = PageDto.Title,
|
|
Description = PageDto.Description,
|
|
Content = PageDto.Content,
|
|
IsCustomPage = PageDto.IsCustomPage,
|
|
IsHtmlBasePage = PageDto.IsHtmlBasePage,
|
|
Slug = PageDto.Slug,
|
|
Data = PageDto.Data,
|
|
Indexing = PageDto.Indexing,
|
|
Sections = PageDto.Sections
|
|
};
|
|
await _restWrapper.CrudApiRest<Shipping, Guid>(Address.PageController).Update<PageActionRequestDto>(request, token);
|
|
_snackbar.Add($"ویرایش {PageDto.Title} با موفقیت انجام شد", Severity.Success);
|
|
_mudDialog.Close(DialogResult.Ok(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;
|
|
}
|
|
}
|
|
|
|
public async Task SubmitCreateAsync()
|
|
{
|
|
try
|
|
{
|
|
IsProcessing = true;
|
|
|
|
var token = await _userUtility.GetBearerTokenAsync();
|
|
if (token == null)
|
|
throw new Exception("Token is null");
|
|
|
|
if (PageDto.Title.IsNullOrEmpty())
|
|
throw new Exception("نام را وارد کنید");
|
|
|
|
var request = new PageActionRequestDto
|
|
{
|
|
Title = PageDto.Title,
|
|
Description = PageDto.Description,
|
|
Content = PageDto.Content,
|
|
IsCustomPage = PageDto.IsCustomPage,
|
|
IsHtmlBasePage = PageDto.IsHtmlBasePage,
|
|
Slug = PageDto.Slug,
|
|
Data = PageDto.Data,
|
|
Indexing = PageDto.Indexing,
|
|
Sections = PageDto.Sections
|
|
};
|
|
await _restWrapper.CrudApiRest<Shipping, Guid>(Address.PageController).Create<PageActionRequestDto>(request, token);
|
|
_snackbar.Add($"ساخت {PageDto.Title} با موفقیت انجام شد", Severity.Success);
|
|
_mudDialog.Close(DialogResult.Ok(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;
|
|
}
|
|
}
|
|
}
|