Add link stuff #8

Merged
jleicher merged 5 commits from add-link into master 2020-12-04 16:27:43 +01:00
3 changed files with 43 additions and 11 deletions
Showing only changes of commit 25af0ebf54 - Show all commits

View File

@ -1,4 +1,3 @@
using System.Threading.Tasks;
using HN.Domain; using HN.Domain;
using HN.Infrastructure; using HN.Infrastructure;
using MediatR; using MediatR;
@ -11,14 +10,6 @@ using Microsoft.Extensions.Hosting;
namespace Website namespace Website
{ {
public class LinkRepository : ILinkRepository
{
public Task AddAsync(Link link)
{
throw new System.NotImplementedException("link repository not implemented yet");
}
}
public class Startup public class Startup
{ {
public Startup(IConfiguration configuration) public Startup(IConfiguration configuration)
@ -32,9 +23,9 @@ namespace Website
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddDbContext<HNDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("Default"))); services.AddDbContext<HNDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("Default")));
services.AddSingleton<ILinkRepository, LinkRepository>(); services.AddScoped<ILinkRepository, LinkRepository>();
services.AddControllersWithViews();
services.AddMediatR(typeof(HN.Application.AddLinkCommand)); services.AddMediatR(typeof(HN.Application.AddLinkCommand));
services.AddControllersWithViews();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -0,0 +1,17 @@
using System.Threading.Tasks;
using HN.Domain;
namespace HN.Infrastructure
{
public sealed class LinkRepository : Repository<Link>, ILinkRepository
{
public LinkRepository(HNDbContext context) : base(context)
{
}
public async Task AddAsync(Link link)
{
await base.AddAsync(link);
}
}
}

View File

@ -0,0 +1,24 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace HN.Infrastructure
{
public abstract class Repository<TEntity> where TEntity : class
{
private readonly HNDbContext _context;
protected readonly DbSet<TEntity> Entries;
protected Repository(HNDbContext context)
{
_context = context;
Entries = _context.Set<TEntity>();
}
protected async Task AddAsync(params TEntity[] entities)
{
await Entries.AddRangeAsync(entities);
await _context.SaveChangesAsync();
}
}
}