oqtane.framework/Oqtane.Client/Modules/HtmlText/Services/HtmlTextService.cs
Pavel Vesely ff18059b06 HtmlText null exception fix
HtmlText Mode switch fix
Control panel fix

(cherry picked from commit b7d2cd0600)
2020-03-06 22:55:46 +01:00

65 lines
2.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using Microsoft.AspNetCore.Components;
using Oqtane.Services;
using Oqtane.Modules.HtmlText.Models;
using Oqtane.Shared;
using Oqtane.Models;
namespace Oqtane.Modules.HtmlText.Services
{
public class HtmlTextService : ServiceBase, IHtmlTextService
{
private readonly HttpClient _http;
private readonly SiteState _siteState;
private readonly NavigationManager _navigationManager;
public HtmlTextService(HttpClient http, SiteState siteState, NavigationManager navigationManager)
{
_http = http;
_siteState = siteState;
_navigationManager = navigationManager;
}
private string apiurl
{
get { return CreateApiUrl(_siteState.Alias, _navigationManager.Uri, "HtmlText"); }
}
public async Task<HtmlTextInfo> GetHtmlTextAsync(int ModuleId)
{
HtmlTextInfo htmltext;
try
{
//because GetJsonAsync() returns an error if no content exists for the ModuleId ( https://github.com/aspnet/AspNetCore/issues/14041 )
//null value is transfered as empty list
var htmltextList = await _http.GetJsonAsync<List<HtmlTextInfo>>(apiurl + "/" + ModuleId.ToString() + "?entityid=" + ModuleId.ToString());
htmltext = htmltextList.FirstOrDefault();
}
catch
{
htmltext = null;
}
return htmltext;
}
public async Task AddHtmlTextAsync(HtmlTextInfo htmltext)
{
await _http.PostJsonAsync(apiurl + "?entityid=" + htmltext.ModuleId.ToString(), htmltext);
}
public async Task UpdateHtmlTextAsync(HtmlTextInfo htmltext)
{
await _http.PutJsonAsync(apiurl + "/" + htmltext.HtmlTextId.ToString() + "?entityid=" + htmltext.ModuleId.ToString(), htmltext);
}
public async Task DeleteHtmlTextAsync(int ModuleId)
{
await _http.DeleteAsync(apiurl + "/" + ModuleId.ToString() + "?entityid=" + ModuleId.ToString());
}
}
}