AdminPanel/NetinaShop.AdminPanel.PWA/Dialogs/BlogActionDialogBox.razor.cs

233 lines
7.6 KiB
C#

using NetinaShop.Domain.Dtos.LargDtos;
using NetinaShop.Domain.Entities.Blogs;
using NetinaShop.Domain.Entities.Products;
namespace NetinaShop.AdminPanel.PWA.Dialogs;
public class BlogActionDialogBoxViewModel : BaseViewModel
{
private readonly ISnackbar _snackbar;
private readonly IRestWrapper _restWrapper;
private readonly IUserUtility _userUtility;
private readonly IDialogService _dialogService;
private readonly MudDialogInstance _mudDialog;
public BlogActionDialogBoxViewModel(ISnackbar snackbar,
IRestWrapper restWrapper,
IUserUtility userUtility,
IDialogService dialogService,
MudDialogInstance mudDialog)
{
_snackbar = snackbar;
_restWrapper = restWrapper;
_userUtility = userUtility;
_dialogService = dialogService;
_mudDialog = mudDialog;
}
public BlogActionDialogBoxViewModel(ISnackbar snackbar,
IRestWrapper restWrapper,
IUserUtility userUtility,
IDialogService dialogService,
MudDialogInstance mudDialog,
BlogSDto blog)
{
_snackbar = snackbar;
_restWrapper = restWrapper;
_userUtility = userUtility;
_dialogService = dialogService;
_mudDialog = mudDialog;
Blog = blog;
}
private BlogSDto? _blog = null;
public BlogSDto? Blog
{
get => _blog;
set
{
_blog = value;
if (_blog != null)
{
IsEditing = true;
}
}
}
public bool IsEditing = false;
public string Title { get; set; } = string.Empty;
public string Content { get; set; } = string.Empty;
public int ReadingTime { get; set; }
public string Tags { get; set; } = string.Empty;
public string Summery { get; set; } = string.Empty;
public bool IsSuggested { get; set; }
public readonly ObservableCollection<StorageFileSDto> Files = new ObservableCollection<StorageFileSDto>();
public override async Task InitializeAsync()
{
if (IsEditing && _blog != null)
{
try
{
IsProcessing = true;
var blogLDto = await _restWrapper.CrudDtoApiRest<Blog, BlogLDto, Guid>(Address.BlogController).ReadOne(_blog.Id);
Title = blogLDto.Title;
Content = blogLDto.Content;
ReadingTime = blogLDto.ReadingTime;
Tags = blogLDto.Tags;
Summery = blogLDto.Summery;
Tags = blogLDto.Tags;
IsSuggested = blogLDto.IsSuggested;
blogLDto.Files.ForEach(f => Files.Add(f));
SelectedCategory = new BlogCategorySDto() { Id = blogLDto.CategoryId, Name = blogLDto.CategoryName };
}
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 void Cancel() => _mudDialog.Cancel();
public async Task SubmitEditAsync()
{
try
{
IsProcessing = true;
if (Blog == null || Blog.Id == default)
throw new Exception("بلاگ اشتباه است");
if (SelectedCategory == null)
throw new Exception("لطفا یک دسته بندی انتخاب کنید");
var token = await _userUtility.GetBearerTokenAsync();
var request = new BlogLDto
{
Id = Blog.Id,
Title = Title,
CategoryId = SelectedCategory.Id,
Content = Content,
Files = this.Files.ToList(),
IsSuggested = IsSuggested,
ReadingTime = ReadingTime,
Summery = Summery,
Tags = Tags
};
await _restWrapper.CrudApiRest<Blog, Guid>(Address.BlogController).Create<BlogLDto>(request, token);
_snackbar.Add($"ویرایش بلاگ {Title} با موفقیت انجام شد", Severity.Success);
_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 async Task SubmitCreateAsync()
{
try
{
IsProcessing = true;
if (SelectedCategory == null)
throw new Exception("لطفا یک دسته بندی انتخاب کنید");
var token = await _userUtility.GetBearerTokenAsync();
var request = new BlogLDto
{
Title = Title,
CategoryId = SelectedCategory.Id,
Content = Content,
Files = this.Files.ToList(),
IsSuggested = IsSuggested,
ReadingTime = ReadingTime,
Summery = Summery,
Tags = Tags
};
await _restWrapper.CrudApiRest<Blog, Guid>(Address.BlogController).Create<BlogLDto>(request, token);
_snackbar.Add($"ساخت بلاگ {Title} با موفقیت انجام شد", Severity.Success);
_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;
}
}
private List<BlogCategorySDto> _blogCategories = new List<BlogCategorySDto>();
public BlogCategorySDto? SelectedCategory;
public async Task<IEnumerable<BlogCategorySDto>> SearchBlogCategory(string category)
{
try
{
if (category.IsNullOrEmpty())
_blogCategories = await _restWrapper.BlogCategoryRestApi.ReadAll(0);
else
_blogCategories = await _restWrapper.BlogCategoryRestApi.ReadAll(category);
return _blogCategories;
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync<ApiResult>();
_snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
return _blogCategories;
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
return _blogCategories;
}
}
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)
Files.Add(storageFile);
}
public void RemoveFile(StorageFileSDto file)
{
Files.Remove(file);
}
}