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 --- .../Attributes/GoodPasswordModelValidation.cs | 24 ++++ .../Attributes/OnlyLettersModelValidation.cs | 20 +++ .../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 +++++ .../DevHive.Web/Controllers/CommentController.cs | 83 ++++++++++++ src/Web/DevHive.Web/Controllers/FeedController.cs | 54 ++++++++ .../DevHive.Web/Controllers/LanguageController.cs | 87 +++++++++++++ src/Web/DevHive.Web/Controllers/PostController.cs | 89 +++++++++++++ src/Web/DevHive.Web/Controllers/RateController.cs | 40 ++++++ src/Web/DevHive.Web/Controllers/RoleController.cs | 77 ++++++++++++ .../Controllers/TechnologyController.cs | 87 +++++++++++++ src/Web/DevHive.Web/Controllers/UserController.cs | 140 +++++++++++++++++++++ src/Web/DevHive.Web/DevHive.Web.csproj | 30 +++++ .../DevHive.Web/Middleware/ExceptionMiddleware.cs | 50 ++++++++ src/Web/DevHive.Web/Program.cs | 23 ++++ src/Web/DevHive.Web/Properties/launchSettings.json | 36 ++++++ src/Web/DevHive.Web/Startup.cs | 70 +++++++++++ src/Web/DevHive.Web/appsettings.json | 20 +++ 30 files changed, 1323 insertions(+) create mode 100644 src/Web/DevHive.Web/Attributes/GoodPasswordModelValidation.cs create mode 100644 src/Web/DevHive.Web/Attributes/OnlyLettersModelValidation.cs 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 create mode 100644 src/Web/DevHive.Web/Controllers/CommentController.cs create mode 100644 src/Web/DevHive.Web/Controllers/FeedController.cs create mode 100644 src/Web/DevHive.Web/Controllers/LanguageController.cs create mode 100644 src/Web/DevHive.Web/Controllers/PostController.cs create mode 100644 src/Web/DevHive.Web/Controllers/RateController.cs create mode 100644 src/Web/DevHive.Web/Controllers/RoleController.cs create mode 100644 src/Web/DevHive.Web/Controllers/TechnologyController.cs create mode 100644 src/Web/DevHive.Web/Controllers/UserController.cs create mode 100644 src/Web/DevHive.Web/DevHive.Web.csproj create mode 100644 src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs create mode 100644 src/Web/DevHive.Web/Program.cs create mode 100644 src/Web/DevHive.Web/Properties/launchSettings.json create mode 100644 src/Web/DevHive.Web/Startup.cs create mode 100644 src/Web/DevHive.Web/appsettings.json (limited to 'src/Web/DevHive.Web') diff --git a/src/Web/DevHive.Web/Attributes/GoodPasswordModelValidation.cs b/src/Web/DevHive.Web/Attributes/GoodPasswordModelValidation.cs new file mode 100644 index 0000000..7d6a1ea --- /dev/null +++ b/src/Web/DevHive.Web/Attributes/GoodPasswordModelValidation.cs @@ -0,0 +1,24 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace DevHive.Web.Attributes +{ + public class GoodPassword : ValidationAttribute + { + public override bool IsValid(object value) + { + var stringValue = (string)value; + + for (int i = 0; i < stringValue.Length; i++) + { + if (Char.IsDigit(stringValue[i])) + { + base.ErrorMessage = "Password must be atleast 5 characters long!"; + return stringValue.Length >= 5; + } + } + base.ErrorMessage = "Password must contain atleast 1 digit!"; + return false; + } + } +} diff --git a/src/Web/DevHive.Web/Attributes/OnlyLettersModelValidation.cs b/src/Web/DevHive.Web/Attributes/OnlyLettersModelValidation.cs new file mode 100644 index 0000000..07afee9 --- /dev/null +++ b/src/Web/DevHive.Web/Attributes/OnlyLettersModelValidation.cs @@ -0,0 +1,20 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace DevHive.Web.Attributes +{ + public class OnlyLetters : ValidationAttribute + { + public override bool IsValid(object value) + { + var stringValue = (string)value; + + foreach (char ch in stringValue) + { + if (!Char.IsLetter(ch)) + return false; + } + return true; + } + } +} 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(); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/CommentController.cs b/src/Web/DevHive.Web/Controllers/CommentController.cs new file mode 100644 index 0000000..c38e300 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/CommentController.cs @@ -0,0 +1,83 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using AutoMapper; +using System; +using DevHive.Web.Models.Comment; +using DevHive.Services.Models.Comment; +using Microsoft.AspNetCore.Authorization; +using DevHive.Services.Interfaces; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + [Authorize(Roles = "User,Admin")] + public class CommentController + { + private readonly ICommentService _commentService; + private readonly IMapper _commentMapper; + + public CommentController(ICommentService commentService, IMapper commentMapper) + { + this._commentService = commentService; + this._commentMapper = commentMapper; + } + + [HttpPost] + public async Task AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel, [FromHeader] string authorization) + { + if (!await this._commentService.ValidateJwtForCreating(userId, authorization)) + return new UnauthorizedResult(); + + CreateCommentServiceModel createCommentServiceModel = + this._commentMapper.Map(createCommentWebModel); + createCommentServiceModel.CreatorId = userId; + + Guid id = await this._commentService.AddComment(createCommentServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Could not create comment!") : + new OkObjectResult(new { Id = id }); + } + + [HttpGet] + [AllowAnonymous] + public async Task GetCommentById(Guid id) + { + ReadCommentServiceModel readCommentServiceModel = await this._commentService.GetCommentById(id); + ReadCommentWebModel readCommentWebModel = this._commentMapper.Map(readCommentServiceModel); + + return new OkObjectResult(readCommentWebModel); + } + + [HttpPut] + public async Task UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization) + { + if (!await this._commentService.ValidateJwtForComment(updateCommentWebModel.CommentId, authorization)) + return new UnauthorizedResult(); + + UpdateCommentServiceModel updateCommentServiceModel = + this._commentMapper.Map(updateCommentWebModel); + updateCommentServiceModel.CreatorId = userId; + + Guid id = await this._commentService.UpdateComment(updateCommentServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Unable to update comment!") : + new OkObjectResult(new { Id = id }); + } + + [HttpDelete] + public async Task DeleteComment(Guid id, [FromHeader] string authorization) + { + if (!await this._commentService.ValidateJwtForComment(id, authorization)) + return new UnauthorizedResult(); + + return await this._commentService.DeleteComment(id) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Comment"); + } + + } +} + diff --git a/src/Web/DevHive.Web/Controllers/FeedController.cs b/src/Web/DevHive.Web/Controllers/FeedController.cs new file mode 100644 index 0000000..abca3e4 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/FeedController.cs @@ -0,0 +1,54 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models; +using DevHive.Web.Models.Comment; +using DevHive.Web.Models.Feed; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + [Authorize(Roles = "User,Admin")] + public class FeedController + { + private readonly IFeedService _feedService; + private readonly IMapper _mapper; + + public FeedController(IFeedService feedService, IMapper mapper) + { + this._feedService = feedService; + this._mapper = mapper; + } + + [HttpPost] + [Route("GetPosts")] + public async Task GetPosts(Guid userId, [FromBody] GetPageWebModel getPageWebModel) + { + GetPageServiceModel getPageServiceModel = this._mapper.Map(getPageWebModel); + getPageServiceModel.UserId = userId; + + ReadPageServiceModel readPageServiceModel = await this._feedService.GetPage(getPageServiceModel); + ReadPageWebModel readPageWebModel = this._mapper.Map(readPageServiceModel); + + return new OkObjectResult(readPageWebModel); + } + + [HttpPost] + [Route("GetUserPosts")] + [AllowAnonymous] + public async Task GetUserPosts(string username, [FromBody] GetPageWebModel getPageWebModel) + { + GetPageServiceModel getPageServiceModel = this._mapper.Map(getPageWebModel); + getPageServiceModel.Username = username; + + ReadPageServiceModel readPageServiceModel = await this._feedService.GetUserPage(getPageServiceModel); + ReadPageWebModel readPageWebModel = this._mapper.Map(readPageServiceModel); + + return new OkObjectResult(readPageWebModel); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/LanguageController.cs b/src/Web/DevHive.Web/Controllers/LanguageController.cs new file mode 100644 index 0000000..5b0d5de --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/LanguageController.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Language; +using DevHive.Web.Models.Language; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class LanguageController + { + private readonly ILanguageService _languageService; + private readonly IMapper _languageMapper; + + public LanguageController(ILanguageService languageService, IMapper mapper) + { + this._languageService = languageService; + this._languageMapper = mapper; + } + + [HttpPost] + [Authorize(Roles = "Admin")] + public async Task Create([FromBody] CreateLanguageWebModel createLanguageWebModel) + { + CreateLanguageServiceModel languageServiceModel = this._languageMapper.Map(createLanguageWebModel); + + Guid id = await this._languageService.CreateLanguage(languageServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create language {createLanguageWebModel.Name}") : + new OkObjectResult(new { Id = id }); + } + + [HttpGet] + [AllowAnonymous] + public async Task GetById(Guid id) + { + ReadLanguageServiceModel languageServiceModel = await this._languageService.GetLanguageById(id); + ReadLanguageWebModel languageWebModel = this._languageMapper.Map(languageServiceModel); + + return new OkObjectResult(languageWebModel); + } + + [HttpGet] + [Route("GetLanguages")] + [Authorize(Roles = "User,Admin")] + public IActionResult GetAllExistingLanguages() + { + HashSet languageServiceModels = this._languageService.GetLanguages(); + HashSet languageWebModels = this._languageMapper.Map>(languageServiceModels); + + return new OkObjectResult(languageWebModels); + } + + [HttpPut] + [Authorize(Roles = "Admin")] + public async Task Update(Guid id, [FromBody] UpdateLanguageWebModel updateModel) + { + UpdateLanguageServiceModel updatelanguageServiceModel = this._languageMapper.Map(updateModel); + updatelanguageServiceModel.Id = id; + + bool result = await this._languageService.UpdateLanguage(updatelanguageServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update Language"); + + return new OkResult(); + } + + [HttpDelete] + [Authorize(Roles = "Admin")] + public async Task Delete(Guid id) + { + bool result = await this._languageService.DeleteLanguage(id); + + if (!result) + return new BadRequestObjectResult("Could not delete Language"); + + return new OkResult(); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/PostController.cs b/src/Web/DevHive.Web/Controllers/PostController.cs new file mode 100644 index 0000000..d3fdbf6 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/PostController.cs @@ -0,0 +1,89 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using AutoMapper; +using System; +using DevHive.Web.Models.Post; +using DevHive.Services.Models.Post; +using Microsoft.AspNetCore.Authorization; +using DevHive.Services.Interfaces; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + [Authorize(Roles = "User,Admin")] + public class PostController + { + private readonly IPostService _postService; + private readonly IMapper _postMapper; + + public PostController(IPostService postService, IMapper postMapper) + { + this._postService = postService; + this._postMapper = postMapper; + } + + #region Create + [HttpPost] + public async Task Create(Guid userId, [FromForm] CreatePostWebModel createPostWebModel, [FromHeader] string authorization) + { + if (!await this._postService.ValidateJwtForCreating(userId, authorization)) + return new UnauthorizedResult(); + + CreatePostServiceModel createPostServiceModel = + this._postMapper.Map(createPostWebModel); + createPostServiceModel.CreatorId = userId; + + Guid id = await this._postService.CreatePost(createPostServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Could not create post!") : + new OkObjectResult(new { Id = id }); + } + #endregion + + #region Read + [HttpGet] + [AllowAnonymous] + public async Task GetById(Guid id) + { + ReadPostServiceModel postServiceModel = await this._postService.GetPostById(id); + ReadPostWebModel postWebModel = this._postMapper.Map(postServiceModel); + + return new OkObjectResult(postWebModel); + } + #endregion + + #region Update + [HttpPut] + public async Task Update(Guid userId, [FromForm] UpdatePostWebModel updatePostWebModel, [FromHeader] string authorization) + { + if (!await this._postService.ValidateJwtForPost(updatePostWebModel.PostId, authorization)) + return new UnauthorizedResult(); + + UpdatePostServiceModel updatePostServiceModel = + this._postMapper.Map(updatePostWebModel); + updatePostServiceModel.CreatorId = userId; + + Guid id = await this._postService.UpdatePost(updatePostServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Could not update post!") : + new OkObjectResult(new { Id = id }); + } + #endregion + + #region Delete + [HttpDelete] + public async Task Delete(Guid id, [FromHeader] string authorization) + { + if (!await this._postService.ValidateJwtForPost(id, authorization)) + return new UnauthorizedResult(); + + return await this._postService.DeletePost(id) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Post"); + } + #endregion + } +} diff --git a/src/Web/DevHive.Web/Controllers/RateController.cs b/src/Web/DevHive.Web/Controllers/RateController.cs new file mode 100644 index 0000000..68b859b --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/RateController.cs @@ -0,0 +1,40 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Post.Rating; +using DevHive.Web.Models.Post.Rating; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class RateController + { + private readonly IRateService _rateService; + private readonly IUserService _userService; + private readonly IMapper _mapper; + + public RateController(IRateService rateService, IUserService userService, IMapper mapper) + { + this._rateService = rateService; + this._userService = userService; + this._mapper = mapper; + } + + [HttpPost] + [Authorize(Roles = "Admin,User")] + public async Task RatePost(Guid userId, [FromBody] RatePostWebModel ratePostWebModel, [FromHeader] string authorization) + { + RatePostServiceModel ratePostServiceModel = this._mapper.Map(ratePostWebModel); + ratePostServiceModel.UserId = userId; + + ReadPostRatingServiceModel readPostRatingServiceModel = await this._rateService.RatePost(ratePostServiceModel); + ReadPostRatingWebModel readPostRatingWebModel = this._mapper.Map(readPostRatingServiceModel); + + return new OkObjectResult(readPostRatingWebModel); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/RoleController.cs b/src/Web/DevHive.Web/Controllers/RoleController.cs new file mode 100644 index 0000000..0d2a2eb --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/RoleController.cs @@ -0,0 +1,77 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using DevHive.Web.Models.Identity.Role; +using AutoMapper; +using System; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Identity.Role; +using Microsoft.AspNetCore.Authorization; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class RoleController + { + private readonly IRoleService _roleService; + private readonly IMapper _roleMapper; + + public RoleController(IRoleService roleService, IMapper mapper) + { + this._roleService = roleService; + this._roleMapper = mapper; + } + + [HttpPost] + [Authorize(Roles = "Admin")] + public async Task Create([FromBody] CreateRoleWebModel createRoleWebModel) + { + CreateRoleServiceModel roleServiceModel = + this._roleMapper.Map(createRoleWebModel); + + Guid id = await this._roleService.CreateRole(roleServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create role {createRoleWebModel.Name}") : + new OkObjectResult(new { Id = id }); + } + + [HttpGet] + [Authorize(Roles = "User,Admin")] + public async Task GetById(Guid id) + { + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); + + return new OkObjectResult(roleWebModel); + } + + [HttpPut] + [Authorize(Roles = "Admin")] + public async Task Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) + { + UpdateRoleServiceModel updateRoleServiceModel = + this._roleMapper.Map(updateRoleWebModel); + updateRoleServiceModel.Id = id; + + bool result = await this._roleService.UpdateRole(updateRoleServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update role!"); + + return new OkResult(); + } + + [HttpDelete] + [Authorize(Roles = "Admin")] + public async Task Delete(Guid id) + { + bool result = await this._roleService.DeleteRole(id); + + if (!result) + return new BadRequestObjectResult("Could not delete role!"); + + return new OkResult(); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/TechnologyController.cs b/src/Web/DevHive.Web/Controllers/TechnologyController.cs new file mode 100644 index 0000000..e507899 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/TechnologyController.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Technology; +using DevHive.Web.Models.Technology; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class TechnologyController + { + private readonly ITechnologyService _technologyService; + private readonly IMapper _technologyMapper; + + public TechnologyController(ITechnologyService technologyService, IMapper technologyMapper) + { + this._technologyService = technologyService; + this._technologyMapper = technologyMapper; + } + + [HttpPost] + [Authorize(Roles = "Admin")] + public async Task Create([FromBody] CreateTechnologyWebModel createTechnologyWebModel) + { + CreateTechnologyServiceModel technologyServiceModel = this._technologyMapper.Map(createTechnologyWebModel); + + Guid id = await this._technologyService.CreateTechnology(technologyServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create technology {createTechnologyWebModel.Name}") : + new OkObjectResult(new { Id = id }); + } + + [HttpGet] + [AllowAnonymous] + public async Task GetById(Guid id) + { + ReadTechnologyServiceModel readTechnologyServiceModel = await this._technologyService.GetTechnologyById(id); + ReadTechnologyWebModel readTechnologyWebModel = this._technologyMapper.Map(readTechnologyServiceModel); + + return new OkObjectResult(readTechnologyWebModel); + } + + [HttpGet] + [Route("GetTechnologies")] + [Authorize(Roles = "User,Admin")] + public IActionResult GetAllExistingTechnologies() + { + HashSet technologyServiceModels = this._technologyService.GetTechnologies(); + HashSet languageWebModels = this._technologyMapper.Map>(technologyServiceModels); + + return new OkObjectResult(languageWebModels); + } + + [HttpPut] + [Authorize(Roles = "Admin")] + public async Task Update(Guid id, [FromBody] UpdateTechnologyWebModel updateModel) + { + UpdateTechnologyServiceModel updateTechnologyServiceModel = this._technologyMapper.Map(updateModel); + updateTechnologyServiceModel.Id = id; + + bool result = await this._technologyService.UpdateTechnology(updateTechnologyServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update Technology"); + + return new OkResult(); + } + + [HttpDelete] + [Authorize(Roles = "Admin")] + public async Task Delete(Guid id) + { + bool result = await this._technologyService.DeleteTechnology(id); + + if (!result) + return new BadRequestObjectResult("Could not delete Technology"); + + return new OkResult(); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/UserController.cs b/src/Web/DevHive.Web/Controllers/UserController.cs new file mode 100644 index 0000000..109bbaa --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/UserController.cs @@ -0,0 +1,140 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Models.Identity.User; +using DevHive.Web.Models.Identity.User; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using DevHive.Common.Models.Identity; +using DevHive.Services.Interfaces; +using Microsoft.Extensions.Hosting; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class UserController : ControllerBase + { + private readonly IUserService _userService; + private readonly IMapper _userMapper; + + public UserController(IUserService userService, IMapper mapper) + { + this._userService = userService; + this._userMapper = mapper; + } + + #region Authentication + [HttpPost] + [Route("Login")] + [AllowAnonymous] + public async Task Login([FromBody] LoginWebModel loginModel) + { + LoginServiceModel loginServiceModel = this._userMapper.Map(loginModel); + + TokenModel TokenModel = await this._userService.LoginUser(loginServiceModel); + TokenWebModel tokenWebModel = this._userMapper.Map(TokenModel); + + return new OkObjectResult(tokenWebModel); + } + + [HttpPost] + [Route("Register")] + [AllowAnonymous] + public async Task Register([FromBody] RegisterWebModel registerModel) + { + RegisterServiceModel registerServiceModel = this._userMapper.Map(registerModel); + + TokenModel TokenModel = await this._userService.RegisterUser(registerServiceModel); + TokenWebModel tokenWebModel = this._userMapper.Map(TokenModel); + + return new CreatedResult("Register", tokenWebModel); + } + #endregion + + #region Read + [HttpGet] + [Authorize(Roles = "User,Admin")] + public async Task GetById(Guid id, [FromHeader] string authorization) + { + if (!await this._userService.ValidJWT(id, authorization)) + return new UnauthorizedResult(); + + UserServiceModel userServiceModel = await this._userService.GetUserById(id); + UserWebModel userWebModel = this._userMapper.Map(userServiceModel); + + return new OkObjectResult(userWebModel); + } + + [HttpGet] + [Route("GetUser")] + [AllowAnonymous] + public async Task GetUser(string username) + { + UserServiceModel friendServiceModel = await this._userService.GetUserByUsername(username); + UserWebModel friend = this._userMapper.Map(friendServiceModel); + + return new OkObjectResult(friend); + } + #endregion + + #region Update + [HttpPut] + [Authorize(Roles = "User,Admin")] + public async Task Update(Guid id, [FromBody] UpdateUserWebModel updateUserWebModel, [FromHeader] string authorization) + { + if (!await this._userService.ValidJWT(id, authorization)) + return new UnauthorizedResult(); + + UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(updateUserWebModel); + updateUserServiceModel.Id = id; + + UserServiceModel userServiceModel = await this._userService.UpdateUser(updateUserServiceModel); + UserWebModel userWebModel = this._userMapper.Map(userServiceModel); + + return new AcceptedResult("UpdateUser", userWebModel); + } + + [HttpPut] + [Route("ProfilePicture")] + [Authorize(Roles = "User,Admin")] + public async Task UpdateProfilePicture(Guid userId, [FromForm] UpdateProfilePictureWebModel updateProfilePictureWebModel, [FromHeader] string authorization) + { + if (!await this._userService.ValidJWT(userId, authorization)) + return new UnauthorizedResult(); + + UpdateProfilePictureServiceModel updateProfilePictureServiceModel = this._userMapper.Map(updateProfilePictureWebModel); + updateProfilePictureServiceModel.UserId = userId; + + ProfilePictureServiceModel profilePictureServiceModel = await this._userService.UpdateProfilePicture(updateProfilePictureServiceModel); + ProfilePictureWebModel profilePictureWebModel = this._userMapper.Map(profilePictureServiceModel); + + return new AcceptedResult("UpdateProfilePicture", profilePictureWebModel); + } + #endregion + + #region Delete + [HttpDelete] + [Authorize(Roles = "User,Admin")] + public async Task Delete(Guid id, [FromHeader] string authorization) + { + if (!await this._userService.ValidJWT(id, authorization)) + return new UnauthorizedResult(); + + bool result = await this._userService.DeleteUser(id); + if (!result) + return new BadRequestObjectResult("Could not delete User"); + + return new OkResult(); + } + #endregion + + [HttpPost] + [Authorize(Roles = "User,Admin")] + [Route("SuperSecretPromotionToAdmin")] + public async Task SuperSecretPromotionToAdmin(Guid userId) + { + return new OkObjectResult(await this._userService.SuperSecretPromotionToAdmin(userId)); + } + } +} diff --git a/src/Web/DevHive.Web/DevHive.Web.csproj b/src/Web/DevHive.Web/DevHive.Web.csproj new file mode 100644 index 0000000..71362ce --- /dev/null +++ b/src/Web/DevHive.Web/DevHive.Web.csproj @@ -0,0 +1,30 @@ + + + net5.0 + + + true + latest + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + + + + diff --git a/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs b/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs new file mode 100644 index 0000000..cb6d4ca --- /dev/null +++ b/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs @@ -0,0 +1,50 @@ +using System; +using System.Net; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; + +namespace DevHive.Web.Middleware +{ + public class ExceptionMiddleware + { + private readonly RequestDelegate _next; + // private readonly ILogger _logger; + + public ExceptionMiddleware(RequestDelegate next) + { + this._next = next; + // this._logger = logger; + } + // public ExceptionMiddleware(RequestDelegate next, ILogger logger) + // { + // this._logger = logger; + // this._next = next; + // } + + public async Task InvokeAsync(HttpContext httpContext) + { + try + { + await this._next(httpContext); + } + catch (Exception ex) + { + // this._logger.LogError($"Something went wrong: {ex}"); + await HandleExceptionAsync(httpContext, ex); + } + } + + private Task HandleExceptionAsync(HttpContext context, Exception exception) + { + context.Response.ContentType = "application/json"; + context.Response.StatusCode = (int)HttpStatusCode.BadRequest; + + return context.Response.WriteAsync(new + { + StatusCode = context.Response.StatusCode, + Message = exception.Message + }.ToString()); + } + } +} diff --git a/src/Web/DevHive.Web/Program.cs b/src/Web/DevHive.Web/Program.cs new file mode 100644 index 0000000..6982da9 --- /dev/null +++ b/src/Web/DevHive.Web/Program.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; + +namespace DevHive.Web +{ + public class Program + { + private const int HTTP_PORT = 5000; + + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.ConfigureKestrel(opt => opt.ListenLocalhost(HTTP_PORT)); + webBuilder.UseStartup(); + }); + } +} diff --git a/src/Web/DevHive.Web/Properties/launchSettings.json b/src/Web/DevHive.Web/Properties/launchSettings.json new file mode 100644 index 0000000..2b65d0b --- /dev/null +++ b/src/Web/DevHive.Web/Properties/launchSettings.json @@ -0,0 +1,36 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:1955", + "sslPort": 44326 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "DevHive.Web": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "DevHive.Web Production": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Production" + } + } + } +} diff --git a/src/Web/DevHive.Web/Startup.cs b/src/Web/DevHive.Web/Startup.cs new file mode 100644 index 0000000..46521cf --- /dev/null +++ b/src/Web/DevHive.Web/Startup.cs @@ -0,0 +1,70 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using DevHive.Web.Configurations.Extensions; +using Newtonsoft.Json; + +namespace DevHive.Web +{ + 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.AddCors(); + + services.AddControllers() + .AddNewtonsoftJson(x => + { + x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; + }); + + services.DatabaseConfiguration(Configuration); + services.SwaggerConfiguration(); + services.JWTConfiguration(Configuration); + services.AutoMapperConfiguration(); + services.DependencyInjectionConfiguration(this.Configuration); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + app.UseCors(x => x + .AllowAnyMethod() + .AllowAnyHeader() + .SetIsOriginAllowed(origin => true) // allow any origin + .AllowCredentials()); // allow credentials + + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + app.UseSwaggerConfiguration(); + } + else + { + app.UseHsts(); + app.UseExceptionHandlerMiddlewareConfiguration(); + } + + app.UseDatabaseConfiguration(); + app.UseAutoMapperConfiguration(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllerRoute( + name: "default", + pattern: "api/{controller}/{action}" + ); + }); + } + } +} diff --git a/src/Web/DevHive.Web/appsettings.json b/src/Web/DevHive.Web/appsettings.json new file mode 100644 index 0000000..bcdcae7 --- /dev/null +++ b/src/Web/DevHive.Web/appsettings.json @@ -0,0 +1,20 @@ +{ + "AppSettings": { + "Secret": "gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm" + }, + "ConnectionStrings": { + "DEV": "Server=localhost;Port=5432;Database=API;User Id=postgres;Password=;" + }, + "Cloud": { + "cloudName": "devhive", + "apiKey": "488664116365813", + "apiSecret": "" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} -- cgit v1.2.3