using Oqtane.Models;
using System.Threading.Tasks;
using System.Net.Http;
using System.Linq;
using System.Collections.Generic;
using Oqtane.Documentation;
using Oqtane.Shared;
using System.Net;
namespace Oqtane.Services
{
///
/// Service to manage s on a
///
public interface IUrlMappingService
{
///
/// Get all s of this .
///
///
/// ID-reference of a
///
Task> GetUrlMappingsAsync(int siteId, bool isMapped);
///
/// Get one specific
///
/// ID-reference of a
///
Task GetUrlMappingAsync(int urlMappingId);
///
/// Get one specific
///
/// ID-reference of a
/// A url
///
Task GetUrlMappingAsync(int siteId, string url);
///
/// Add / save a new to the database.
///
///
///
Task AddUrlMappingAsync(UrlMapping urlMapping);
///
/// Update a in the database.
///
///
///
Task UpdateUrlMappingAsync(UrlMapping urlMapping);
///
/// Delete a in the database.
///
/// ID-reference of a
///
Task DeleteUrlMappingAsync(int urlMappingId);
}
[PrivateApi("Don't show in the documentation, as everything should use the Interface")]
public class UrlMappingService : ServiceBase, IUrlMappingService
{
public UrlMappingService(HttpClient http, SiteState siteState) : base(http, siteState) { }
private string Apiurl => CreateApiUrl("UrlMapping");
public async Task> GetUrlMappingsAsync(int siteId, bool isMapped)
{
List urlMappings = await GetJsonAsync>($"{Apiurl}?siteid={siteId}&ismapped={isMapped}");
return urlMappings.OrderByDescending(item => item.RequestedOn).ToList();
}
public async Task GetUrlMappingAsync(int urlMappingId)
{
return await GetJsonAsync($"{Apiurl}/{urlMappingId}");
}
public async Task GetUrlMappingAsync(int siteId, string url)
{
return await GetJsonAsync($"{Apiurl}/url/{siteId}?url={WebUtility.UrlEncode(url)}");
}
public async Task AddUrlMappingAsync(UrlMapping role)
{
return await PostJsonAsync(Apiurl, role);
}
public async Task UpdateUrlMappingAsync(UrlMapping role)
{
return await PutJsonAsync($"{Apiurl}/{role.UrlMappingId}", role);
}
public async Task DeleteUrlMappingAsync(int urlMappingId)
{
await DeleteAsync($"{Apiurl}/{urlMappingId}");
}
}
}