Module.EventRegistration/Server/Controllers/AnmeldeToolController.cs
2025-04-07 11:47:28 +02:00

115 lines
4.6 KiB
C#

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 AdamGais.Module.AnmeldeTool.Services;
using Oqtane.Controllers;
using System.Net;
using System.Threading.Tasks;
namespace AdamGais.Module.AnmeldeTool.Controllers
{
[Route(ControllerRoutes.ApiRoute)]
public class AnmeldeToolController : ModuleControllerBase
{
private readonly IAnmeldeToolService _AnmeldeToolService;
public AnmeldeToolController(IAnmeldeToolService AnmeldeToolService, ILogManager logger, IHttpContextAccessor accessor) : base(logger, accessor)
{
_AnmeldeToolService = AnmeldeToolService;
}
// GET: api/<controller>?moduleid=x
[HttpGet]
[Authorize(Policy = PolicyNames.ViewModule)]
public async Task<IEnumerable<Models.AnmeldeTool>> Get(string moduleid)
{
int ModuleId;
if (int.TryParse(moduleid, out ModuleId) && IsAuthorizedEntityId(EntityNames.Module, ModuleId))
{
return await _AnmeldeToolService.GetAnmeldeToolsAsync(ModuleId);
}
else
{
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized AnmeldeTool Get Attempt {ModuleId}", moduleid);
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
return null;
}
}
// GET api/<controller>/5
[HttpGet("{id}/{moduleid}")]
[Authorize(Policy = PolicyNames.ViewModule)]
public async Task<Models.AnmeldeTool> Get(int id, int moduleid)
{
Models.AnmeldeTool AnmeldeTool = await _AnmeldeToolService.GetAnmeldeToolAsync(id, moduleid);
if (AnmeldeTool != null && IsAuthorizedEntityId(EntityNames.Module, AnmeldeTool.ModuleId))
{
return AnmeldeTool;
}
else
{
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized AnmeldeTool Get Attempt {AnmeldeToolId} {ModuleId}", id, moduleid);
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
return null;
}
}
// POST api/<controller>
[HttpPost]
[Authorize(Policy = PolicyNames.EditModule)]
public async Task<Models.AnmeldeTool> Post([FromBody] Models.AnmeldeTool AnmeldeTool)
{
if (ModelState.IsValid && IsAuthorizedEntityId(EntityNames.Module, AnmeldeTool.ModuleId))
{
AnmeldeTool = await _AnmeldeToolService.AddAnmeldeToolAsync(AnmeldeTool);
}
else
{
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized AnmeldeTool Post Attempt {AnmeldeTool}", AnmeldeTool);
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
AnmeldeTool = null;
}
return AnmeldeTool;
}
// PUT api/<controller>/5
[HttpPut("{id}")]
[Authorize(Policy = PolicyNames.EditModule)]
public async Task<Models.AnmeldeTool> Put(int id, [FromBody] Models.AnmeldeTool AnmeldeTool)
{
if (ModelState.IsValid && AnmeldeTool.AnmeldeToolId == id && IsAuthorizedEntityId(EntityNames.Module, AnmeldeTool.ModuleId))
{
AnmeldeTool = await _AnmeldeToolService.UpdateAnmeldeToolAsync(AnmeldeTool);
}
else
{
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized AnmeldeTool Put Attempt {AnmeldeTool}", AnmeldeTool);
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
AnmeldeTool = null;
}
return AnmeldeTool;
}
// DELETE api/<controller>/5
[HttpDelete("{id}/{moduleid}")]
[Authorize(Policy = PolicyNames.EditModule)]
public async Task Delete(int id, int moduleid)
{
Models.AnmeldeTool AnmeldeTool = await _AnmeldeToolService.GetAnmeldeToolAsync(id, moduleid);
if (AnmeldeTool != null && IsAuthorizedEntityId(EntityNames.Module, AnmeldeTool.ModuleId))
{
await _AnmeldeToolService.DeleteAnmeldeToolAsync(id, AnmeldeTool.ModuleId);
}
else
{
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized AnmeldeTool Delete Attempt {AnmeldeToolId} {ModuleId}", id, moduleid);
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
}
}
}
}