2023-02-26 09:40:04 -08:00
|
|
|
using System.Net;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using Newsbot.Collector.Database.Repositories;
|
2023-03-11 10:43:06 -08:00
|
|
|
using Newsbot.Collector.Domain.Dto;
|
2023-02-26 09:40:04 -08:00
|
|
|
using Newsbot.Collector.Domain.Interfaces;
|
|
|
|
using Newsbot.Collector.Domain.Models;
|
|
|
|
|
|
|
|
namespace Newsbot.Collector.Api.Controllers;
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
[Route("api/discord/webhooks")]
|
|
|
|
public class DiscordWebHookController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly ILogger<DiscordWebHookController> _logger;
|
|
|
|
private readonly ConnectionStrings _settings;
|
|
|
|
private readonly IDiscordWebHooksRepository _webhooks;
|
|
|
|
|
|
|
|
public DiscordWebHookController(ILogger<DiscordWebHookController> logger, IOptions<ConnectionStrings> settings)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_settings = settings.Value;
|
|
|
|
_webhooks = new DiscordWebhooksTable(_settings.Database);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet(Name = "GetDiscordWebhooks")]
|
2023-03-11 10:43:06 -08:00
|
|
|
public IEnumerable<DiscordWebHookDto> Get(int page)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-03-11 10:43:06 -08:00
|
|
|
var items = new List<DiscordWebHookDto>();
|
|
|
|
var res = _webhooks.List(page);
|
|
|
|
foreach (var item in res)
|
|
|
|
{
|
|
|
|
items.Add(DiscordWebHookDto.Convert(item));
|
|
|
|
}
|
|
|
|
return items;
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost(Name = "New")]
|
2023-03-11 10:43:06 -08:00
|
|
|
public DiscordWebHookDto New(string url, string server, string channel)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-03-11 10:43:06 -08:00
|
|
|
var exists = _webhooks.GetByUrl(url);
|
|
|
|
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
|
|
|
if (exists is not null)
|
|
|
|
{
|
|
|
|
return DiscordWebHookDto.Convert(exists);
|
|
|
|
}
|
|
|
|
|
|
|
|
var res = _webhooks.New(new DiscordWebHookModel
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
Server = server,
|
|
|
|
Channel = channel,
|
|
|
|
Enabled = true,
|
|
|
|
});
|
2023-03-11 10:43:06 -08:00
|
|
|
|
|
|
|
return DiscordWebHookDto.Convert(res);
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("by/serverAndChannel")]
|
2023-03-11 10:43:06 -08:00
|
|
|
public IEnumerable<DiscordWebHookDto> GetByServerAndChannel(string server, string channel)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-03-11 10:43:06 -08:00
|
|
|
var items = new List<DiscordWebHookDto>();
|
|
|
|
var res = _webhooks.ListByServerAndChannel(server, channel, 25);
|
|
|
|
|
|
|
|
foreach (var item in res)
|
|
|
|
{
|
|
|
|
items.Add(DiscordWebHookDto.Convert(item));
|
|
|
|
}
|
|
|
|
return items;
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
2023-03-11 10:43:06 -08:00
|
|
|
public DiscordWebHookDto GetById(Guid id)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-03-11 10:43:06 -08:00
|
|
|
var res = _webhooks.GetByID(id);
|
|
|
|
return DiscordWebHookDto.Convert(res);
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("{id}/disable")]
|
|
|
|
public void DisableById(Guid id)
|
|
|
|
{
|
|
|
|
_webhooks.Disable(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("{id}/enable")]
|
|
|
|
public void EnableById(Guid id)
|
|
|
|
{
|
|
|
|
_webhooks.Enable(id);
|
|
|
|
}
|
|
|
|
}
|