Merge pull request 'SES-67' (#6) from SES-67 into master

Reviewed-on: #6
This commit is contained in:
Łukasz Góreczny 2020-12-03 22:35:20 +01:00
commit 422d794d79
12 changed files with 239 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)
{ }
}
}

View File

@ -0,0 +1,28 @@
using Microsoft.Extensions.DependencyInjection;
using SessionCompanion.Database.Repositories;
using SessionCompanion.Database.Repositories.Base;
using SessionCompanion.Database.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SessionCompanion.Configurations
{
public static class RepositoryConfiguration
{
public static IServiceCollection AddRepositories(this IServiceCollection services)
{
services.AddScoped<IRepository<Alignment>, AlignmentRepository>();
services.AddScoped<IRepository<Background>, BackgroundRepository>();
services.AddScoped<IRepository<Biography>, BiographyRepository>();
services.AddScoped<IRepository<Character>, CharacterRepository>();
services.AddScoped<IRepository<Class>, ClassRepository>();
services.AddScoped<IRepository<Race>, RaceRepository>();
services.AddScoped<IRepository<Statistics>, StatisticsRepository>();
services.AddScoped<IRepository<User>, UserRepository>();
return services;
}
}
}

View File

@ -6,6 +6,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SessionCompanion.Configurations;
using SessionCompanion.Database;
namespace SessionCompanion
@ -27,6 +28,8 @@ namespace SessionCompanion
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddRepositories();
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{