Newsbot.Collector/Newsbot.Collector.Database/Repositories/AuthorsTable.cs

47 lines
1.2 KiB
C#
Raw Normal View History

using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Interfaces;
namespace Newsbot.Collector.Database.Repositories;
public class AuthorsTable : IAuthorTable
{
private readonly DatabaseContext _context;
public AuthorsTable(string connectionString)
{
//_connectionString = connectionString;
_context = new DatabaseContext(connectionString);
}
public AuthorsTable(DatabaseContext context)
{
_context = context;
}
public AuthorEntity New(AuthorEntity entity)
{
entity.Id = Guid.NewGuid();
_context.Authors.Add(entity);
_context.SaveChanges();
return entity;
}
public List<AuthorEntity> ListBySourceId(Guid sourceId)
{
return _context.Authors
.Where(s => s.SourceId.Equals(sourceId)).ToList();
}
public int TotalPosts(Guid id)
{
return _context.Authors.Count(x => x.Id.Equals(id));
}
public AuthorEntity? GetBySourceIdAndName(Guid sourceId, string name)
{
return _context.Authors
.Where(s => s.SourceId.Equals(sourceId))
.FirstOrDefault(n => n.Name.Equals(name));
}
}