SES-67 Add repostitories

This commit is contained in:
Karol Górzyński 2020-12-03 18:52:19 +01:00
parent d2d8bd8cab
commit e3bcf28f46
10 changed files with 208 additions and 0 deletions

View File

@ -0,0 +1,14 @@
using SessionCompanion.Database.Repositories.Base;
using SessionCompanion.Database.Tables;
using System;
using System.Collections.Generic;
using System.Text;
namespace SessionCompanion.Database.Repositories
{
public class AlignmentRepository : Repository<Alignment>, IRepository<Alignment>
{
public AlignmentRepository(ApplicationDbContext _dbContext) : base(_dbContext)
{ }
}
}

View File

@ -0,0 +1,14 @@
using SessionCompanion.Database.Repositories.Base;
using SessionCompanion.Database.Tables;
using System;
using System.Collections.Generic;
using System.Text;
namespace SessionCompanion.Database.Repositories
{
public class BackgroundRepository : Repository<Background>, IRepository<Background>
{
public BackgroundRepository(ApplicationDbContext _dbContext) : base(_dbContext)
{ }
}
}

View File

@ -0,0 +1,24 @@
using SessionCompanion.Database.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SessionCompanion.Database.Repositories.Base
{
public interface IRepository<T> where T : BaseEntity
{
IQueryable<T> Get();
IQueryable<T> Get(Expression<Func<T, bool>> expression);
Task<T> Get(int id);
Task Create(T entity);
Task Update(T entity);
void Delete(T entity);
Task<bool> Any(Expression<Func<T, bool>> expression);
Task<bool> Any(int id);
void Dispose();
Task Save();
}
}

View File

@ -0,0 +1,72 @@
using Microsoft.EntityFrameworkCore;
using SessionCompanion.Database.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SessionCompanion.Database.Repositories.Base
{
public class Repository<T> : IRepository<T> where T : BaseEntity
{
protected ApplicationDbContext RepositoryContext { get; set; }
public Repository(ApplicationDbContext repositoryContext)
{
this.RepositoryContext = repositoryContext;
}
public IQueryable<T> Get()
{
return this.RepositoryContext.Set<T>();
}
public IQueryable<T> Get(Expression<Func<T, bool>> expression)
{
return this.RepositoryContext.Set<T>().Where(expression);
}
public Task<bool> Any(Expression<Func<T, bool>> expression)
{
return RepositoryContext.Set<T>().AnyAsync(expression);
}
public Task<bool> Any(int id)
{
return RepositoryContext.Set<T>().AnyAsync(n => n.Id == id);
}
public async Task<T> Get(int id)
{
return await RepositoryContext.Set<T>().FindAsync(id);
}
public virtual Task Create(T entity)
{
return Task.FromResult(RepositoryContext.Set<T>().AddAsync(entity));
}
public virtual Task Update(T entity)
{
return Task.FromResult(RepositoryContext.Set<T>().Update(entity));
}
public virtual void Delete(T entity)
{
this.RepositoryContext.Set<T>().Remove(entity);
}
public virtual async Task Save()
{
await RepositoryContext.SaveChangesAsync();
}
public void Dispose()
{
RepositoryContext.Dispose();
}
}
}

View File

@ -0,0 +1,14 @@
using SessionCompanion.Database.Repositories.Base;
using SessionCompanion.Database.Tables;
using System;
using System.Collections.Generic;
using System.Text;
namespace SessionCompanion.Database.Repositories
{
public class BiographyRepository : Repository<Biography>, IRepository<Biography>
{
public BiographyRepository(ApplicationDbContext _dbContext) : base(_dbContext)
{ }
}
}

View File

@ -0,0 +1,14 @@
using SessionCompanion.Database.Repositories.Base;
using SessionCompanion.Database.Tables;
using System;
using System.Collections.Generic;
using System.Text;
namespace SessionCompanion.Database.Repositories
{
public class CharacterRepository : Repository<Character>, IRepository<Character>
{
public CharacterRepository(ApplicationDbContext _dbContext) : base(_dbContext)
{ }
}
}

View File

@ -0,0 +1,14 @@
using SessionCompanion.Database.Repositories.Base;
using SessionCompanion.Database.Tables;
using System;
using System.Collections.Generic;
using System.Text;
namespace SessionCompanion.Database.Repositories
{
public class ClassRepository : Repository<Class>, IRepository<Class>
{
public ClassRepository(ApplicationDbContext _dbContext) : base(_dbContext)
{ }
}
}

View File

@ -0,0 +1,14 @@
using SessionCompanion.Database.Repositories.Base;
using SessionCompanion.Database.Tables;
using System;
using System.Collections.Generic;
using System.Text;
namespace SessionCompanion.Database.Repositories
{
public class RaceRepository : Repository<Race>, IRepository<Race>
{
public RaceRepository(ApplicationDbContext _dbContext) : base(_dbContext)
{ }
}
}

View File

@ -0,0 +1,14 @@
using SessionCompanion.Database.Repositories.Base;
using SessionCompanion.Database.Tables;
using System;
using System.Collections.Generic;
using System.Text;
namespace SessionCompanion.Database.Repositories
{
public class StatisticsRepository : Repository<Statistics>, IRepository<Statistics>
{
public StatisticsRepository(ApplicationDbContext _dbContext) : base(_dbContext)
{ }
}
}

View File

@ -0,0 +1,14 @@
using SessionCompanion.Database.Repositories.Base;
using SessionCompanion.Database.Tables;
using System;
using System.Collections.Generic;
using System.Text;
namespace SessionCompanion.Database.Repositories
{
public class UserRepository : Repository<User>, IRepository<User>
{
public UserRepository(ApplicationDbContext _dbContext) : base(_dbContext)
{ }
}
}