using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Http; using Oqtane.Shared; using Oqtane.Enums; using Oqtane.Infrastructure; using SZUAbsolventenverein.Module.HallOfFame.Services; using Oqtane.Controllers; using System.Net; using System.Threading.Tasks; namespace SZUAbsolventenverein.Module.HallOfFame.Controllers { [Route(ControllerRoutes.ApiRoute)] public class HallOfFameController : ModuleControllerBase { private readonly IHallOfFameService _HallOfFameService; public HallOfFameController(IHallOfFameService HallOfFameService, ILogManager logger, IHttpContextAccessor accessor) : base(logger, accessor) { _HallOfFameService = HallOfFameService; } // GET: api/?moduleid=x // GET: api/?moduleid=x [HttpGet] [Authorize(Policy = PolicyNames.ViewModule)] public async Task> Get(string moduleid) { int ModuleId; if (int.TryParse(moduleid, out ModuleId) && IsAuthorizedEntityId(EntityNames.Module, ModuleId)) { var list = await _HallOfFameService.GetHallOfFamesAsync(ModuleId); // Filter: Show only Published unless user has Edit permissions (simplified check for now, can be expanded) // For now, let's filter in memory or service. The requirement says: "Hauptseite zeigt nur Published". // We will filter here. return list.Where(item => item.Status == "Published"); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Get Attempt {ModuleId}", moduleid); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; return null; } } // GET api//5 [HttpGet("{id}/{moduleid}")] [Authorize(Policy = PolicyNames.ViewModule)] public async Task Get(int id, int moduleid) { Models.HallOfFame HallOfFame = await _HallOfFameService.GetHallOfFameAsync(id, moduleid); if (HallOfFame != null && IsAuthorizedEntityId(EntityNames.Module, HallOfFame.ModuleId)) { return HallOfFame; } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Get Attempt {HallOfFameId} {ModuleId}", id, moduleid); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; return null; } } // GET api//user/5?moduleid=x [HttpGet("user/{userid}")] [Authorize(Policy = PolicyNames.ViewModule)] public async Task GetByUserId(int userid, string moduleid) { int ModuleId; if (int.TryParse(moduleid, out ModuleId) && IsAuthorizedEntityId(EntityNames.Module, ModuleId)) { var list = await _HallOfFameService.GetHallOfFamesAsync(ModuleId); return list.FirstOrDefault(item => item.UserId == userid); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame GetByUserId Attempt {UserId} {ModuleId}", userid, moduleid); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; return null; } } // POST api/ [HttpPost] [Authorize(Policy = PolicyNames.EditModule)] public async Task Post([FromBody] Models.HallOfFame HallOfFame) { if (ModelState.IsValid && IsAuthorizedEntityId(EntityNames.Module, HallOfFame.ModuleId)) { // Enforce one entry per user var allEntries = await _HallOfFameService.GetHallOfFamesAsync(HallOfFame.ModuleId); if (allEntries.Any(e => e.UserId == HallOfFame.UserId)) { _logger.Log(LogLevel.Error, this, LogFunction.Security, "User {UserId} already has a Hall of Fame entry.", HallOfFame.UserId); HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; return null; } HallOfFame = await _HallOfFameService.AddHallOfFameAsync(HallOfFame); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Post Attempt {HallOfFame}", HallOfFame); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; HallOfFame = null; } return HallOfFame; } // PUT api//5 [HttpPut("{id}")] [Authorize(Policy = PolicyNames.EditModule)] public async Task Put(int id, [FromBody] Models.HallOfFame HallOfFame) { if (ModelState.IsValid && HallOfFame.HallOfFameId == id && IsAuthorizedEntityId(EntityNames.Module, HallOfFame.ModuleId)) { var existing = await _HallOfFameService.GetHallOfFameAsync(id, HallOfFame.ModuleId); if (existing != null && existing.UserId == HallOfFame.UserId) { HallOfFame = await _HallOfFameService.UpdateHallOfFameAsync(HallOfFame); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Put Attempt by User {UserId} for Entry {HallOfFameId}", HallOfFame.UserId, id); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; HallOfFame = null; } } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Put Attempt {HallOfFame}", HallOfFame); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; HallOfFame = null; } return HallOfFame; } // DELETE api//5 [HttpDelete("{id}/{moduleid}")] [Authorize(Policy = PolicyNames.EditModule)] public async Task Delete(int id, int moduleid) { Models.HallOfFame HallOfFame = await _HallOfFameService.GetHallOfFameAsync(id, moduleid); if (HallOfFame != null && IsAuthorizedEntityId(EntityNames.Module, HallOfFame.ModuleId)) { await _HallOfFameService.DeleteHallOfFameAsync(id, HallOfFame.ModuleId); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Delete Attempt {HallOfFameId} {ModuleId}", id, moduleid); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; } } } }