James Tombleson
82dea60126
All checks were successful
continuous-integration/drone/pr Build is passing
154 lines
4.5 KiB
C#
154 lines
4.5 KiB
C#
using System.Data;
|
|
using Dapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newsbot.Collector.Domain.Interfaces;
|
|
using Newsbot.Collector.Domain.Entities;
|
|
using Npgsql;
|
|
|
|
namespace Newsbot.Collector.Database.Repositories;
|
|
|
|
public class DiscordWebhooksTable : IDiscordWebHooksRepository
|
|
{
|
|
//private readonly string _connectionString;
|
|
private DatabaseContext _context;
|
|
|
|
public DiscordWebhooksTable(string connectionString)
|
|
{
|
|
//_connectionString = connectionString;
|
|
_context = new DatabaseContext(connectionString);
|
|
}
|
|
|
|
public DiscordWebhooksTable(DatabaseContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public DiscordWebhookEntity New(DiscordWebhookEntity model)
|
|
{
|
|
model.Id = new Guid();
|
|
|
|
//using var context = new DatabaseContext(_connectionString);
|
|
_context.DiscordWebhooks.Add(model);
|
|
_context.SaveChanges();
|
|
return model;
|
|
}
|
|
|
|
public DiscordWebhookEntity GetById(string userId, Guid id)
|
|
{
|
|
//using var context = new DatabaseContext(_connectionString);
|
|
var res = _context.DiscordWebhooks
|
|
.Where(i => i.UserId != null && i.UserId.Equals(userId))
|
|
.FirstOrDefault(d => d.Id.Equals(id));
|
|
res ??= new DiscordWebhookEntity();
|
|
return res;
|
|
}
|
|
|
|
public DiscordWebhookEntity GetById(Guid id)
|
|
{
|
|
var res = _context.DiscordWebhooks
|
|
.FirstOrDefault(d => d.Id.Equals(id));
|
|
res ??= new DiscordWebhookEntity();
|
|
return res;
|
|
}
|
|
|
|
public DiscordWebhookEntity GetByUrl(string url)
|
|
{
|
|
//using var context = new DatabaseContext(_connectionString);
|
|
var res = _context.DiscordWebhooks.FirstOrDefault(d => d.Url.Equals(url));
|
|
res ??= new DiscordWebhookEntity();
|
|
return res;
|
|
}
|
|
|
|
public List<DiscordWebhookEntity> List(int page, int count = 25)
|
|
{
|
|
//using var context = new DatabaseContext(_connectionString);
|
|
var res = _context.DiscordWebhooks
|
|
.Skip(page * count)
|
|
.Take(count)
|
|
.ToList();
|
|
res ??= new List<DiscordWebhookEntity>();
|
|
return res;
|
|
}
|
|
|
|
public List<DiscordWebhookEntity> ListByUserId(string userId, int page)
|
|
{
|
|
var query = _context.DiscordWebhooks
|
|
.Where(p => p.UserId != null && p.UserId.Equals(userId))
|
|
.Skip(page * 25)
|
|
.Take(25)
|
|
.ToList();
|
|
return query;
|
|
}
|
|
|
|
public List<DiscordWebhookEntity> ListByServer(string server, int limit = 25)
|
|
{
|
|
//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<DiscordWebhookEntity> ListByServerAndChannel(string userId, string server, string channel, int limit = 25)
|
|
{
|
|
//using var context = new DatabaseContext(_connectionString);
|
|
var res = _context.DiscordWebhooks
|
|
.Where(i => i.UserId != null && i.UserId.Equals(userId))
|
|
.Where(s => s.Server.Equals(server))
|
|
.Where(c => c.Channel.Equals(channel))
|
|
.Take(limit)
|
|
.ToList();
|
|
res ??= new List<DiscordWebhookEntity>();
|
|
return res;
|
|
}
|
|
|
|
public int Disable(string userId, Guid id)
|
|
{
|
|
var res = GetById(userId, id);
|
|
//using var context = new DatabaseContext(_connectionString);
|
|
|
|
res.Enabled = true;
|
|
|
|
_context.DiscordWebhooks.Update(res);
|
|
try
|
|
{
|
|
_context.SaveChanges();
|
|
return 1;
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
Console.WriteLine($"Failed to update DiscordWebhook ID = {id}. {ex.Message}");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public int Enable(string userId, Guid id)
|
|
{
|
|
var res = GetById(userId, id);
|
|
//using var context = new DatabaseContext(_connectionString);
|
|
|
|
res.Enabled = false;
|
|
|
|
_context.DiscordWebhooks.Update(res);
|
|
try
|
|
{
|
|
_context.SaveChanges();
|
|
return 1;
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
Console.WriteLine($"Failed to update DiscordWebhook ID = {id}. {ex.Message}");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
//private IDbConnection OpenConnection(string connectionString)
|
|
//{
|
|
// var conn = new NpgsqlConnection(_connectionString);
|
|
// conn.Open();
|
|
// return conn;
|
|
//}
|
|
} |