2023-06-25 21:02:26 -07:00
|
|
|
using Newsbot.Collector.Domain.Entities;
|
2023-02-26 09:40:04 -08:00
|
|
|
using Newsbot.Collector.Domain.Interfaces;
|
2023-02-19 21:39:03 -08:00
|
|
|
|
|
|
|
namespace Newsbot.Collector.Database.Repositories;
|
|
|
|
|
2023-07-09 21:29:33 -07:00
|
|
|
public class DiscordNotificationTable : IDiscordNotificationRepository
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-07-06 22:21:53 -07:00
|
|
|
//private readonly string _connectionString;
|
|
|
|
private DatabaseContext _context;
|
2023-02-19 21:39:03 -08:00
|
|
|
|
2023-07-09 21:29:33 -07:00
|
|
|
public DiscordNotificationTable(string connectionString)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-07-06 22:21:53 -07:00
|
|
|
//_connectionString = connectionString;
|
|
|
|
_context = new DatabaseContext(connectionString);
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-07-09 21:29:33 -07:00
|
|
|
public DiscordNotificationTable(DatabaseContext context)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-07-06 22:21:53 -07:00
|
|
|
_context = context;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-07-09 21:29:33 -07:00
|
|
|
public DiscordNotificationEntity New(DiscordNotificationEntity model)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-06-25 21:02:26 -07:00
|
|
|
model.Id = new Guid();
|
2023-07-23 22:57:35 -07:00
|
|
|
|
2023-07-09 21:29:33 -07:00
|
|
|
_context.DiscordNotification.Add(model);
|
2023-07-06 22:21:53 -07:00
|
|
|
_context.SaveChanges();
|
2023-07-23 22:57:35 -07:00
|
|
|
|
2023-06-25 21:02:26 -07:00
|
|
|
return model;
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-07-23 22:57:35 -07:00
|
|
|
public List<DiscordNotificationEntity> List(string userId, int page = 0, int count = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-07-23 22:57:35 -07:00
|
|
|
return _context.DiscordNotification
|
|
|
|
.Where(x => x.UserId != null && x.UserId.Equals(userId))
|
|
|
|
.Skip(page * count)
|
|
|
|
.Take(count)
|
|
|
|
.ToList();
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-07-23 22:57:35 -07:00
|
|
|
public List<DiscordNotificationEntity> ListBySourceId(string userId, Guid id, int page = 0, int count = 25)
|
|
|
|
{
|
|
|
|
return _context.DiscordNotification
|
|
|
|
.Where(f => f.SourceId.Equals(id))
|
|
|
|
.Where(f => f.UserId != null && f.UserId.Equals(userId))
|
|
|
|
.Skip(page * count)
|
|
|
|
.ToList();
|
|
|
|
}
|
|
|
|
|
2023-07-09 21:29:33 -07:00
|
|
|
public List<DiscordNotificationEntity> ListBySourceId(Guid id, int page = 0, int count = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-07-23 22:57:35 -07:00
|
|
|
return _context.DiscordNotification
|
|
|
|
.Where(f => f.SourceId.Equals(id))
|
2023-06-25 21:02:26 -07:00
|
|
|
.Skip(page * count)
|
|
|
|
.ToList();
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-07-23 22:57:35 -07:00
|
|
|
public List<DiscordNotificationEntity> ListByWebhook(string userId, Guid id, int page = 0, int count = 25)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-07-23 22:57:35 -07:00
|
|
|
return _context.DiscordNotification
|
|
|
|
.Where(f => f.DiscordWebHookId.Equals(id))
|
|
|
|
.Where(f => f.UserId != null && f.UserId.Equals(userId))
|
|
|
|
.Skip(page * count)
|
|
|
|
.ToList();
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-07-23 22:57:35 -07:00
|
|
|
public DiscordNotificationEntity GetById(string userId, Guid id)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-07-09 21:29:33 -07:00
|
|
|
var res = _context.DiscordNotification
|
2023-07-23 22:57:35 -07:00
|
|
|
.Where(f => f.UserId != null && f.UserId.Equals(userId))
|
2023-06-25 21:02:26 -07:00
|
|
|
.FirstOrDefault(f => f.Id.Equals(id));
|
2023-07-09 21:29:33 -07:00
|
|
|
return res ??= new DiscordNotificationEntity();
|
2023-02-26 09:40:04 -08:00
|
|
|
}
|
|
|
|
|
2023-07-23 22:57:35 -07:00
|
|
|
public DiscordNotificationEntity GetByWebhookAndSource(string userId, Guid webhookId, Guid sourceId)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-07-09 21:29:33 -07:00
|
|
|
var res = _context.DiscordNotification
|
2023-07-23 22:57:35 -07:00
|
|
|
.Where(f => f.UserId != null && f.UserId.Equals(userId))
|
2023-06-25 21:02:26 -07:00
|
|
|
.Where(f => f.DiscordWebHookId.Equals(webhookId))
|
|
|
|
.FirstOrDefault(f => f.SourceId.Equals(sourceId));
|
2023-07-09 21:29:33 -07:00
|
|
|
return res ??= new DiscordNotificationEntity();
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
|
2023-07-23 22:57:35 -07:00
|
|
|
public int Delete(string userId, Guid id)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-07-23 22:57:35 -07:00
|
|
|
var res = _context.DiscordNotification
|
|
|
|
.Where(f => f.UserId != null && f.UserId.Equals(userId))
|
|
|
|
.FirstOrDefault(f => f.Id.Equals(id));
|
2023-06-25 21:02:26 -07:00
|
|
|
if (res is null)
|
2023-02-26 09:40:04 -08:00
|
|
|
{
|
2023-07-23 22:57:35 -07:00
|
|
|
return -1;
|
2023-06-25 21:02:26 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 21:29:33 -07:00
|
|
|
_context.DiscordNotification.Remove(res);
|
2023-07-23 22:57:35 -07:00
|
|
|
return _context.SaveChanges();
|
2023-02-19 21:39:03 -08:00
|
|
|
}
|
|
|
|
}
|