Newsbot.Collector/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs

154 lines
4.5 KiB
C#
Raw Normal View History

using System.Data;
using Dapper;
2023-07-23 22:58:25 -07:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newsbot.Collector.Domain.Interfaces;
2023-06-23 21:55:56 -07:00
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;
}
2023-06-23 21:55:56 -07:00
public DiscordWebhookEntity New(DiscordWebhookEntity model)
{
2023-06-23 21:55:56 -07:00
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));
2023-06-23 21:55:56 -07:00
res ??= new DiscordWebhookEntity();
return res;
}
2023-06-23 21:55:56 -07:00
public DiscordWebhookEntity GetByUrl(string url)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordWebhooks.FirstOrDefault(d => d.Url.Equals(url));
2023-06-23 21:55:56 -07:00
res ??= new DiscordWebhookEntity();
return res;
}
2023-06-23 21:55:56 -07:00
public List<DiscordWebhookEntity> List(int page, int count = 25)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordWebhooks
2023-06-23 21:55:56 -07:00
.Skip(page * count)
.Take(count)
.ToList();
res ??= new List<DiscordWebhookEntity>();
return res;
}
2023-07-23 22:58:25 -07:00
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;
}
2023-06-23 21:55:56 -07:00
public List<DiscordWebhookEntity> ListByServer(string server, int limit = 25)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordWebhooks
2023-06-23 21:55:56 -07:00
.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))
2023-06-23 21:55:56 -07:00
.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);
2023-06-23 21:55:56 -07:00
res.Enabled = true;
_context.DiscordWebhooks.Update(res);
2023-06-23 21:55:56 -07:00
try
{
_context.SaveChanges();
2023-06-23 21:55:56 -07:00
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);
2023-06-23 21:55:56 -07:00
res.Enabled = false;
_context.DiscordWebhooks.Update(res);
2023-06-23 21:55:56 -07:00
try
{
_context.SaveChanges();
2023-06-23 21:55:56 -07:00
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;
//}
}