Files
Module.HallOfFame/Client/Services/HallOfFameService.cs
Adam Gaiswinkler 1bff5ebbbd feat(halloffame): implement image upload and enhance module functionality
- Added image upload system (JPG/PNG, max 5MB) with live preview and removal option
- Fixed Concurrency Exception during deletion (split transactions for reports and entries)
- Optimized card layout: consistent height and height-based truncation for descriptions
- Added sort direction toggle (Ascending/Descending) with arrow icons for Date, Name, and Year
- Refactored HallOfFameService to use streams for Server/Wasm compatibility
- Improved error handling and UI feedback for upload and delete operations
2026-02-10 17:45:48 +01:00

100 lines
4.8 KiB
C#

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<List<Models.HallOfFame>> GetHallOfFamesAsync(int ModuleId);
Task<Models.HallOfFame> GetHallOfFameAsync(int HallOfFameId, int ModuleId);
Task<Models.HallOfFame> GetHallOfFameByUserIdAsync(int UserId, int ModuleId);
Task<Models.HallOfFame> AddHallOfFameAsync(Models.HallOfFame HallOfFame);
Task<Models.HallOfFame> UpdateHallOfFameAsync(Models.HallOfFame HallOfFame);
Task DeleteHallOfFameAsync(int HallOfFameId, int ModuleId);
Task ReportAsync(int HallOfFameId, int ModuleId, string reason);
Task<List<Models.HallOfFameReport>> GetHallOfFameReportsAsync(int HallOfFameId, int ModuleId);
Task DeleteHallOfFameReportAsync(int HallOfFameReportId, int ModuleId);
Task<string> 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<List<Models.HallOfFame>> GetHallOfFamesAsync(int ModuleId)
{
return await GetJsonAsync<List<Models.HallOfFame>>(CreateAuthorizationPolicyUrl($"{Apiurl}?moduleid={ModuleId}", EntityNames.Module, ModuleId), Enumerable.Empty<Models.HallOfFame>().ToList());
}
public async Task<Models.HallOfFame> GetHallOfFameAsync(int HallOfFameId, int ModuleId)
{
return await GetJsonAsync<Models.HallOfFame>(CreateAuthorizationPolicyUrl($"{Apiurl}/{HallOfFameId}/{ModuleId}", EntityNames.Module, ModuleId));
}
public async Task<Models.HallOfFame> GetHallOfFameByUserIdAsync(int UserId, int ModuleId)
{
return await GetJsonAsync<Models.HallOfFame>(CreateAuthorizationPolicyUrl($"{Apiurl}/user/{UserId}?moduleid={ModuleId}", EntityNames.Module, ModuleId));
}
public async Task<Models.HallOfFame> AddHallOfFameAsync(Models.HallOfFame HallOfFame)
{
return await PostJsonAsync<Models.HallOfFame>(CreateAuthorizationPolicyUrl($"{Apiurl}", EntityNames.Module, HallOfFame.ModuleId), HallOfFame);
}
public async Task<Models.HallOfFame> UpdateHallOfFameAsync(Models.HallOfFame HallOfFame)
{
return await PutJsonAsync<Models.HallOfFame>(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<List<Models.HallOfFameReport>> GetHallOfFameReportsAsync(int HallOfFameId, int ModuleId)
{
return await GetJsonAsync<List<Models.HallOfFameReport>>(CreateAuthorizationPolicyUrl($"{Apiurl}/reports/{HallOfFameId}?moduleid={ModuleId}", EntityNames.Module, ModuleId), Enumerable.Empty<Models.HallOfFameReport>().ToList());
}
public async Task DeleteHallOfFameReportAsync(int HallOfFameReportId, int ModuleId)
{
await DeleteAsync(CreateAuthorizationPolicyUrl($"{Apiurl}/report/{HallOfFameReportId}/{ModuleId}", EntityNames.Module, ModuleId));
}
public async Task<string> 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<Dictionary<string, string>>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
return result["url"];
}
return null;
}
}
}