James Tombleson
82dea60126
All checks were successful
continuous-integration/drone/pr Build is passing
114 lines
3.5 KiB
C#
114 lines
3.5 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Options;
|
|
using Newsbot.Collector.Api.Middleware;
|
|
using Newsbot.Collector.Domain.Dto;
|
|
using Newsbot.Collector.Domain.Entities;
|
|
using Newsbot.Collector.Domain.Interfaces;
|
|
using Newsbot.Collector.Domain.Models;
|
|
using Newsbot.Collector.Domain.Requests;
|
|
using Newsbot.Collector.Domain.Results;
|
|
|
|
namespace Newsbot.Collector.Api.Controllers.v1;
|
|
|
|
[ApiController]
|
|
[Route("api/v1/discord/webhooks")]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
public class DiscordWebHookController : ControllerBase
|
|
{
|
|
private readonly ILogger<DiscordWebHookController> _logger;
|
|
private readonly IDiscordWebHooksRepository _webhooks;
|
|
|
|
public DiscordWebHookController(ILogger<DiscordWebHookController> logger, IOptions<ConnectionStrings> settings, IDiscordWebHooksRepository webhooks)
|
|
{
|
|
_logger = logger;
|
|
_webhooks = webhooks;
|
|
}
|
|
|
|
[HttpGet(Name = "GetDiscordWebhooks")]
|
|
public ActionResult<IEnumerable<DiscordWebHookDto>> Get(int page)
|
|
{
|
|
var userId = HttpContext.GetUserId();
|
|
if (userId.Equals(string.Empty))
|
|
{
|
|
_logger.LogWarning("Unable to find the user ID in the JWD Token");
|
|
return new BadRequestResult();
|
|
}
|
|
|
|
var items = new List<DiscordWebHookDto>();
|
|
var res = _webhooks.ListByUserId(userId, page);
|
|
foreach (var item in res)
|
|
{
|
|
items.Add(DiscordWebHookDto.Convert(item));
|
|
}
|
|
|
|
return new OkObjectResult(items);
|
|
}
|
|
|
|
[HttpPost(Name = "New")]
|
|
public ActionResult<DiscordWebhookResult> New([FromBody] NewDiscordWebhookRequest request)
|
|
{
|
|
var userId = HttpContext.GetUserId();
|
|
|
|
var exists = _webhooks.GetByUrl(request.Url ?? "");
|
|
if (exists.Id != Guid.Empty)
|
|
{
|
|
return new BadRequestObjectResult(new DiscordWebhookResult
|
|
{
|
|
IsSuccessful = true,
|
|
Items = new List<DiscordWebHookDto> { DiscordWebHookDto.Convert(exists) }
|
|
});
|
|
}
|
|
|
|
var res = _webhooks.New(new DiscordWebhookEntity
|
|
{
|
|
UserId = userId,
|
|
Url = request.Url ?? "",
|
|
Server = request.Server ?? "",
|
|
Channel = request.Channel ?? "",
|
|
Enabled = true,
|
|
});
|
|
|
|
return new OkObjectResult(new DiscordWebhookResult
|
|
{
|
|
IsSuccessful = true,
|
|
Items = new List<DiscordWebHookDto>
|
|
{
|
|
DiscordWebHookDto.Convert(res)
|
|
}
|
|
});
|
|
}
|
|
|
|
[HttpGet("by/serverAndChannel")]
|
|
public IEnumerable<DiscordWebHookDto> GetByServerAndChannel(string server, string channel)
|
|
{
|
|
var items = new List<DiscordWebHookDto>();
|
|
var res = _webhooks.ListByServerAndChannel(HttpContext.GetUserId(), server, channel, 25);
|
|
|
|
foreach (var item in res)
|
|
{
|
|
items.Add(DiscordWebHookDto.Convert(item));
|
|
}
|
|
return items;
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public DiscordWebHookDto GetById(string userId, Guid id)
|
|
{
|
|
var res = _webhooks.GetById(userId, id);
|
|
return DiscordWebHookDto.Convert(res);
|
|
}
|
|
|
|
[HttpPost("{id}/disable")]
|
|
public void DisableById(Guid id)
|
|
{
|
|
_webhooks.Disable(HttpContext.GetUserId(), id);
|
|
}
|
|
|
|
[HttpPost("{id}/enable")]
|
|
public void EnableById(Guid id)
|
|
{
|
|
_webhooks.Enable(HttpContext.GetUserId(), id);
|
|
}
|
|
} |