From 98e17766b203734a1817eed94338e2d25f4395f7 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 13 Feb 2021 16:20:18 +0200 Subject: Project Restructure P.1 --- .../Extensions/ConfigureAutoMapper.cs | 24 ++++++++ .../Configurations/Extensions/ConfigureDatabase.cs | 70 ++++++++++++++++++++++ .../Extensions/ConfigureDependencyInjection.cs | 38 ++++++++++++ .../ConfigureExceptionHandlerMiddleware.cs | 16 +++++ .../Configurations/Extensions/ConfigureJWT.cs | 54 +++++++++++++++++ .../Configurations/Extensions/ConfigureSwagger.cs | 23 +++++++ .../Configurations/Mapping/CommentMappings.cs | 17 ++++++ .../Configurations/Mapping/FeedMappings.cs | 18 ++++++ .../Configurations/Mapping/LanguageMappings.cs | 23 +++++++ .../Configurations/Mapping/PostMappings.cs | 17 ++++++ .../Configurations/Mapping/RatingMappings.cs | 16 +++++ .../Configurations/Mapping/RoleMappings.cs | 21 +++++++ .../Configurations/Mapping/TechnologyMappings.cs | 23 +++++++ .../Configurations/Mapping/UserMappings.cs | 33 ++++++++++ 14 files changed, 393 insertions(+) create mode 100644 src/Web/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs create mode 100644 src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs create mode 100644 src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs create mode 100644 src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs create mode 100644 src/Web/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs create mode 100644 src/Web/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs create mode 100644 src/Web/DevHive.Web/Configurations/Mapping/CommentMappings.cs create mode 100644 src/Web/DevHive.Web/Configurations/Mapping/FeedMappings.cs create mode 100644 src/Web/DevHive.Web/Configurations/Mapping/LanguageMappings.cs create mode 100644 src/Web/DevHive.Web/Configurations/Mapping/PostMappings.cs create mode 100644 src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs create mode 100644 src/Web/DevHive.Web/Configurations/Mapping/RoleMappings.cs create mode 100644 src/Web/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs create mode 100644 src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs (limited to 'src/Web/DevHive.Web/Configurations') diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs new file mode 100644 index 0000000..8b7d657 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs @@ -0,0 +1,24 @@ +using System; +using AutoMapper; +//using AutoMapper.Configuration; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class ConfigureAutoMapper + { + public static void AutoMapperConfiguration(this IServiceCollection services) + { + services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); + } + + public static void UseAutoMapperConfiguration(this IApplicationBuilder app) + { + var config = new MapperConfiguration(cfg => + { + cfg.AllowNullCollections = true; + }); + } + } +} \ No newline at end of file diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs new file mode 100644 index 0000000..9f02dd7 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs @@ -0,0 +1,70 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using DevHive.Data.Models; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Builder; +using System; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using DevHive.Data; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class DatabaseExtensions + { + public static void DatabaseConfiguration(this IServiceCollection services, IConfiguration configuration) + { + services.AddDbContext(options => + { + options.EnableSensitiveDataLogging(true); + options.UseNpgsql(configuration.GetConnectionString("DEV")); + }); + + services.AddIdentity() + .AddRoles() + .AddEntityFrameworkStores(); + + services.Configure(options => + { + options.User.RequireUniqueEmail = true; + + options.Password.RequireDigit = true; + options.Password.RequiredLength = 5; + options.Password.RequiredUniqueChars = 0; + options.Password.RequireLowercase = false; + options.Password.RequireNonAlphanumeric = false; + options.Password.RequireUppercase = false; + + options.Lockout.AllowedForNewUsers = true; + options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5); + options.Lockout.MaxFailedAccessAttempts = 5; + }); + + services.AddAuthorization(options => + { + options.AddPolicy("User", options => + { + options.RequireAuthenticatedUser(); + options.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); + options.RequireRole("User"); + }); + + options.AddPolicy("Administrator", options => + { + options.RequireAuthenticatedUser(); + options.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); + options.RequireRole("Admin"); + }); + }); + } + + public static void UseDatabaseConfiguration(this IApplicationBuilder app) + { + app.UseHttpsRedirection(); + app.UseRouting(); + + app.UseAuthentication(); + app.UseAuthorization(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs new file mode 100644 index 0000000..88f21d4 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -0,0 +1,38 @@ +using DevHive.Data.Interfaces.Repositories; +using DevHive.Data.Repositories; +using DevHive.Services.Interfaces; +using DevHive.Services.Services; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class ConfigureDependencyInjection + { + public static void DependencyInjectionConfiguration(this IServiceCollection services, IConfiguration configuration) + { + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(options => + new CloudinaryService( + cloudName: configuration.GetSection("Cloud").GetSection("cloudName").Value, + apiKey: configuration.GetSection("Cloud").GetSection("apiKey").Value, + apiSecret: configuration.GetSection("Cloud").GetSection("apiSecret").Value)); + services.AddTransient(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs new file mode 100644 index 0000000..286727f --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs @@ -0,0 +1,16 @@ +using DevHive.Web.Middleware; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class ConfigureExceptionHandlerMiddleware + { + public static void ExceptionHandlerMiddlewareConfiguration(this IServiceCollection services) { } + + public static void UseExceptionHandlerMiddlewareConfiguration(this IApplicationBuilder app) + { + app.UseMiddleware(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs new file mode 100644 index 0000000..d422bc8 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs @@ -0,0 +1,54 @@ +using System.Text; +using System.Threading.Tasks; +using DevHive.Services.Options; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.IdentityModel.Tokens; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class JWTExtensions + { + public static void JWTConfiguration(this IServiceCollection services, IConfiguration configuration) + { + services.AddSingleton(new JWTOptions(configuration + .GetSection("AppSettings") + .GetSection("Secret") + .Value)); + + // Get key from appsettings.json + var key = Encoding.ASCII.GetBytes(configuration + .GetSection("AppSettings") + .GetSection("Secret") + .Value); + + // Setup Jwt Authentication + services.AddAuthentication(x => + { + x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + }) + .AddJwtBearer(x => + { + x.Events = new JwtBearerEvents + { + OnTokenValidated = context => + { + // TODO: add more authentication + return Task.CompletedTask; + } + }; + x.RequireHttpsMetadata = false; + x.SaveToken = true; + x.TokenValidationParameters = new TokenValidationParameters + { + //ValidateIssuerSigningKey = false, + IssuerSigningKey = new SymmetricSecurityKey(key), + ValidateIssuer = false, + ValidateAudience = false + }; + }); + } + } +} \ No newline at end of file diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs new file mode 100644 index 0000000..a0641ab --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.OpenApi.Models; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class SwaggerExtensions + { + public static void SwaggerConfiguration(this IServiceCollection services) + { + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" }); + }); + } + + public static void UseSwaggerConfiguration(this IApplicationBuilder app) + { + app.UseSwagger(); + app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1")); + } + } +} \ No newline at end of file diff --git a/src/Web/DevHive.Web/Configurations/Mapping/CommentMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/CommentMappings.cs new file mode 100644 index 0000000..b8d6829 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/CommentMappings.cs @@ -0,0 +1,17 @@ +using AutoMapper; +using DevHive.Services.Models.Comment; +using DevHive.Web.Models.Comment; + +namespace DevHive.Web.Configurations.Mapping +{ + public class CommentMappings : Profile + { + public CommentMappings() + { + CreateMap(); + CreateMap(); + + CreateMap(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/FeedMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/FeedMappings.cs new file mode 100644 index 0000000..0909f6d --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/FeedMappings.cs @@ -0,0 +1,18 @@ +using AutoMapper; +using DevHive.Services.Models; +using DevHive.Web.Models.Comment; +using DevHive.Web.Models.Feed; + +namespace DevHive.Web.Configurations.Mapping +{ + public class FeedMappings : Profile + { + public FeedMappings() + { + CreateMap() + .ForMember(dest => dest.FirstRequestIssued, src => src.MapFrom(p => p.FirstPageTimeIssued)); + + CreateMap(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/LanguageMappings.cs new file mode 100644 index 0000000..eca0d1a --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -0,0 +1,23 @@ +using AutoMapper; +using DevHive.Web.Models.Language; +using DevHive.Services.Models.Language; + +namespace DevHive.Web.Configurations.Mapping +{ + public class LanguageMappings : Profile + { + public LanguageMappings() + { + CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/PostMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/PostMappings.cs new file mode 100644 index 0000000..a5b46ee --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/PostMappings.cs @@ -0,0 +1,17 @@ +using AutoMapper; +using DevHive.Services.Models.Post; +using DevHive.Web.Models.Post; + +namespace DevHive.Web.Configurations.Mapping +{ + public class PostMappings : Profile + { + public PostMappings() + { + CreateMap(); + CreateMap(); + + CreateMap(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs new file mode 100644 index 0000000..4e071de --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs @@ -0,0 +1,16 @@ +using AutoMapper; +using DevHive.Services.Models.Post.Rating; +using DevHive.Web.Models.Post.Rating; + +namespace DevHive.Web.Configurations.Mapping +{ + public class RatingMappings : Profile + { + public RatingMappings() + { + CreateMap(); + + CreateMap(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/RoleMappings.cs new file mode 100644 index 0000000..2ea2742 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -0,0 +1,21 @@ +using AutoMapper; +using DevHive.Web.Models.Identity.Role; +using DevHive.Services.Models.Identity.Role; + +namespace DevHive.Web.Configurations.Mapping +{ + public class RoleMappings : Profile + { + public RoleMappings() + { + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs new file mode 100644 index 0000000..708b6ac --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -0,0 +1,23 @@ +using AutoMapper; +using DevHive.Web.Models.Technology; +using DevHive.Services.Models.Technology; + +namespace DevHive.Web.Configurations.Mapping +{ + public class TechnologyMappings : Profile + { + public TechnologyMappings() + { + CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs new file mode 100644 index 0000000..f58e7ca --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -0,0 +1,33 @@ +using AutoMapper; +using DevHive.Services.Models.Identity.User; +using DevHive.Web.Models.Identity.User; +using DevHive.Common.Models.Identity; + +namespace DevHive.Web.Configurations.Mapping +{ + public class UserMappings : Profile + { + public UserMappings() + { + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap(); + + CreateMap(); + + //Update + CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + } + } +} -- cgit v1.2.3