using Microsoft.EntityFrameworkCore; 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 async Task NewAsync(AuthorEntity entity) { entity.Id = Guid.NewGuid(); _context.Authors.Add(entity); await _context.SaveChangesAsync(); return entity; } public async Task CreateIfMissingAsync(AuthorEntity entity) { var res = await GetBySourceIdAndNameAsync(entity.SourceId, entity.Name); if (res is null) { entity.Id = Guid.NewGuid(); _context.Authors.Add(entity); await _context.SaveChangesAsync(); return entity; } return res; } public async Task> ListBySourceIdAsync(Guid sourceId) { return await _context.Authors .Where(s => s.SourceId.Equals(sourceId)).ToListAsync(); } public async Task TotalPostsAsync(Guid id) { return await _context.Authors.CountAsync(x => x.Id.Equals(id)); } public async Task GetBySourceIdAndNameAsync(Guid sourceId, string name) { return await _context.Authors .Where(s => s.SourceId.Equals(sourceId)) .FirstOrDefaultAsync(n => n.Name.Equals(name)); } public async Task GetById(Guid id) { return await _context.Authors.FirstOrDefaultAsync(q => q.Id.Equals(id)); } }