2023-02-26 09:40:04 -08:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using Newsbot.Collector.Database.Repositories;
|
|
|
|
using Newsbot.Collector.Domain.Dto;
|
|
|
|
using Newsbot.Collector.Domain.Interfaces;
|
|
|
|
using Newsbot.Collector.Domain.Models;
|
|
|
|
|
|
|
|
namespace Newsbot.Collector.Api.Controllers;
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
[Route("api/subscriptions")]
|
|
|
|
public class SubscriptionsController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly IDiscordWebHooksRepository _discord;
|
2023-04-13 22:13:06 -07:00
|
|
|
private readonly ILogger<SubscriptionsController> _logger;
|
2023-02-26 09:40:04 -08:00
|
|
|
private readonly ISourcesRepository _sources;
|
2023-04-13 22:13:06 -07:00
|
|
|
private readonly ISubscriptionRepository _subscription;
|
|
|
|
|
|
|
|
public SubscriptionsController(ILogger<SubscriptionsController> logger, IOptions<ConnectionStrings> settings)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2023-03-11 10:43:06 -08:00
|
|
|
_subscription = new SubscriptionsTable(settings.Value.Database);
|
|
|
|
_discord = new DiscordWebhooksTable(settings.Value.Database);
|
|
|
|
_sources = new SourcesTable(settings.Value.Database);
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet(Name = "ListSubscriptions")]
|
|
|
|
public IEnumerable<SubscriptionDto> List(int page)
|
|
|
|
{
|
|
|
|
var res = new List<SubscriptionDto>();
|
|
|
|
var items = _subscription.List(page);
|
2023-04-13 22:13:06 -07:00
|
|
|
foreach (var item in items) res.Add(SubscriptionDto.Convert(item));
|
2023-02-26 09:40:04 -08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
public SubscriptionDto GetById(Guid id)
|
|
|
|
{
|
|
|
|
return SubscriptionDto.Convert(_subscription.GetById(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{id}/details")]
|
|
|
|
public SubscriptionDetailsDto GetDetailsById(Guid id)
|
|
|
|
{
|
|
|
|
var sub = _subscription.GetById(id);
|
2023-04-13 22:13:06 -07:00
|
|
|
var webhook = _discord.GetByID(sub.DiscordWebHookId);
|
|
|
|
var source = _sources.GetByID(sub.SourceId);
|
2023-02-26 09:40:04 -08:00
|
|
|
|
|
|
|
return SubscriptionDetailsDto.Convert(sub, source, webhook);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("{id}/delete")]
|
|
|
|
public void DeleteById(Guid id)
|
|
|
|
{
|
|
|
|
_subscription.Delete(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("by/discordid")]
|
|
|
|
public IEnumerable<SubscriptionDto> GetByDiscordId(Guid id)
|
|
|
|
{
|
|
|
|
var res = new List<SubscriptionDto>();
|
|
|
|
var items = _subscription.ListByWebhook(id);
|
2023-04-13 22:13:06 -07:00
|
|
|
foreach (var item in items) res.Add(SubscriptionDto.Convert(item));
|
2023-02-26 09:40:04 -08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-03-11 10:43:06 -08:00
|
|
|
[HttpGet("by/sourceId")]
|
2023-02-26 09:40:04 -08:00
|
|
|
public IEnumerable<SubscriptionDto> GetBySourceId(Guid id)
|
|
|
|
{
|
|
|
|
var res = new List<SubscriptionDto>();
|
|
|
|
var items = _subscription.ListBySourceID(id);
|
2023-04-13 22:13:06 -07:00
|
|
|
foreach (var item in items) res.Add(SubscriptionDto.Convert(item));
|
2023-02-26 09:40:04 -08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-03-11 10:43:06 -08:00
|
|
|
[HttpPost(Name = "New Subscription")]
|
2023-04-13 22:13:06 -07:00
|
|
|
public ActionResult<SubscriptionDto> New(Guid sourceId, Guid discordId)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-04-13 22:13:06 -07:00
|
|
|
if (sourceId == Guid.Empty) return new BadRequestResult();
|
|
|
|
if (discordId == Guid.Empty) return new BadRequestResult();
|
|
|
|
|
2023-03-11 10:43:06 -08:00
|
|
|
var exists = _subscription.GetByWebhookAndSource(discordId, sourceId);
|
2023-04-13 22:13:06 -07:00
|
|
|
if (exists.Id != Guid.Empty) return SubscriptionDto.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();
|
2023-03-11 10:43:06 -08:00
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
var item = _subscription.New(new SubscriptionModel
|
|
|
|
{
|
2023-04-13 22:13:06 -07:00
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
SourceId = sourceId,
|
|
|
|
DiscordWebHookId = discordId,
|
|
|
|
CodeAllowCommits = false,
|
|
|
|
CodeAllowReleases = false
|
2023-02-26 09:40:04 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
return SubscriptionDto.Convert(item);
|
|
|
|
}
|
2023-04-13 22:13:06 -07:00
|
|
|
|
|
|
|
[HttpPost("new/codeproject")]
|
|
|
|
public ActionResult<SubscriptionDto> 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 = _subscription.GetByWebhookAndSource(discordId, sourceId);
|
|
|
|
if (exists.Id != Guid.Empty) return SubscriptionDto.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 = _subscription.New(new SubscriptionModel
|
|
|
|
{
|
|
|
|
DiscordWebHookId = discordId,
|
|
|
|
SourceId = sourceId,
|
|
|
|
CodeAllowCommits = allowCommits,
|
|
|
|
CodeAllowReleases = allowReleases
|
|
|
|
});
|
|
|
|
|
|
|
|
return new SubscriptionDto();
|
|
|
|
}
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|