using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net; using System.Threading.Tasks; using Oqtane.Services; using Oqtane.Shared; using System.Text.Json; namespace SZUAbsolventenverein.Module.HallOfFame.Services { public interface IHallOfFameService { Task> GetHallOfFamesAsync(int ModuleId); Task GetHallOfFameAsync(int HallOfFameId, int ModuleId); Task GetHallOfFameByUserIdAsync(int UserId, int ModuleId); Task AddHallOfFameAsync(Models.HallOfFame HallOfFame); Task UpdateHallOfFameAsync(Models.HallOfFame HallOfFame); Task DeleteHallOfFameAsync(int HallOfFameId, int ModuleId); Task ReportAsync(int HallOfFameId, int ModuleId, string reason); Task> GetHallOfFameReportsAsync(int HallOfFameId, int ModuleId); Task DeleteHallOfFameReportAsync(int HallOfFameReportId, int ModuleId); Task UploadFileAsync(System.IO.Stream stream, string fileName, int ModuleId); } public class HallOfFameService : ServiceBase, IHallOfFameService { public HallOfFameService(HttpClient http, SiteState siteState) : base(http, siteState) { } private string Apiurl => CreateApiUrl("HallOfFame"); public async Task> GetHallOfFamesAsync(int ModuleId) { return await GetJsonAsync>(CreateAuthorizationPolicyUrl($"{Apiurl}?moduleid={ModuleId}", EntityNames.Module, ModuleId), Enumerable.Empty().ToList()); } public async Task GetHallOfFameAsync(int HallOfFameId, int ModuleId) { return await GetJsonAsync(CreateAuthorizationPolicyUrl($"{Apiurl}/{HallOfFameId}/{ModuleId}", EntityNames.Module, ModuleId)); } public async Task GetHallOfFameByUserIdAsync(int UserId, int ModuleId) { return await GetJsonAsync(CreateAuthorizationPolicyUrl($"{Apiurl}/user/{UserId}?moduleid={ModuleId}", EntityNames.Module, ModuleId)); } public async Task AddHallOfFameAsync(Models.HallOfFame HallOfFame) { return await PostJsonAsync(CreateAuthorizationPolicyUrl($"{Apiurl}", EntityNames.Module, HallOfFame.ModuleId), HallOfFame); } public async Task UpdateHallOfFameAsync(Models.HallOfFame HallOfFame) { return await PutJsonAsync(CreateAuthorizationPolicyUrl($"{Apiurl}/{HallOfFame.HallOfFameId}", EntityNames.Module, HallOfFame.ModuleId), HallOfFame); } public async Task DeleteHallOfFameAsync(int HallOfFameId, int ModuleId) { await DeleteAsync(CreateAuthorizationPolicyUrl($"{Apiurl}/{HallOfFameId}/{ModuleId}", EntityNames.Module, ModuleId)); } public async Task ReportAsync(int HallOfFameId, int ModuleId, string reason) { await PutAsync(CreateAuthorizationPolicyUrl($"{Apiurl}/report/{HallOfFameId}?reason={WebUtility.UrlEncode(reason)}", EntityNames.Module, ModuleId)); } public async Task> GetHallOfFameReportsAsync(int HallOfFameId, int ModuleId) { return await GetJsonAsync>(CreateAuthorizationPolicyUrl($"{Apiurl}/reports/{HallOfFameId}?moduleid={ModuleId}", EntityNames.Module, ModuleId), Enumerable.Empty().ToList()); } public async Task DeleteHallOfFameReportAsync(int HallOfFameReportId, int ModuleId) { await DeleteAsync(CreateAuthorizationPolicyUrl($"{Apiurl}/report/{HallOfFameReportId}/{ModuleId}", EntityNames.Module, ModuleId)); } public async Task UploadFileAsync(System.IO.Stream stream, string fileName, int ModuleId) { var uri = CreateAuthorizationPolicyUrl($"{Apiurl}/upload", EntityNames.Module, ModuleId); using var content = new MultipartFormDataContent(); var fileContent = new StreamContent(stream); fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); content.Add(fileContent, "file", fileName); var response = await GetHttpClient().PostAsync(uri, content); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); return result["url"]; } return null; } } }