using Oqtane.Models;
using System.Threading.Tasks;
using System.Net.Http;
using System.Collections.Generic;
using Oqtane.Shared;
using System.Net;
using Oqtane.Documentation;
namespace Oqtane.Services
{
///
/// Services to store and retrieve a
///
public interface IPageService
{
///
/// Returns a list of pages
///
///
///
Task> GetPagesAsync(int siteId);
///
/// Returns a specific page
///
///
///
Task GetPageAsync(int pageId);
///
/// Returns a specific page by its defined path
///
///
///
///
Task GetPageAsync(string path, int siteId);
///
/// Adds a new page
///
///
///
Task AddPageAsync(Page page);
///
/// Adds a new page
///
///
///
Task AddPageAsync(int pageId, int userId);
///
/// Updates a existing page
///
///
///
Task UpdatePageAsync(Page page);
///
/// Updates order of all page modules in the given parent
///
///
///
///
///
Task UpdatePageOrderAsync(int siteId, int pageId, int? parentId);
///
/// Deletes a page
///
///
///
Task DeletePageAsync(int pageId);
}
[PrivateApi("Don't show in the documentation, as everything should use the Interface")]
public class PageService : ServiceBase, IPageService
{
public PageService(HttpClient http, SiteState siteState) : base(http, siteState) { }
private string Apiurl => CreateApiUrl("Page");
public async Task> GetPagesAsync(int siteId)
{
return await GetJsonAsync>($"{Apiurl}?siteid={siteId}");
}
public async Task GetPageAsync(int pageId)
{
return await GetJsonAsync($"{Apiurl}/{pageId}");
}
public async Task GetPageAsync(string path, int siteId)
{
try
{
return await GetJsonAsync($"{Apiurl}/path/{siteId}?path={WebUtility.UrlEncode(path)}");
}
catch
{
return null;
}
}
public async Task AddPageAsync(Page page)
{
return await PostJsonAsync(Apiurl, page);
}
public async Task AddPageAsync(int pageId, int userId)
{
return await PostJsonAsync($"{Apiurl}/{pageId}?userid={userId}", null);
}
public async Task UpdatePageAsync(Page page)
{
return await PutJsonAsync($"{Apiurl}/{page.PageId}", page);
}
public async Task UpdatePageOrderAsync(int siteId, int pageId, int? parentId)
{
var parent = parentId == null
? string.Empty
: parentId.ToString();
await PutAsync($"{Apiurl}/?siteid={siteId}&pageid={pageId}&parentid={parent}");
}
public async Task DeletePageAsync(int pageId)
{
await DeleteAsync($"{Apiurl}/{pageId}");
}
}
}