Newsbot.Collector/Newsbot.Collector.Api/Controllers/v1/DiscordWebHooksController.cs

114 lines
3.5 KiB
C#
Raw Normal View History

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;
2023-06-23 21:55:56 -07:00
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 ?? "");
2023-06-23 21:55:56 -07:00
if (exists.Id != Guid.Empty)
{
return new BadRequestObjectResult(new DiscordWebhookResult
{
IsSuccessful = true,
Items = new List<DiscordWebHookDto> { DiscordWebHookDto.Convert(exists) }
});
}
2023-06-23 21:55:56 -07:00
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);
}
}