using Oqtane.Models; using System.Threading.Tasks; using System.Net.Http; using System.Linq; using System.Collections.Generic; using Oqtane.Documentation; using Oqtane.Shared; namespace Oqtane.Services { /// /// Service to manage s on a /// public interface IRoleService { /// /// Get all s of this . /// /// Will exclude global roles which are for all sites. To get those as well, use the overload /// /// ID-reference of a /// Task> GetRolesAsync(int siteId); /// /// Get roles of the and optionally include global Roles. /// /// ID-reference to a /// True if it should also include global roles. False will return the same data as just calling /// Task> GetRolesAsync(int siteId, bool includeGlobalRoles); /// /// Get one specific /// /// ID-reference of a /// Task GetRoleAsync(int roleId); /// /// Add / save a new to the database. /// /// /// Task AddRoleAsync(Role role); /// /// Update a in the database. /// /// /// Task UpdateRoleAsync(Role role); /// /// Delete / mark-as-deleted a in the database. /// /// ID-reference of a /// Task DeleteRoleAsync(int roleId); } [PrivateApi("Don't show in the documentation, as everything should use the Interface")] public class RoleService : ServiceBase, IRoleService { public RoleService(HttpClient http, SiteState siteState) : base(http, siteState) { } private string Apiurl => CreateApiUrl("Role"); public async Task> GetRolesAsync(int siteId) { return await GetRolesAsync(siteId, false); } public async Task> GetRolesAsync(int siteId, bool includeGlobalRoles) { List roles = await GetJsonAsync>($"{Apiurl}?siteid={siteId}&global={includeGlobalRoles}"); return roles.OrderBy(item => item.Name).ToList(); } public async Task GetRoleAsync(int roleId) { return await GetJsonAsync($"{Apiurl}/{roleId}"); } public async Task AddRoleAsync(Role role) { return await PostJsonAsync(Apiurl, role); } public async Task UpdateRoleAsync(Role role) { return await PutJsonAsync($"{Apiurl}/{role.RoleId}", role); } public async Task DeleteRoleAsync(int roleId) { await DeleteAsync($"{Apiurl}/{roleId}"); } } }