Newsbot.Collector/Newsbot.Collector.Database/Repositories/DiscordNotificationTable.cs

97 lines
3.1 KiB
C#

using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Interfaces;
namespace Newsbot.Collector.Database.Repositories;
public class DiscordNotificationTable : IDiscordNotificationRepository
{
//private readonly string _connectionString;
private DatabaseContext _context;
public DiscordNotificationTable(string connectionString)
{
//_connectionString = connectionString;
_context = new DatabaseContext(connectionString);
}
public DiscordNotificationTable(DatabaseContext context)
{
_context = context;
}
public DiscordNotificationEntity New(DiscordNotificationEntity model)
{
model.Id = new Guid();
_context.DiscordNotification.Add(model);
_context.SaveChanges();
return model;
}
public List<DiscordNotificationEntity> List(string userId, int page = 0, int count = 25)
{
return _context.DiscordNotification
.Where(x => x.UserId != null && x.UserId.Equals(userId))
.Skip(page * count)
.Take(count)
.ToList();
}
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();
}
public List<DiscordNotificationEntity> ListBySourceId(Guid id, int page = 0, int count = 25)
{
return _context.DiscordNotification
.Where(f => f.SourceId.Equals(id))
.Skip(page * count)
.ToList();
}
public List<DiscordNotificationEntity> ListByWebhook(string userId, Guid id, int page = 0, int count = 25)
{
return _context.DiscordNotification
.Where(f => f.DiscordWebHookId.Equals(id))
.Where(f => f.UserId != null && f.UserId.Equals(userId))
.Skip(page * count)
.ToList();
}
public DiscordNotificationEntity GetById(string userId, Guid id)
{
var res = _context.DiscordNotification
.Where(f => f.UserId != null && f.UserId.Equals(userId))
.FirstOrDefault(f => f.Id.Equals(id));
return res ??= new DiscordNotificationEntity();
}
public DiscordNotificationEntity GetByWebhookAndSource(string userId, Guid webhookId, Guid sourceId)
{
var res = _context.DiscordNotification
.Where(f => f.UserId != null && f.UserId.Equals(userId))
.Where(f => f.DiscordWebHookId.Equals(webhookId))
.FirstOrDefault(f => f.SourceId.Equals(sourceId));
return res ??= new DiscordNotificationEntity();
}
public int Delete(string userId, Guid id)
{
var res = _context.DiscordNotification
.Where(f => f.UserId != null && f.UserId.Equals(userId))
.FirstOrDefault(f => f.Id.Equals(id));
if (res is null)
{
return -1;
}
_context.DiscordNotification.Remove(res);
return _context.SaveChanges();
}
}