130 lines
4.7 KiB
C#
130 lines
4.7 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newsbot.Collector.Domain.Dto;
|
|
using Newsbot.Collector.Domain.Entities;
|
|
using Newsbot.Collector.Domain.Interfaces;
|
|
|
|
namespace Newsbot.Collector.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/subscriptions")]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
public class DiscordNotificationController : ControllerBase
|
|
{
|
|
private readonly ILogger<DiscordNotificationController> _logger;
|
|
private readonly IDiscordWebHooksRepository _discord;
|
|
private readonly ISourcesRepository _sources;
|
|
private readonly IDiscordNotificationRepository _discordNotification;
|
|
|
|
public DiscordNotificationController(ILogger<DiscordNotificationController> logger, IDiscordWebHooksRepository discord, ISourcesRepository sources, IDiscordNotificationRepository discordNotification)
|
|
{
|
|
_logger = logger;
|
|
_discord = discord;
|
|
_sources = sources;
|
|
_discordNotification = discordNotification;
|
|
}
|
|
|
|
[HttpGet(Name = "ListSubscriptions")]
|
|
public IEnumerable<DiscordNotificationDto> List(int page)
|
|
{
|
|
var res = new List<DiscordNotificationDto>();
|
|
var items = _discordNotification.List(page);
|
|
foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item));
|
|
return res;
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public DiscordNotificationDto GetById(Guid id)
|
|
{
|
|
return DiscordNotificationDto.Convert(_discordNotification.GetById(id));
|
|
}
|
|
|
|
[HttpGet("{id}/details")]
|
|
public DiscordNotificationDetailsDto GetDetailsById(Guid id)
|
|
{
|
|
var sub = _discordNotification.GetById(id);
|
|
var webhook = _discord.GetById(sub.DiscordWebHookId);
|
|
var source = _sources.GetById(sub.SourceId);
|
|
|
|
return DiscordNotificationDetailsDto.Convert(sub, source, webhook);
|
|
}
|
|
|
|
[HttpPost("{id}/delete")]
|
|
public void DeleteById(Guid id)
|
|
{
|
|
_discordNotification.Delete(id);
|
|
}
|
|
|
|
[HttpGet("by/discordId/{id}")]
|
|
public IEnumerable<DiscordNotificationDto> GetByDiscordId(Guid id)
|
|
{
|
|
var res = new List<DiscordNotificationDto>();
|
|
var items = _discordNotification.ListByWebhook(id);
|
|
foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item));
|
|
return res;
|
|
}
|
|
|
|
[HttpGet("by/sourceId/{id}")]
|
|
public IEnumerable<DiscordNotificationDto> GetBySourceId(Guid id)
|
|
{
|
|
var res = new List<DiscordNotificationDto>();
|
|
var items = _discordNotification.ListBySourceId(id);
|
|
foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item));
|
|
return res;
|
|
}
|
|
|
|
[HttpPost(Name = "New Subscription")]
|
|
public ActionResult<DiscordNotificationDto> New(Guid sourceId, Guid discordId)
|
|
{
|
|
if (sourceId == Guid.Empty) return new BadRequestResult();
|
|
if (discordId == Guid.Empty) return new BadRequestResult();
|
|
|
|
var exists = _discordNotification.GetByWebhookAndSource(discordId, sourceId);
|
|
if (exists.Id != Guid.Empty) return DiscordNotificationDto.Convert(exists);
|
|
|
|
var discord = _discord.GetById(discordId);
|
|
if (discord.Id == Guid.Empty) return new BadRequestResult();
|
|
|
|
var source = _sources.GetById(sourceId);
|
|
if (source.Id == Guid.Empty) return new BadRequestResult();
|
|
|
|
var item = _discordNotification.New(new DiscordNotificationEntity
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
SourceId = sourceId,
|
|
DiscordWebHookId = discordId,
|
|
CodeAllowCommits = false,
|
|
CodeAllowReleases = false
|
|
});
|
|
|
|
return DiscordNotificationDto.Convert(item);
|
|
}
|
|
|
|
[HttpPost("new/codeproject")]
|
|
public ActionResult<DiscordNotificationDto> NewCodeProjectSubscription(Guid sourceId, Guid discordId, bool allowReleases,
|
|
bool allowCommits)
|
|
{
|
|
if (sourceId == Guid.Empty) return new BadRequestResult();
|
|
if (discordId == Guid.Empty) return new BadRequestResult();
|
|
|
|
var exists = _discordNotification.GetByWebhookAndSource(discordId, sourceId);
|
|
if (exists.Id != Guid.Empty) return DiscordNotificationDto.Convert(exists);
|
|
|
|
var discord = _discord.GetById(discordId);
|
|
if (discord.Id == Guid.Empty) return new BadRequestResult();
|
|
|
|
var source = _sources.GetById(sourceId);
|
|
if (source.Id == Guid.Empty) return new BadRequestResult();
|
|
|
|
var sub = _discordNotification.New(new DiscordNotificationEntity
|
|
{
|
|
DiscordWebHookId = discordId,
|
|
SourceId = sourceId,
|
|
CodeAllowCommits = allowCommits,
|
|
CodeAllowReleases = allowReleases
|
|
});
|
|
|
|
return new DiscordNotificationDto();
|
|
}
|
|
} |