using API.Schema; using API.Schema.Contexts; using API.Schema.LibraryConnectors; using Asp.Versioning; using log4net; using Microsoft.AspNetCore.Mvc; using static Microsoft.AspNetCore.Http.StatusCodes; namespace API.Controllers; [ApiVersion(2)] [ApiController] [Route("v{v:apiVersion}/[controller]")] public class LibraryConnectorController(LibraryContext context, ILog Log) : Controller { /// /// Gets all configured ToLibrary-Connectors /// /// [HttpGet] [ProducesResponseType(Status200OK, "application/json")] public IActionResult GetAllConnectors() { LibraryConnector[] connectors = context.LibraryConnectors.ToArray(); return Ok(connectors); } /// /// Returns ToLibrary-Connector with requested ID /// /// ToLibrary-Connector-ID /// /// Connector with ID not found. [HttpGet("{LibraryControllerId}")] [ProducesResponseType(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound)] public IActionResult GetConnector(string LibraryControllerId) { LibraryConnector? ret = context.LibraryConnectors.Find(LibraryControllerId); return (ret is not null) switch { true => Ok(ret), false => NotFound() }; } /// /// Creates a new ToLibrary-Connector /// /// ToLibrary-Connector /// /// Error during Database Operation [HttpPut] [ProducesResponseType(Status201Created)] [ProducesResponseType(Status500InternalServerError, "text/plain")] public IActionResult CreateConnector([FromBody]LibraryConnector libraryConnector) { try { context.LibraryConnectors.Add(libraryConnector); context.SaveChanges(); return Created(); } catch (Exception e) { Log.Error(e); return StatusCode(500, e.Message); } } /// /// Deletes the ToLibrary-Connector with the requested ID /// /// ToLibrary-Connector-ID /// /// Connector with ID not found. /// Error during Database Operation [HttpDelete("{LibraryControllerId}")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound)] [ProducesResponseType(Status500InternalServerError, "text/plain")] public IActionResult DeleteConnector(string LibraryControllerId) { try { LibraryConnector? ret = context.LibraryConnectors.Find(LibraryControllerId); if (ret is null) return NotFound(); context.Remove(ret); context.SaveChanges(); return Ok(); } catch (Exception e) { Log.Error(e); return StatusCode(500, e.Message); } } }