From e3bcf28f46029fd6e72d197cb5a23ee7128c8463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20G=C3=B3rzy=C5=84ski?= Date: Thu, 3 Dec 2020 18:52:19 +0100 Subject: [PATCH 1/2] SES-67 Add repostitories --- .../Repositories/AlignmentRepository.cs | 14 ++++ .../Repositories/BackgroundRepository.cs | 14 ++++ .../Repositories/Base/IRepository.cs | 24 +++++++ .../Repositories/Base/Repository.cs | 72 +++++++++++++++++++ .../Repositories/BiographyRepository.cs | 14 ++++ .../Repositories/CharacterRepository.cs | 14 ++++ .../Repositories/ClassRepository.cs | 14 ++++ .../Repositories/RaceRepository.cs | 14 ++++ .../Repositories/StatisticsRepository.cs | 14 ++++ .../Repositories/UserRepository.cs | 14 ++++ 10 files changed, 208 insertions(+) create mode 100644 SessionCompanion/SessionCompanion.Database/Repositories/AlignmentRepository.cs create mode 100644 SessionCompanion/SessionCompanion.Database/Repositories/BackgroundRepository.cs create mode 100644 SessionCompanion/SessionCompanion.Database/Repositories/Base/IRepository.cs create mode 100644 SessionCompanion/SessionCompanion.Database/Repositories/Base/Repository.cs create mode 100644 SessionCompanion/SessionCompanion.Database/Repositories/BiographyRepository.cs create mode 100644 SessionCompanion/SessionCompanion.Database/Repositories/CharacterRepository.cs create mode 100644 SessionCompanion/SessionCompanion.Database/Repositories/ClassRepository.cs create mode 100644 SessionCompanion/SessionCompanion.Database/Repositories/RaceRepository.cs create mode 100644 SessionCompanion/SessionCompanion.Database/Repositories/StatisticsRepository.cs create mode 100644 SessionCompanion/SessionCompanion.Database/Repositories/UserRepository.cs diff --git a/SessionCompanion/SessionCompanion.Database/Repositories/AlignmentRepository.cs b/SessionCompanion/SessionCompanion.Database/Repositories/AlignmentRepository.cs new file mode 100644 index 0000000..76d78d4 --- /dev/null +++ b/SessionCompanion/SessionCompanion.Database/Repositories/AlignmentRepository.cs @@ -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, IRepository + { + public AlignmentRepository(ApplicationDbContext _dbContext) : base(_dbContext) + { } + } +} diff --git a/SessionCompanion/SessionCompanion.Database/Repositories/BackgroundRepository.cs b/SessionCompanion/SessionCompanion.Database/Repositories/BackgroundRepository.cs new file mode 100644 index 0000000..bbafd17 --- /dev/null +++ b/SessionCompanion/SessionCompanion.Database/Repositories/BackgroundRepository.cs @@ -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, IRepository + { + public BackgroundRepository(ApplicationDbContext _dbContext) : base(_dbContext) + { } + } +} diff --git a/SessionCompanion/SessionCompanion.Database/Repositories/Base/IRepository.cs b/SessionCompanion/SessionCompanion.Database/Repositories/Base/IRepository.cs new file mode 100644 index 0000000..a07b5f9 --- /dev/null +++ b/SessionCompanion/SessionCompanion.Database/Repositories/Base/IRepository.cs @@ -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 where T : BaseEntity + { + IQueryable Get(); + IQueryable Get(Expression> expression); + Task Get(int id); + Task Create(T entity); + Task Update(T entity); + void Delete(T entity); + Task Any(Expression> expression); + Task Any(int id); + void Dispose(); + Task Save(); + } +} diff --git a/SessionCompanion/SessionCompanion.Database/Repositories/Base/Repository.cs b/SessionCompanion/SessionCompanion.Database/Repositories/Base/Repository.cs new file mode 100644 index 0000000..8fc3536 --- /dev/null +++ b/SessionCompanion/SessionCompanion.Database/Repositories/Base/Repository.cs @@ -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 : IRepository where T : BaseEntity + { + protected ApplicationDbContext RepositoryContext { get; set; } + + public Repository(ApplicationDbContext repositoryContext) + { + this.RepositoryContext = repositoryContext; + } + + public IQueryable Get() + { + return this.RepositoryContext.Set(); + } + + public IQueryable Get(Expression> expression) + { + return this.RepositoryContext.Set().Where(expression); + } + + public Task Any(Expression> expression) + { + return RepositoryContext.Set().AnyAsync(expression); + } + + public Task Any(int id) + { + return RepositoryContext.Set().AnyAsync(n => n.Id == id); + } + + + public async Task Get(int id) + { + return await RepositoryContext.Set().FindAsync(id); + } + + public virtual Task Create(T entity) + { + return Task.FromResult(RepositoryContext.Set().AddAsync(entity)); + } + + public virtual Task Update(T entity) + { + return Task.FromResult(RepositoryContext.Set().Update(entity)); + } + + public virtual void Delete(T entity) + { + this.RepositoryContext.Set().Remove(entity); + } + + public virtual async Task Save() + { + await RepositoryContext.SaveChangesAsync(); + } + + public void Dispose() + { + RepositoryContext.Dispose(); + } + } +} diff --git a/SessionCompanion/SessionCompanion.Database/Repositories/BiographyRepository.cs b/SessionCompanion/SessionCompanion.Database/Repositories/BiographyRepository.cs new file mode 100644 index 0000000..2a773f8 --- /dev/null +++ b/SessionCompanion/SessionCompanion.Database/Repositories/BiographyRepository.cs @@ -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, IRepository + { + public BiographyRepository(ApplicationDbContext _dbContext) : base(_dbContext) + { } + } +} diff --git a/SessionCompanion/SessionCompanion.Database/Repositories/CharacterRepository.cs b/SessionCompanion/SessionCompanion.Database/Repositories/CharacterRepository.cs new file mode 100644 index 0000000..7432e5d --- /dev/null +++ b/SessionCompanion/SessionCompanion.Database/Repositories/CharacterRepository.cs @@ -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, IRepository + { + public CharacterRepository(ApplicationDbContext _dbContext) : base(_dbContext) + { } + } +} diff --git a/SessionCompanion/SessionCompanion.Database/Repositories/ClassRepository.cs b/SessionCompanion/SessionCompanion.Database/Repositories/ClassRepository.cs new file mode 100644 index 0000000..b1b0954 --- /dev/null +++ b/SessionCompanion/SessionCompanion.Database/Repositories/ClassRepository.cs @@ -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, IRepository + { + public ClassRepository(ApplicationDbContext _dbContext) : base(_dbContext) + { } + } +} diff --git a/SessionCompanion/SessionCompanion.Database/Repositories/RaceRepository.cs b/SessionCompanion/SessionCompanion.Database/Repositories/RaceRepository.cs new file mode 100644 index 0000000..2f9b13d --- /dev/null +++ b/SessionCompanion/SessionCompanion.Database/Repositories/RaceRepository.cs @@ -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, IRepository + { + public RaceRepository(ApplicationDbContext _dbContext) : base(_dbContext) + { } + } +} diff --git a/SessionCompanion/SessionCompanion.Database/Repositories/StatisticsRepository.cs b/SessionCompanion/SessionCompanion.Database/Repositories/StatisticsRepository.cs new file mode 100644 index 0000000..6c30a4d --- /dev/null +++ b/SessionCompanion/SessionCompanion.Database/Repositories/StatisticsRepository.cs @@ -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, IRepository + { + public StatisticsRepository(ApplicationDbContext _dbContext) : base(_dbContext) + { } + } +} diff --git a/SessionCompanion/SessionCompanion.Database/Repositories/UserRepository.cs b/SessionCompanion/SessionCompanion.Database/Repositories/UserRepository.cs new file mode 100644 index 0000000..18feac0 --- /dev/null +++ b/SessionCompanion/SessionCompanion.Database/Repositories/UserRepository.cs @@ -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, IRepository + { + public UserRepository(ApplicationDbContext _dbContext) : base(_dbContext) + { } + } +} -- 2.20.1 From dfc3cdac982c6f09edd19b5e7d98212196cef7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20G=C3=B3rzy=C5=84ski?= Date: Thu, 3 Dec 2020 18:52:53 +0100 Subject: [PATCH 2/2] SES-67 Add config to repositories --- .../Configurations/RepositoryConfiguration.cs | 28 +++ SessionCompanion/SessionCompanion/Startup.cs | 163 +++++++++--------- 2 files changed, 111 insertions(+), 80 deletions(-) create mode 100644 SessionCompanion/SessionCompanion/Configurations/RepositoryConfiguration.cs diff --git a/SessionCompanion/SessionCompanion/Configurations/RepositoryConfiguration.cs b/SessionCompanion/SessionCompanion/Configurations/RepositoryConfiguration.cs new file mode 100644 index 0000000..8db8860 --- /dev/null +++ b/SessionCompanion/SessionCompanion/Configurations/RepositoryConfiguration.cs @@ -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, AlignmentRepository>(); + services.AddScoped, BackgroundRepository>(); + services.AddScoped, BiographyRepository>(); + services.AddScoped, CharacterRepository>(); + services.AddScoped, ClassRepository>(); + services.AddScoped, RaceRepository>(); + services.AddScoped, StatisticsRepository>(); + services.AddScoped, UserRepository>(); + + return services; + } + } +} diff --git a/SessionCompanion/SessionCompanion/Startup.cs b/SessionCompanion/SessionCompanion/Startup.cs index 74d6b69..ac583c8 100644 --- a/SessionCompanion/SessionCompanion/Startup.cs +++ b/SessionCompanion/SessionCompanion/Startup.cs @@ -1,86 +1,89 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.HttpsPolicy; -using Microsoft.AspNetCore.SpaServices.AngularCli; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.HttpsPolicy; +using Microsoft.AspNetCore.SpaServices.AngularCli; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using SessionCompanion.Configurations; using SessionCompanion.Database; -namespace SessionCompanion -{ - public class Startup - { - - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - services.AddControllersWithViews(); - services.AddDbContext(options => - options.UseSqlServer( - Configuration.GetConnectionString("DefaultConnection"))); - // In production, the Angular files will be served from this directory - services.AddSpaStaticFiles(configuration => - { - configuration.RootPath = "ClientApp/dist"; - }); - - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { +namespace SessionCompanion +{ + public class Startup + { + + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddControllersWithViews(); + services.AddDbContext(options => + options.UseSqlServer( + Configuration.GetConnectionString("DefaultConnection"))); + services.AddRepositories(); + + // In production, the Angular files will be served from this directory + services.AddSpaStaticFiles(configuration => + { + configuration.RootPath = "ClientApp/dist"; + }); + + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { using (var serviceScope = app.ApplicationServices.GetRequiredService().CreateScope()) { serviceScope.ServiceProvider.GetService().Database.Migrate(); - } - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else - { - app.UseExceptionHandler("/Error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseHsts(); - } - - app.UseHttpsRedirection(); - app.UseStaticFiles(); - if (!env.IsDevelopment()) - { - app.UseSpaStaticFiles(); - } - - app.UseRouting(); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllerRoute( - name: "default", - pattern: "{controller}/{action=Index}/{id?}"); - }); - - app.UseSpa(spa => - { - // To learn more about options for serving an Angular SPA from ASP.NET Core, - // see https://go.microsoft.com/fwlink/?linkid=864501 - - spa.Options.SourcePath = "ClientApp"; - - if (env.IsDevelopment()) - { - spa.UseAngularCliServer(npmScript: "start"); - } - }); - } - } -} + } + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + app.UseExceptionHandler("/Error"); + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); + } + + app.UseHttpsRedirection(); + app.UseStaticFiles(); + if (!env.IsDevelopment()) + { + app.UseSpaStaticFiles(); + } + + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllerRoute( + name: "default", + pattern: "{controller}/{action=Index}/{id?}"); + }); + + app.UseSpa(spa => + { + // To learn more about options for serving an Angular SPA from ASP.NET Core, + // see https://go.microsoft.com/fwlink/?linkid=864501 + + spa.Options.SourcePath = "ClientApp"; + + if (env.IsDevelopment()) + { + spa.UseAngularCliServer(npmScript: "start"); + } + }); + } + } +} -- 2.20.1