2023-07-06 22:26:19 -07:00
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-02-26 09:40:04 -08:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.Extensions.Options;
|
2023-07-23 22:54:00 -07:00
|
|
|
using Newsbot.Collector.Api.Middleware;
|
2023-03-11 10:43:06 -08:00
|
|
|
using Newsbot.Collector.Domain.Dto;
|
2023-06-23 21:55:56 -07:00
|
|
|
using Newsbot.Collector.Domain.Entities;
|
2023-02-26 09:40:04 -08:00
|
|
|
using Newsbot.Collector.Domain.Interfaces;
|
|
|
|
using Newsbot.Collector.Domain.Models;
|
2023-07-29 09:44:15 -07:00
|
|
|
using Newsbot.Collector.Domain.Requests;
|
|
|
|
using Newsbot.Collector.Domain.Results;
|
2023-02-26 09:40:04 -08:00
|
|
|
|
2023-07-23 22:54:00 -07:00
|
|
|
namespace Newsbot.Collector.Api.Controllers.v1;
|
2023-02-26 09:40:04 -08:00
|
|
|
|
|
|
|
[ApiController]
|
2023-07-23 22:54:00 -07:00
|
|
|
[Route("api/v1/discord/webhooks")]
|
2023-07-06 22:26:19 -07:00
|
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
2023-02-26 09:40:04 -08:00
|
|
|
public class DiscordWebHookController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly ILogger<DiscordWebHookController> _logger;
|
|
|
|
private readonly IDiscordWebHooksRepository _webhooks;
|
|
|
|
|
2023-07-06 22:26:19 -07:00
|
|
|
public DiscordWebHookController(ILogger<DiscordWebHookController> logger, IOptions<ConnectionStrings> settings, IDiscordWebHooksRepository webhooks)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2023-07-06 22:26:19 -07:00
|
|
|
_webhooks = webhooks;
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet(Name = "GetDiscordWebhooks")]
|
2023-07-23 22:54:00 -07:00
|
|
|
public ActionResult<IEnumerable<DiscordWebHookDto>> Get(int page)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-07-23 22:54:00 -07:00
|
|
|
var userId = HttpContext.GetUserId();
|
|
|
|
if (userId.Equals(string.Empty))
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Unable to find the user ID in the JWD Token");
|
|
|
|
return new BadRequestResult();
|
|
|
|
}
|
|
|
|
|
2023-03-11 10:43:06 -08:00
|
|
|
var items = new List<DiscordWebHookDto>();
|
2023-07-23 22:54:00 -07:00
|
|
|
var res = _webhooks.ListByUserId(userId, page);
|
2023-03-11 10:43:06 -08:00
|
|
|
foreach (var item in res)
|
|
|
|
{
|
|
|
|
items.Add(DiscordWebHookDto.Convert(item));
|
|
|
|
}
|
2023-07-23 22:54:00 -07:00
|
|
|
|
|
|
|
return new OkObjectResult(items);
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost(Name = "New")]
|
2023-07-29 09:44:15 -07:00
|
|
|
public ActionResult<DiscordWebhookResult> New([FromBody] NewDiscordWebhookRequest request)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-07-29 09:44:15 -07:00
|
|
|
var userId = HttpContext.GetUserId();
|
|
|
|
|
|
|
|
var exists = _webhooks.GetByUrl(request.Url ?? "");
|
2023-06-23 21:55:56 -07:00
|
|
|
if (exists.Id != Guid.Empty)
|
2023-03-11 10:43:06 -08:00
|
|
|
{
|
2023-07-29 09:44:15 -07:00
|
|
|
return new BadRequestObjectResult(new DiscordWebhookResult
|
|
|
|
{
|
|
|
|
IsSuccessful = true,
|
|
|
|
Items = new List<DiscordWebHookDto> { DiscordWebHookDto.Convert(exists) }
|
|
|
|
});
|
2023-03-11 10:43:06 -08:00
|
|
|
}
|
|
|
|
|
2023-06-23 21:55:56 -07:00
|
|
|
var res = _webhooks.New(new DiscordWebhookEntity
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-07-29 09:44:15 -07:00
|
|
|
UserId = userId,
|
|
|
|
Url = request.Url ?? "",
|
|
|
|
Server = request.Server ?? "",
|
|
|
|
Channel = request.Channel ?? "",
|
2023-02-26 09:40:04 -08:00
|
|
|
Enabled = true,
|
|
|
|
});
|
2023-03-11 10:43:06 -08:00
|
|
|
|
2023-07-29 09:44:15 -07:00
|
|
|
return new OkObjectResult(new DiscordWebhookResult
|
|
|
|
{
|
|
|
|
IsSuccessful = true,
|
|
|
|
Items = new List<DiscordWebHookDto>
|
|
|
|
{
|
|
|
|
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>();
|
2023-07-29 09:44:15 -07:00
|
|
|
var res = _webhooks.ListByServerAndChannel(HttpContext.GetUserId(), server, channel, 25);
|
2023-03-11 10:43:06 -08:00
|
|
|
|
|
|
|
foreach (var item in res)
|
|
|
|
{
|
|
|
|
items.Add(DiscordWebHookDto.Convert(item));
|
|
|
|
}
|
|
|
|
return items;
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
2023-07-29 09:44:15 -07:00
|
|
|
public DiscordWebHookDto GetById(string userId, Guid id)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-07-29 09:44:15 -07:00
|
|
|
var res = _webhooks.GetById(userId, id);
|
2023-03-11 10:43:06 -08:00
|
|
|
return DiscordWebHookDto.Convert(res);
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("{id}/disable")]
|
|
|
|
public void DisableById(Guid id)
|
|
|
|
{
|
2023-07-29 09:44:15 -07:00
|
|
|
_webhooks.Disable(HttpContext.GetUserId(), id);
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("{id}/enable")]
|
|
|
|
public void EnableById(Guid id)
|
|
|
|
{
|
2023-07-29 09:44:15 -07:00
|
|
|
_webhooks.Enable(HttpContext.GetUserId(), id);
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
}
|