using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using System.Collections.Generic; 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 [HttpGet] [Authorize(Policy = PolicyNames.ViewModule)] public async Task> Get(string moduleid) { int ModuleId; if (int.TryParse(moduleid, out ModuleId) && IsAuthorizedEntityId(EntityNames.Module, ModuleId)) { return await _HallOfFameService.GetHallOfFamesAsync(ModuleId); } 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; } } // POST api/ [HttpPost] [Authorize(Policy = PolicyNames.EditModule)] public async Task Post([FromBody] Models.HallOfFame HallOfFame) { if (ModelState.IsValid && IsAuthorizedEntityId(EntityNames.Module, HallOfFame.ModuleId)) { 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)) { HallOfFame = await _HallOfFameService.UpdateHallOfFameAsync(HallOfFame); } 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; } } } }