using API.Schema;
using API.Schema.NotificationConnectors;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using static Microsoft.AspNetCore.Http.StatusCodes;
namespace API.Controllers;
[ApiVersion(2)]
[ApiController]
[Produces("application/json")]
[Route("v{v:apiVersion}/[controller]")]
public class NotificationConnectorController(PgsqlContext context) : Controller
{
///
/// Gets all configured Notification-Connectors
///
/// Array of configured Notification-Connectors
[HttpGet]
[ProducesResponseType(Status200OK)]
public IActionResult GetAllConnectors()
{
NotificationConnector[] ret = context.NotificationConnectors.ToArray();
return Ok(ret);
}
///
/// Returns Notification-Connector with requested ID
///
/// Notification-Connector-ID
/// Notification-Connector
[HttpGet("{id}")]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult GetConnector(string id)
{
NotificationConnector? ret = context.NotificationConnectors.Find(id);
return (ret is not null) switch
{
true => Ok(ret),
false => NotFound()
};
}
///
/// Creates a new Notification-Connector
///
/// Notification-Connector
/// Nothing
[HttpPut]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status500InternalServerError)]
public IActionResult CreateConnector([FromBody]NotificationConnector notificationConnector)
{
try
{
context.NotificationConnectors.Add(notificationConnector);
context.SaveChanges();
return Created();
}
catch (Exception e)
{
return StatusCode(500, e.Message);
}
}
///
/// Deletes the Notification-Connector with the requested ID
///
/// Notification-Connector-ID
/// Nothing
[HttpDelete("{id}")]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status404NotFound)]
[ProducesResponseType(Status500InternalServerError)]
public IActionResult DeleteConnector(string id)
{
try
{
NotificationConnector? ret = context.NotificationConnectors.Find(id);
switch (ret is not null)
{
case true:
context.Remove(ret);
context.SaveChanges();
return Ok();
case false: return NotFound();
}
}
catch (Exception e)
{
return StatusCode(500, e.Message);
}
}
}