Migration to EF and starting to add Identity #11
@ -0,0 +1,130 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
@ -1,133 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using Newsbot.Collector.Database.Repositories;
|
|
||||||
using Newsbot.Collector.Domain.Dto;
|
|
||||||
using Newsbot.Collector.Domain.Entities;
|
|
||||||
using Newsbot.Collector.Domain.Interfaces;
|
|
||||||
using Newsbot.Collector.Domain.Models;
|
|
||||||
|
|
||||||
namespace Newsbot.Collector.Api.Controllers;
|
|
||||||
|
|
||||||
[ApiController]
|
|
||||||
[Route("api/subscriptions")]
|
|
||||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
||||||
public class SubscriptionsController : ControllerBase
|
|
||||||
{
|
|
||||||
private readonly ILogger<SubscriptionsController> _logger;
|
|
||||||
private readonly IDiscordWebHooksRepository _discord;
|
|
||||||
private readonly ISourcesRepository _sources;
|
|
||||||
private readonly ISubscriptionRepository _subscription;
|
|
||||||
|
|
||||||
public SubscriptionsController(ILogger<SubscriptionsController> logger, IDiscordWebHooksRepository discord, ISourcesRepository sources, ISubscriptionRepository subscription)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
_discord = discord;
|
|
||||||
_sources = sources;
|
|
||||||
_subscription = subscription;
|
|
||||||
}
|
|
||||||
|
|
||||||
[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/{id}")]
|
|
||||||
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/{id}")]
|
|
||||||
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 SubscriptionEntity
|
|
||||||
{
|
|
||||||
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 SubscriptionEntity
|
|
||||||
{
|
|
||||||
DiscordWebHookId = discordId,
|
|
||||||
SourceId = sourceId,
|
|
||||||
CodeAllowCommits = allowCommits,
|
|
||||||
CodeAllowReleases = allowReleases
|
|
||||||
});
|
|
||||||
|
|
||||||
return new SubscriptionDto();
|
|
||||||
}
|
|
||||||
}
|
|
@ -48,7 +48,7 @@ builder.Services.AddScoped<IDiscordQueueRepository, DiscordQueueTable>();
|
|||||||
builder.Services.AddScoped<IDiscordWebHooksRepository, DiscordWebhooksTable>();
|
builder.Services.AddScoped<IDiscordWebHooksRepository, DiscordWebhooksTable>();
|
||||||
builder.Services.AddScoped<IIconsRepository, IconsTable>();
|
builder.Services.AddScoped<IIconsRepository, IconsTable>();
|
||||||
builder.Services.AddScoped<ISourcesRepository, SourcesTable>();
|
builder.Services.AddScoped<ISourcesRepository, SourcesTable>();
|
||||||
builder.Services.AddScoped<ISubscriptionRepository, SubscriptionsTable>();
|
builder.Services.AddScoped<IDiscordNotificationRepository, DiscordNotificationTable>();
|
||||||
|
|
||||||
// Configure Identity
|
// Configure Identity
|
||||||
builder.Services.AddScoped<IIdentityService, IdentityService>();
|
builder.Services.AddScoped<IIdentityService, IdentityService>();
|
||||||
|
@ -10,11 +10,12 @@ namespace Newsbot.Collector.Database;
|
|||||||
public class DatabaseContext : IdentityDbContext
|
public class DatabaseContext : IdentityDbContext
|
||||||
{
|
{
|
||||||
public DbSet<ArticlesEntity> Articles { get; set; } = null!;
|
public DbSet<ArticlesEntity> Articles { get; set; } = null!;
|
||||||
|
|
||||||
|
public DbSet<DiscordNotificationEntity> DiscordNotification { get; set; } = null!;
|
||||||
public DbSet<DiscordQueueEntity> DiscordQueue { get; set; } = null!;
|
public DbSet<DiscordQueueEntity> DiscordQueue { get; set; } = null!;
|
||||||
public DbSet<DiscordWebhookEntity> DiscordWebhooks { get; set; } = null!;
|
public DbSet<DiscordWebhookEntity> DiscordWebhooks { get; set; } = null!;
|
||||||
public DbSet<IconEntity> Icons { get; set; } = null!;
|
public DbSet<IconEntity> Icons { get; set; } = null!;
|
||||||
public DbSet<SourceEntity> Sources { get; set; } = null!;
|
public DbSet<SourceEntity> Sources { get; set; } = null!;
|
||||||
public DbSet<SubscriptionEntity> Subscriptions { get; set; } = null!;
|
|
||||||
|
|
||||||
//public DbSet<UserEntity> Users { get; set; } = null!;
|
//public DbSet<UserEntity> Users { get; set; } = null!;
|
||||||
|
|
||||||
|
@ -8,78 +8,78 @@ using Npgsql;
|
|||||||
|
|
||||||
namespace Newsbot.Collector.Database.Repositories;
|
namespace Newsbot.Collector.Database.Repositories;
|
||||||
|
|
||||||
public class SubscriptionsTable : ISubscriptionRepository
|
public class DiscordNotificationTable : IDiscordNotificationRepository
|
||||||
{
|
{
|
||||||
//private readonly string _connectionString;
|
//private readonly string _connectionString;
|
||||||
private DatabaseContext _context;
|
private DatabaseContext _context;
|
||||||
|
|
||||||
public SubscriptionsTable(string connectionString)
|
public DiscordNotificationTable(string connectionString)
|
||||||
{
|
{
|
||||||
//_connectionString = connectionString;
|
//_connectionString = connectionString;
|
||||||
_context = new DatabaseContext(connectionString);
|
_context = new DatabaseContext(connectionString);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SubscriptionsTable(DatabaseContext context)
|
public DiscordNotificationTable(DatabaseContext context)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SubscriptionEntity New(SubscriptionEntity model)
|
public DiscordNotificationEntity New(DiscordNotificationEntity model)
|
||||||
{
|
{
|
||||||
model.Id = new Guid();
|
model.Id = new Guid();
|
||||||
//using var context = new DatabaseContext(_connectionString);
|
//using var context = new DatabaseContext(_connectionString);
|
||||||
_context.Subscriptions.Add(model);
|
_context.DiscordNotification.Add(model);
|
||||||
_context.SaveChanges();
|
_context.SaveChanges();
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SubscriptionEntity> List(int page = 0, int count = 25)
|
public List<DiscordNotificationEntity> List(int page = 0, int count = 25)
|
||||||
{
|
{
|
||||||
//using var context = new DatabaseContext(_connectionString);
|
//using var context = new DatabaseContext(_connectionString);
|
||||||
return _context.Subscriptions.Skip(page * count).Take(count).ToList();
|
return _context.DiscordNotification.Skip(page * count).Take(count).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SubscriptionEntity> ListBySourceId(Guid id, int page = 0, int count = 25)
|
public List<DiscordNotificationEntity> ListBySourceId(Guid id, int page = 0, int count = 25)
|
||||||
{
|
{
|
||||||
//using var context = new DatabaseContext(_connectionString);
|
//using var context = new DatabaseContext(_connectionString);
|
||||||
return _context.Subscriptions.Where(f => f.SourceId.Equals(id))
|
return _context.DiscordNotification.Where(f => f.SourceId.Equals(id))
|
||||||
.Skip(page * count)
|
.Skip(page * count)
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SubscriptionEntity> ListByWebhook(Guid id, int page = 0, int count = 25)
|
public List<DiscordNotificationEntity> ListByWebhook(Guid id, int page = 0, int count = 25)
|
||||||
{
|
{
|
||||||
//using var context = new DatabaseContext(_connectionString);
|
//using var context = new DatabaseContext(_connectionString);
|
||||||
return _context.Subscriptions.Where(f => f.DiscordWebHookId.Equals(id)).Skip(page * count).ToList();
|
return _context.DiscordNotification.Where(f => f.DiscordWebHookId.Equals(id)).Skip(page * count).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SubscriptionEntity GetById(Guid id)
|
public DiscordNotificationEntity GetById(Guid id)
|
||||||
{
|
{
|
||||||
//using var context = new DatabaseContext(_connectionString);
|
//using var context = new DatabaseContext(_connectionString);
|
||||||
var res = _context.Subscriptions
|
var res = _context.DiscordNotification
|
||||||
.FirstOrDefault(f => f.Id.Equals(id));
|
.FirstOrDefault(f => f.Id.Equals(id));
|
||||||
return res ??= new SubscriptionEntity();
|
return res ??= new DiscordNotificationEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SubscriptionEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId)
|
public DiscordNotificationEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId)
|
||||||
{
|
{
|
||||||
//using var context = new DatabaseContext(_connectionString);
|
//using var context = new DatabaseContext(_connectionString);
|
||||||
var res = _context.Subscriptions
|
var res = _context.DiscordNotification
|
||||||
.Where(f => f.DiscordWebHookId.Equals(webhookId))
|
.Where(f => f.DiscordWebHookId.Equals(webhookId))
|
||||||
.FirstOrDefault(f => f.SourceId.Equals(sourceId));
|
.FirstOrDefault(f => f.SourceId.Equals(sourceId));
|
||||||
return res ??= new SubscriptionEntity();
|
return res ??= new DiscordNotificationEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Delete(Guid id)
|
public void Delete(Guid id)
|
||||||
{
|
{
|
||||||
//using var context = new DatabaseContext(_connectionString);
|
//using var context = new DatabaseContext(_connectionString);
|
||||||
var res = _context.Subscriptions.FirstOrDefault(f => f.Id.Equals(id));
|
var res = _context.DiscordNotification.FirstOrDefault(f => f.Id.Equals(id));
|
||||||
if (res is null)
|
if (res is null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_context.Subscriptions.Remove(res);
|
_context.DiscordNotification.Remove(res);
|
||||||
_context.SaveChanges();
|
_context.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
@ -3,7 +3,7 @@ using Newsbot.Collector.Domain.Models;
|
|||||||
|
|
||||||
namespace Newsbot.Collector.Domain.Dto;
|
namespace Newsbot.Collector.Domain.Dto;
|
||||||
|
|
||||||
public class SubscriptionDetailsDto
|
public class DiscordNotificationDetailsDto
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public bool CodeAllowReleases { get; set; }
|
public bool CodeAllowReleases { get; set; }
|
||||||
@ -11,14 +11,14 @@ public class SubscriptionDetailsDto
|
|||||||
public SourceDto? Source { get; set; }
|
public SourceDto? Source { get; set; }
|
||||||
public DiscordWebHookDto? DiscordWebHook { get; set; }
|
public DiscordWebHookDto? DiscordWebHook { get; set; }
|
||||||
|
|
||||||
public static SubscriptionDetailsDto Convert(SubscriptionEntity subscription, SourceEntity source,
|
public static DiscordNotificationDetailsDto Convert(DiscordNotificationEntity discordNotification, SourceEntity source,
|
||||||
DiscordWebhookEntity discord)
|
DiscordWebhookEntity discord)
|
||||||
{
|
{
|
||||||
return new SubscriptionDetailsDto
|
return new DiscordNotificationDetailsDto
|
||||||
{
|
{
|
||||||
Id = subscription.Id,
|
Id = discordNotification.Id,
|
||||||
CodeAllowCommits = subscription.CodeAllowCommits,
|
CodeAllowCommits = discordNotification.CodeAllowCommits,
|
||||||
CodeAllowReleases = subscription.CodeAllowReleases,
|
CodeAllowReleases = discordNotification.CodeAllowReleases,
|
||||||
Source = SourceDto.Convert(source),
|
Source = SourceDto.Convert(source),
|
||||||
DiscordWebHook = DiscordWebHookDto.Convert(discord)
|
DiscordWebHook = DiscordWebHookDto.Convert(discord)
|
||||||
};
|
};
|
@ -3,7 +3,7 @@ using Newsbot.Collector.Domain.Models;
|
|||||||
|
|
||||||
namespace Newsbot.Collector.Domain.Dto;
|
namespace Newsbot.Collector.Domain.Dto;
|
||||||
|
|
||||||
public class SubscriptionDto
|
public class DiscordNotificationDto
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public Guid SourceId { get; set; }
|
public Guid SourceId { get; set; }
|
||||||
@ -11,9 +11,9 @@ public class SubscriptionDto
|
|||||||
public bool CodeAllowReleases { get; set; }
|
public bool CodeAllowReleases { get; set; }
|
||||||
public bool CodeAllowCommits { get; set; }
|
public bool CodeAllowCommits { get; set; }
|
||||||
|
|
||||||
public static SubscriptionDto Convert(SubscriptionEntity model)
|
public static DiscordNotificationDto Convert(DiscordNotificationEntity model)
|
||||||
{
|
{
|
||||||
return new SubscriptionDto
|
return new DiscordNotificationDto
|
||||||
{
|
{
|
||||||
Id = model.Id,
|
Id = model.Id,
|
||||||
SourceId = model.SourceId,
|
SourceId = model.SourceId,
|
@ -1,6 +1,6 @@
|
|||||||
namespace Newsbot.Collector.Domain.Entities;
|
namespace Newsbot.Collector.Domain.Entities;
|
||||||
|
|
||||||
public class SubscriptionEntity
|
public class DiscordNotificationEntity
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public bool CodeAllowReleases { get; set; }
|
public bool CodeAllowReleases { get; set; }
|
@ -0,0 +1,18 @@
|
|||||||
|
using Newsbot.Collector.Domain.Entities;
|
||||||
|
using Newsbot.Collector.Domain.Models;
|
||||||
|
|
||||||
|
namespace Newsbot.Collector.Domain.Interfaces;
|
||||||
|
|
||||||
|
public interface IDiscordNotificationRepository
|
||||||
|
{
|
||||||
|
DiscordNotificationEntity New(DiscordNotificationEntity model);
|
||||||
|
|
||||||
|
List<DiscordNotificationEntity> List(int page = 0, int count = 25);
|
||||||
|
List<DiscordNotificationEntity> ListBySourceId(Guid id, int page = 0, int count = 25);
|
||||||
|
List<DiscordNotificationEntity> ListByWebhook(Guid id, int page = 0, int count = 25);
|
||||||
|
|
||||||
|
DiscordNotificationEntity GetById(Guid id);
|
||||||
|
DiscordNotificationEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId);
|
||||||
|
|
||||||
|
void Delete(Guid id);
|
||||||
|
}
|
@ -1,18 +0,0 @@
|
|||||||
using Newsbot.Collector.Domain.Entities;
|
|
||||||
using Newsbot.Collector.Domain.Models;
|
|
||||||
|
|
||||||
namespace Newsbot.Collector.Domain.Interfaces;
|
|
||||||
|
|
||||||
public interface ISubscriptionRepository
|
|
||||||
{
|
|
||||||
SubscriptionEntity New(SubscriptionEntity model);
|
|
||||||
|
|
||||||
List<SubscriptionEntity> List(int page = 0, int count = 25);
|
|
||||||
List<SubscriptionEntity> ListBySourceId(Guid id, int page = 0, int count = 25);
|
|
||||||
List<SubscriptionEntity> ListByWebhook(Guid id, int page = 0, int count = 25);
|
|
||||||
|
|
||||||
SubscriptionEntity GetById(Guid id);
|
|
||||||
SubscriptionEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId);
|
|
||||||
|
|
||||||
void Delete(Guid id);
|
|
||||||
}
|
|
@ -1,3 +1,4 @@
|
|||||||
|
using Newsbot.Collector.Database;
|
||||||
using Newsbot.Collector.Database.Repositories;
|
using Newsbot.Collector.Database.Repositories;
|
||||||
using Newsbot.Collector.Domain.Entities;
|
using Newsbot.Collector.Domain.Entities;
|
||||||
using Newsbot.Collector.Domain.Interfaces;
|
using Newsbot.Collector.Domain.Interfaces;
|
||||||
@ -32,13 +33,13 @@ public class DiscordNotificationJobOptions
|
|||||||
public class DiscordNotificationJob
|
public class DiscordNotificationJob
|
||||||
{
|
{
|
||||||
private const string JobName = "DiscordNotifications";
|
private const string JobName = "DiscordNotifications";
|
||||||
|
private ILogger _logger;
|
||||||
|
//private DatabaseContext _databaseContext;
|
||||||
private IArticlesRepository _article;
|
private IArticlesRepository _article;
|
||||||
private IIconsRepository _icons;
|
private IIconsRepository _icons;
|
||||||
private ILogger _logger;
|
|
||||||
|
|
||||||
private IDiscordQueueRepository _queue;
|
private IDiscordQueueRepository _queue;
|
||||||
private ISourcesRepository _sources;
|
private ISourcesRepository _sources;
|
||||||
private ISubscriptionRepository _subs;
|
private IDiscordNotificationRepository _subs;
|
||||||
private IDiscordWebHooksRepository _webhook;
|
private IDiscordWebHooksRepository _webhook;
|
||||||
|
|
||||||
public DiscordNotificationJob()
|
public DiscordNotificationJob()
|
||||||
@ -47,18 +48,19 @@ public class DiscordNotificationJob
|
|||||||
_article = new ArticlesTable("");
|
_article = new ArticlesTable("");
|
||||||
_webhook = new DiscordWebhooksTable("");
|
_webhook = new DiscordWebhooksTable("");
|
||||||
_sources = new SourcesTable("");
|
_sources = new SourcesTable("");
|
||||||
_subs = new SubscriptionsTable("");
|
_subs = new DiscordNotificationTable("");
|
||||||
_icons = new IconsTable("");
|
_icons = new IconsTable("");
|
||||||
_logger = JobLogger.GetLogger("", JobName);
|
_logger = JobLogger.GetLogger("", JobName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void InitAndExecute(DiscordNotificationJobOptions options)
|
public void InitAndExecute(DiscordNotificationJobOptions options)
|
||||||
{
|
{
|
||||||
|
//_databaseContext = new DatabaseContext(options.ConnectionString ?? "");
|
||||||
_queue = new DiscordQueueTable(options.ConnectionString ?? "");
|
_queue = new DiscordQueueTable(options.ConnectionString ?? "");
|
||||||
_article = new ArticlesTable(options.ConnectionString ?? "");
|
_article = new ArticlesTable(options.ConnectionString ?? "");
|
||||||
_webhook = new DiscordWebhooksTable(options.ConnectionString ?? "");
|
_webhook = new DiscordWebhooksTable(options.ConnectionString ?? "");
|
||||||
_sources = new SourcesTable(options.ConnectionString ?? "");
|
_sources = new SourcesTable(options.ConnectionString ?? "");
|
||||||
_subs = new SubscriptionsTable(options.ConnectionString ?? "");
|
_subs = new DiscordNotificationTable(options.ConnectionString ?? "");
|
||||||
_icons = new IconsTable(options.ConnectionString ?? "");
|
_icons = new IconsTable(options.ConnectionString ?? "");
|
||||||
|
|
||||||
_logger = JobLogger.GetLogger(options.OpenTelemetry ?? "", JobName);
|
_logger = JobLogger.GetLogger(options.OpenTelemetry ?? "", JobName);
|
||||||
@ -76,6 +78,7 @@ public class DiscordNotificationJob
|
|||||||
private void Execute()
|
private void Execute()
|
||||||
{
|
{
|
||||||
//collect all the new requests
|
//collect all the new requests
|
||||||
|
|
||||||
var requests = _queue.List(100);
|
var requests = _queue.List(100);
|
||||||
_logger.Debug($"{JobName} - Collected {requests.Count} items to send");
|
_logger.Debug($"{JobName} - Collected {requests.Count} items to send");
|
||||||
|
|
||||||
@ -121,7 +124,7 @@ public class DiscordNotificationJob
|
|||||||
_queue.Delete(request.Id);
|
_queue.Delete(request.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendSubscriptionNotification(Guid requestId, ArticlesEntity articleDetails, SourceEntity sourceDetails, IconEntity sourceIcon, SubscriptionEntity sub)
|
public void SendSubscriptionNotification(Guid requestId, ArticlesEntity articleDetails, SourceEntity sourceDetails, IconEntity sourceIcon, DiscordNotificationEntity sub)
|
||||||
{
|
{
|
||||||
// Check if the subscription code flags
|
// Check if the subscription code flags
|
||||||
// If the article is a code commit and the subscription does not want them, skip.
|
// If the article is a code commit and the subscription does not want them, skip.
|
||||||
@ -134,7 +137,7 @@ public class DiscordNotificationJob
|
|||||||
var discordDetails = _webhook.GetById(sub.DiscordWebHookId);
|
var discordDetails = _webhook.GetById(sub.DiscordWebHookId);
|
||||||
if (discordDetails.Enabled == false) return;
|
if (discordDetails.Enabled == false) return;
|
||||||
|
|
||||||
var client = new DiscordWebhookClient(discordDetails.Url);
|
var client = new DiscordClient(discordDetails.Url);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
client.SendMessage(GenerateDiscordMessage(sourceDetails, articleDetails, sourceIcon));
|
client.SendMessage(GenerateDiscordMessage(sourceDetails, articleDetails, sourceIcon));
|
||||||
|
@ -10,7 +10,7 @@ public class DiscordNotificationJobTest
|
|||||||
public void PostTestMessage()
|
public void PostTestMessage()
|
||||||
{
|
{
|
||||||
var uri = "";
|
var uri = "";
|
||||||
var webhookClient = new DiscordWebhookClient(uri);
|
var webhookClient = new DiscordClient(uri);
|
||||||
|
|
||||||
var client = new DiscordNotificationJob();
|
var client = new DiscordNotificationJob();
|
||||||
var msg = client.GenerateDiscordMessage(new SourceEntity
|
var msg = client.GenerateDiscordMessage(new SourceEntity
|
||||||
@ -78,7 +78,7 @@ public class DiscordNotificationJobTest
|
|||||||
Id = Guid.NewGuid(),
|
Id = Guid.NewGuid(),
|
||||||
FileName = "https://www.redditstatic.com/desktop2x/img/favicon/android-icon-192x192.png"
|
FileName = "https://www.redditstatic.com/desktop2x/img/favicon/android-icon-192x192.png"
|
||||||
},
|
},
|
||||||
new SubscriptionEntity
|
new DiscordNotificationEntity
|
||||||
{
|
{
|
||||||
CodeAllowCommits = false,
|
CodeAllowCommits = false,
|
||||||
CodeAllowReleases = true
|
CodeAllowReleases = true
|
||||||
|
16
Newsbot.Collector.Tests/Tables/DiscordQueueTableTests.cs
Normal file
16
Newsbot.Collector.Tests/Tables/DiscordQueueTableTests.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Newsbot.Collector.Database.Repositories;
|
||||||
|
using Newsbot.Collector.Domain.Consts;
|
||||||
|
|
||||||
|
namespace Newsbot.Collector.Tests.Tables;
|
||||||
|
|
||||||
|
public class DiscordQueueTableTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void TableListsData()
|
||||||
|
{
|
||||||
|
var str = TestHelper.LoadConfig().GetConnectionString("Database") ?? "";
|
||||||
|
var context = new DiscordQueueTable(str);
|
||||||
|
var results = context.List();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user