67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
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<AuthorEntity> NewAsync(AuthorEntity entity)
|
|
{
|
|
entity.Id = Guid.NewGuid();
|
|
_context.Authors.Add(entity);
|
|
await _context.SaveChangesAsync();
|
|
return entity;
|
|
}
|
|
|
|
public async Task<AuthorEntity> 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<List<AuthorEntity>> ListBySourceIdAsync(Guid sourceId)
|
|
{
|
|
return await _context.Authors
|
|
.Where(s => s.SourceId.Equals(sourceId)).ToListAsync();
|
|
}
|
|
|
|
public async Task<int> TotalPostsAsync(Guid id)
|
|
{
|
|
return await _context.Authors.CountAsync(x => x.Id.Equals(id));
|
|
}
|
|
|
|
public async Task<AuthorEntity?> GetBySourceIdAndNameAsync(Guid sourceId, string name)
|
|
{
|
|
return await _context.Authors
|
|
.Where(s => s.SourceId.Equals(sourceId))
|
|
.FirstOrDefaultAsync(n => n.Name.Equals(name));
|
|
|
|
}
|
|
|
|
public async Task<AuthorEntity?> GetById(Guid id)
|
|
{
|
|
return await _context.Authors.FirstOrDefaultAsync(q => q.Id.Equals(id));
|
|
}
|
|
} |