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.EventRegistration.Services; using Oqtane.Controllers; using System.Net; using System.Threading.Tasks; namespace SZUAbsolventenverein.Module.EventRegistration.Controllers { [Route(ControllerRoutes.ApiRoute)] public class EventRegistrationController : ModuleControllerBase { private readonly IEventRegistrationService _EventRegistrationService; public EventRegistrationController(IEventRegistrationService EventRegistrationService, ILogManager logger, IHttpContextAccessor accessor) : base(logger, accessor) { _EventRegistrationService = EventRegistrationService; } // 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 _EventRegistrationService.GetEventsAsync(ModuleId); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration 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.Event EventRegistration = await _EventRegistrationService.GetEventAsync(id, moduleid); if (EventRegistration != null && IsAuthorizedEntityId(EntityNames.Module, EventRegistration.ModuleId)) { return EventRegistration; } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Get Attempt {EventRegistrationId} {ModuleId}", id, moduleid); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; return null; } } // POST api/ [HttpPost] [Authorize(Policy = PolicyNames.EditModule)] public async Task Post([FromBody] Models.Event EventRegistration) { if (ModelState.IsValid && IsAuthorizedEntityId(EntityNames.Module, EventRegistration.ModuleId)) { EventRegistration = await _EventRegistrationService.AddEventAsync(EventRegistration); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Post Attempt {EventRegistration}", EventRegistration); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; EventRegistration = null; } return EventRegistration; } // PUT api//5 [HttpPut("{id}")] [Authorize(Policy = PolicyNames.EditModule)] public async Task Put(int id, [FromBody] Models.Event EventRegistration) { if (ModelState.IsValid && EventRegistration.EventId == id && IsAuthorizedEntityId(EntityNames.Module, EventRegistration.ModuleId)) { EventRegistration = await _EventRegistrationService.UpdateEventAsync(EventRegistration); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Put Attempt {EventRegistration}", EventRegistration); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; EventRegistration = null; } return EventRegistration; } // DELETE api//5 [HttpDelete("{id}/{moduleid}")] [Authorize(Policy = PolicyNames.EditModule)] public async Task Delete(int id, int moduleid) { Models.Event EventRegistration = await _EventRegistrationService.GetEventAsync(id, moduleid); if (EventRegistration != null && IsAuthorizedEntityId(EntityNames.Module, EventRegistration.ModuleId)) { await _EventRegistrationService.DeleteEventAsync(id, EventRegistration.ModuleId); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Delete Attempt {EventRegistrationId} {ModuleId}", id, moduleid); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; } } // GET api//5 [HttpGet("details/{id}/{moduleid}")] [Authorize(Policy = PolicyNames.ViewModule)] public async Task<(Models.Event, Models.Response)> GetDetails(int id, int moduleid) { Models.Event EventRegistration; Models.Response EventResponse; (EventRegistration, EventResponse) = await _EventRegistrationService.GetEventDetails(id, moduleid); if (EventRegistration != null && EventResponse != null && IsAuthorizedEntityId(EntityNames.Module, EventRegistration.ModuleId)) { return (EventRegistration, EventResponse); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Get Attempt {EventRegistrationId} {ModuleId}", id, moduleid); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; return (null, null); } } // TODO: Add Event Response Endpoints. } }