Newsbot.Collector/Newsbot.Collector.Api/Controllers/SubscriptionsController.cs
James Tombleson 84b4137bdd
Features/codeproject/subscription options (#28)
* Updated migrations to add new columns to subscriptions

* repos updated with new columns

* dto updated with new columns

* subscription model was updated

* DiscordNotificationJob.cs was updated to reflect subscription options

* updated seed for codeproject subscriptions
2023-04-13 22:13:06 -07:00

129 lines
4.3 KiB
C#

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;
private readonly ILogger<SubscriptionsController> _logger;
private readonly ISourcesRepository _sources;
private readonly ISubscriptionRepository _subscription;
public SubscriptionsController(ILogger<SubscriptionsController> logger, IOptions<ConnectionStrings> settings)
{
_logger = logger;
_subscription = new SubscriptionsTable(settings.Value.Database);
_discord = new DiscordWebhooksTable(settings.Value.Database);
_sources = new SourcesTable(settings.Value.Database);
}
[HttpGet(Name = "ListSubscriptions")]
public IEnumerable<SubscriptionDto> List(int page)
{
var res = new List<SubscriptionDto>();
var items = _subscription.List(page);
foreach (var item in items) res.Add(SubscriptionDto.Convert(item));
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);
var webhook = _discord.GetByID(sub.DiscordWebHookId);
var source = _sources.GetByID(sub.SourceId);
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);
foreach (var item in items) res.Add(SubscriptionDto.Convert(item));
return res;
}
[HttpGet("by/sourceId")]
public IEnumerable<SubscriptionDto> GetBySourceId(Guid id)
{
var res = new List<SubscriptionDto>();
var items = _subscription.ListBySourceID(id);
foreach (var item in items) res.Add(SubscriptionDto.Convert(item));
return res;
}
[HttpPost(Name = "New Subscription")]
public ActionResult<SubscriptionDto> New(Guid sourceId, Guid discordId)
{
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 item = _subscription.New(new SubscriptionModel
{
Id = Guid.NewGuid(),
SourceId = sourceId,
DiscordWebHookId = discordId,
CodeAllowCommits = false,
CodeAllowReleases = false
});
return SubscriptionDto.Convert(item);
}
[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();
}
}