discord web hooks updated with ef
This commit is contained in:
parent
f2995ff156
commit
0b68261181
@ -3,6 +3,7 @@ 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;
|
||||
|
||||
@ -40,12 +41,12 @@ public class DiscordWebHookController : ControllerBase
|
||||
{
|
||||
var exists = _webhooks.GetByUrl(url);
|
||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
if (exists.ID != Guid.Empty)
|
||||
if (exists.Id != Guid.Empty)
|
||||
{
|
||||
return DiscordWebHookDto.Convert(exists);
|
||||
}
|
||||
|
||||
var res = _webhooks.New(new DiscordWebHookModel
|
||||
var res = _webhooks.New(new DiscordWebhookEntity
|
||||
{
|
||||
Url = url,
|
||||
Server = server,
|
||||
@ -72,7 +73,7 @@ public class DiscordWebHookController : ControllerBase
|
||||
[HttpGet("{id}")]
|
||||
public DiscordWebHookDto GetById(Guid id)
|
||||
{
|
||||
var res = _webhooks.GetByID(id);
|
||||
var res = _webhooks.GetById(id);
|
||||
return DiscordWebHookDto.Convert(res);
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@ using System.Data;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Newsbot.Collector.Domain.Interfaces;
|
||||
using Newsbot.Collector.Domain.Models;
|
||||
using Newsbot.Collector.Domain.Entities;
|
||||
using Npgsql;
|
||||
|
||||
namespace Newsbot.Collector.Database.Repositories;
|
||||
@ -22,103 +22,104 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
||||
_connectionString = connstr;
|
||||
}
|
||||
|
||||
public DiscordWebHookModel New(DiscordWebHookModel model)
|
||||
public DiscordWebhookEntity New(DiscordWebhookEntity model)
|
||||
{
|
||||
var uid = Guid.NewGuid();
|
||||
using var conn = OpenConnection(_connectionString);
|
||||
var query =
|
||||
"Insert Into DiscordWebHooks (ID, Url, Server, Channel, Enabled) Values (@id, @url, @server, @channel, @enabled);";
|
||||
conn.Execute(query, new
|
||||
{
|
||||
id = uid,
|
||||
url = model.Url,
|
||||
server = model.Server,
|
||||
channel = model.Channel,
|
||||
enabled = model.Enabled
|
||||
});
|
||||
model.ID = uid;
|
||||
model.Id = new Guid();
|
||||
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
context.DiscordWebhooks.Add(model);
|
||||
context.SaveChanges();
|
||||
return model;
|
||||
}
|
||||
|
||||
public DiscordWebHookModel GetByID(Guid id)
|
||||
public DiscordWebhookEntity GetById(Guid id)
|
||||
{
|
||||
using var conn = OpenConnection(_connectionString);
|
||||
var query = "Select * from DiscordWebHooks Where ID = @id LIMIT 1;";
|
||||
return conn.Query<DiscordWebHookModel>(query, new
|
||||
{
|
||||
id
|
||||
}).First();
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.DiscordWebhooks.FirstOrDefault(d => d.Id.Equals(id));
|
||||
res ??= new DiscordWebhookEntity();
|
||||
return res;
|
||||
}
|
||||
|
||||
public DiscordWebHookModel GetByUrl(string url)
|
||||
public DiscordWebhookEntity GetByUrl(string url)
|
||||
{
|
||||
using var conn = OpenConnection(_connectionString);
|
||||
var query = "Select * From DiscordWebHooks Where url = @url;";
|
||||
try
|
||||
{
|
||||
var res = conn.QueryFirst<DiscordWebHookModel>(query, new
|
||||
{
|
||||
url
|
||||
});
|
||||
return res;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new DiscordWebHookModel();
|
||||
}
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.DiscordWebhooks.FirstOrDefault(d => d.Url.Equals(url));
|
||||
res ??= new DiscordWebhookEntity();
|
||||
return res;
|
||||
}
|
||||
|
||||
public List<DiscordWebHookModel> List(int page, int count = 25)
|
||||
public List<DiscordWebhookEntity> List(int page, int count = 25)
|
||||
{
|
||||
using var conn = OpenConnection(_connectionString);
|
||||
var query = @"Select * From DiscordWebHooks
|
||||
Offset @offset Fetch Next @count Rows Only;";
|
||||
return conn.Query<DiscordWebHookModel>(query, new
|
||||
{
|
||||
offset = page * count, count
|
||||
}).ToList();
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.DiscordWebhooks
|
||||
.Skip(page * count)
|
||||
.Take(count)
|
||||
.ToList();
|
||||
res ??= new List<DiscordWebhookEntity>();
|
||||
return res;
|
||||
}
|
||||
|
||||
public List<DiscordWebHookModel> ListByServer(string server, int limit = 25)
|
||||
public List<DiscordWebhookEntity> ListByServer(string server, int limit = 25)
|
||||
{
|
||||
using var conn = OpenConnection(_connectionString);
|
||||
var query = "Select * From DiscordWebHooks Where Server = @id Limit @limit;";
|
||||
return conn.Query<DiscordWebHookModel>(query, new
|
||||
{
|
||||
server, limit
|
||||
}).ToList();
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.DiscordWebhooks
|
||||
.Where(d => d.Server.Equals(server))
|
||||
.Take(limit)
|
||||
.ToList();
|
||||
res ??= new List<DiscordWebhookEntity>();
|
||||
return res;
|
||||
}
|
||||
|
||||
public List<DiscordWebHookModel> ListByServerAndChannel(string server, string channel, int limit = 25)
|
||||
public List<DiscordWebhookEntity> ListByServerAndChannel(string server, string channel, int limit = 25)
|
||||
{
|
||||
using var conn = OpenConnection(_connectionString);
|
||||
var query = "SELECT * FROM DiscordWebHooks WHERE Server = @server and Channel = @channel Limit @limit;";
|
||||
return conn.Query<DiscordWebHookModel>(query, new
|
||||
{
|
||||
server,
|
||||
channel,
|
||||
limit
|
||||
}).ToList();
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
var res = context.DiscordWebhooks
|
||||
.Where(s => s.Server.Equals(server))
|
||||
.Where(c => c.Channel.Equals(channel))
|
||||
.Take(limit)
|
||||
.ToList();
|
||||
res ??= new List<DiscordWebhookEntity>();
|
||||
return res;
|
||||
}
|
||||
|
||||
public int Disable(Guid id)
|
||||
{
|
||||
using var conn = OpenConnection(_connectionString);
|
||||
var query = "Update discordwebhooks Set Enabled = FALSE where ID = @id;";
|
||||
return conn.Execute(query, new
|
||||
var res = GetById(id);
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
|
||||
res.Enabled = true;
|
||||
|
||||
context.DiscordWebhooks.Update(res);
|
||||
try
|
||||
{
|
||||
id
|
||||
});
|
||||
context.SaveChanges();
|
||||
return 1;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to update DiscordWebhook ID = {id}. {ex.Message}");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int Enable(Guid id)
|
||||
{
|
||||
using var conn = OpenConnection(_connectionString);
|
||||
var query = "Update discordwebhooks Set Enabled = TRUE where ID = @id;";
|
||||
return conn.Execute(query, new
|
||||
var res = GetById(id);
|
||||
using var context = new DatabaseContext(_connectionString);
|
||||
|
||||
res.Enabled = false;
|
||||
|
||||
context.DiscordWebhooks.Update(res);
|
||||
try
|
||||
{
|
||||
id
|
||||
});
|
||||
context.SaveChanges();
|
||||
return 1;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to update DiscordWebhook ID = {id}. {ex.Message}");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private IDbConnection OpenConnection(string connectionString)
|
||||
|
@ -1,4 +1,4 @@
|
||||
using Newsbot.Collector.Domain.Models;
|
||||
using Newsbot.Collector.Domain.Entities;
|
||||
|
||||
namespace Newsbot.Collector.Domain.Dto;
|
||||
|
||||
@ -10,11 +10,11 @@ public class DiscordWebHookDto
|
||||
public string? Channel { get; set; }
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
public static DiscordWebHookDto Convert(DiscordWebHookModel model)
|
||||
public static DiscordWebHookDto Convert(DiscordWebhookEntity model)
|
||||
{
|
||||
return new DiscordWebHookDto
|
||||
{
|
||||
ID = model.ID,
|
||||
ID = model.Id,
|
||||
Url = model.Url,
|
||||
Server = model.Server,
|
||||
Channel = model.Channel,
|
||||
|
@ -1,18 +1,17 @@
|
||||
using Microsoft.VisualBasic;
|
||||
using Newsbot.Collector.Domain.Models;
|
||||
using Newsbot.Collector.Domain.Entities;
|
||||
|
||||
namespace Newsbot.Collector.Domain.Interfaces;
|
||||
|
||||
public interface IDiscordWebHooksRepository
|
||||
{
|
||||
DiscordWebHookModel New(DiscordWebHookModel model);
|
||||
DiscordWebhookEntity New(DiscordWebhookEntity model);
|
||||
|
||||
DiscordWebHookModel GetByID(Guid id);
|
||||
DiscordWebHookModel GetByUrl(string url);
|
||||
DiscordWebhookEntity GetById(Guid id);
|
||||
DiscordWebhookEntity GetByUrl(string url);
|
||||
|
||||
List<DiscordWebHookModel> List(int page, int count = 25);
|
||||
List<DiscordWebHookModel> ListByServer(string server, int limit);
|
||||
List<DiscordWebHookModel> ListByServerAndChannel(string server, string channel, int limit);
|
||||
List<DiscordWebhookEntity> List(int page, int count = 25);
|
||||
List<DiscordWebhookEntity> ListByServer(string server, int limit);
|
||||
List<DiscordWebhookEntity> ListByServerAndChannel(string server, string channel, int limit);
|
||||
|
||||
int Disable(Guid id);
|
||||
int Enable(Guid id);
|
||||
|
@ -132,7 +132,7 @@ public class DiscordNotificationJob
|
||||
if (articleDetails.CodeIsRelease && !sub.CodeAllowReleases) throw new MessageTypeNotRequestedException("Message was a code release and was not requested by the subscription");
|
||||
|
||||
// find the discord webhooks we need to post to
|
||||
var discordDetails = _webhook.GetByID(sub.DiscordWebHookId);
|
||||
var discordDetails = _webhook.GetById(sub.DiscordWebHookId);
|
||||
if (discordDetails.Enabled == false) return;
|
||||
|
||||
var client = new DiscordWebhookClient(discordDetails.Url);
|
||||
|
Loading…
Reference in New Issue
Block a user