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/?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 _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//5 [HttpGet("{id}/{moduleid}")] [Authorize(Policy = PolicyNames.ViewModule)] public async Task 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/ [HttpPost] [Authorize(Policy = PolicyNames.EditModule)] public async Task 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//5 [HttpPut("{id}")] [Authorize(Policy = PolicyNames.EditModule)] public async Task 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//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; } } } }