aboutsummaryrefslogtreecommitdiff
path: root/src/Web/DevHive.Web
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-04-07 18:41:30 +0300
committertranstrike <transtrike@gmail.com>2021-04-07 18:41:30 +0300
commitb8d1edfc582bee2ef648757bcad1bbadd075bb08 (patch)
tree0042a44c3091af36994cdc9b91a835ed1d803eaa /src/Web/DevHive.Web
parentf4eac9c89bef0c21d7ccb29cb0841fa621c79f46 (diff)
parentda7d6223c261aac8e8f18458c11fb48cf9ca4cfe (diff)
downloadDevHive-b8d1edfc582bee2ef648757bcad1bbadd075bb08.tar
DevHive-b8d1edfc582bee2ef648757bcad1bbadd075bb08.tar.gz
DevHive-b8d1edfc582bee2ef648757bcad1bbadd075bb08.zip
Merged from dev
Diffstat (limited to 'src/Web/DevHive.Web')
-rw-r--r--src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs2
-rw-r--r--src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs5
-rw-r--r--src/Web/DevHive.Web/Configurations/Mapping/ProfilePictureMappings.cs16
-rw-r--r--src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs3
-rw-r--r--src/Web/DevHive.Web/Controllers/ProfilePictureController.cs56
-rw-r--r--src/Web/DevHive.Web/DevHive.Web.csproj31
-rw-r--r--src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs11
-rw-r--r--src/Web/DevHive.Web/Program.cs26
-rw-r--r--src/Web/DevHive.Web/Startup.cs6
-rw-r--r--src/Web/DevHive.Web/appsettings.json40
10 files changed, 143 insertions, 53 deletions
diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs
index 1bd8df0..b4c49b4 100644
--- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs
+++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs
@@ -18,7 +18,7 @@ namespace DevHive.Web.Configurations.Extensions
{
services.AddDbContext<DevHiveContext>(options =>
{
- options.EnableSensitiveDataLogging(true);
+ // options.EnableSensitiveDataLogging(true);
options.UseNpgsql(configuration.GetConnectionString("DEV"), options =>
{
options.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs
index ae14520..f49a335 100644
--- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs
+++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs
@@ -17,16 +17,16 @@ namespace DevHive.Web.Configurations.Extensions
services.AddTransient<ILanguageRepository, LanguageRepository>();
services.AddTransient<IRoleRepository, RoleRepository>();
services.AddTransient<ITechnologyRepository, TechnologyRepository>();
- services.AddTransient<IUserRepository, UserRepository>();
services.AddTransient<IPostRepository, PostRepository>();
services.AddTransient<ICommentRepository, CommentRepository>();
services.AddTransient<IFeedRepository, FeedRepository>();
services.AddTransient<IRatingRepository, RatingRepository>();
+ services.AddTransient<IProfilePictureRepository, ProfilePictureRepository>();
+ services.AddTransient<IUserRepository, UserRepository>();
services.AddTransient<ILanguageService, LanguageService>();
services.AddTransient<IRoleService, RoleService>();
services.AddTransient<ITechnologyService, TechnologyService>();
- services.AddTransient<IUserService, UserService>();
services.AddTransient<IPostService, PostService>();
services.AddTransient<ICommentService, CommentService>();
services.AddTransient<IFeedService, FeedService>();
@@ -46,7 +46,6 @@ namespace DevHive.Web.Configurations.Extensions
signingKey: Encoding.ASCII.GetBytes(configuration.GetSection("Jwt").GetSection("signingKey").Value),
validationIssuer: configuration.GetSection("Jwt").GetSection("validationIssuer").Value,
audience: configuration.GetSection("Jwt").GetSection("audience").Value));
- services.AddTransient<IRatingService, RatingService>();
}
}
}
diff --git a/src/Web/DevHive.Web/Configurations/Mapping/ProfilePictureMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/ProfilePictureMappings.cs
new file mode 100644
index 0000000..8c12a20
--- /dev/null
+++ b/src/Web/DevHive.Web/Configurations/Mapping/ProfilePictureMappings.cs
@@ -0,0 +1,16 @@
+using System;
+using AutoMapper;
+using DevHive.Web.Models.ProfilePicture;
+using DevHive.Services.Models.ProfilePicture;
+
+namespace DevHive.Web.Configurations.Mapping
+{
+ public class ProfilePictureMappings : Profile
+ {
+ public ProfilePictureMappings()
+ {
+ CreateMap<ProfilePictureWebModel, ProfilePictureServiceModel>()
+ .ForMember(dest => dest.ProfilePictureFormFile, src => src.MapFrom(p => p.Picture));
+ }
+ }
+}
diff --git a/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs
index 14aaa3a..dabec93 100644
--- a/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs
+++ b/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs
@@ -23,9 +23,6 @@ namespace DevHive.Web.Configurations.Mapping
CreateMap<UsernameWebModel, FriendServiceModel>();
CreateMap<UsernameWebModel, UpdateFriendServiceModel>();
- CreateMap<UpdateProfilePictureWebModel, UpdateProfilePictureServiceModel>();
- CreateMap<ProfilePictureServiceModel, ProfilePictureWebModel>();
-
CreateMap<UpdateUserServiceModel, UpdateUserWebModel>();
CreateMap<FriendServiceModel, UsernameWebModel>();
}
diff --git a/src/Web/DevHive.Web/Controllers/ProfilePictureController.cs b/src/Web/DevHive.Web/Controllers/ProfilePictureController.cs
index 2eec99e..9a76e2c 100644
--- a/src/Web/DevHive.Web/Controllers/ProfilePictureController.cs
+++ b/src/Web/DevHive.Web/Controllers/ProfilePictureController.cs
@@ -1,8 +1,12 @@
using System;
using System.Threading.Tasks;
-using DevHive.Web.Models.User;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
+using DevHive.Services.Interfaces;
+using DevHive.Services.Models.ProfilePicture;
+using DevHive.Common.Jwt.Interfaces;
+using AutoMapper;
+using DevHive.Web.Models.ProfilePicture;
namespace DevHive.Web.Controllers
{
@@ -13,36 +17,50 @@ namespace DevHive.Web.Controllers
[Route("api/[controller]")]
public class ProfilePictureController
{
- // private readonly ProfilePictureService _profilePictureService;
+ private readonly IProfilePictureService _profilePictureService;
+ private readonly IJwtService _jwtService;
+ private readonly IMapper _profilePictureMapper;
- // public ProfilePictureController(ProfilePictureService profilePictureService)
- // {
- // this._profilePictureService = profilePictureService;
- // }
+ public ProfilePictureController(IProfilePictureService profilePictureService, IJwtService jwtService, IMapper profilePictureMapper)
+ {
+ this._profilePictureService = profilePictureService;
+ this._jwtService = jwtService;
+ this._profilePictureMapper = profilePictureMapper;
+ }
+
+ /// <summary>
+ /// Get the URL of user's profile picture
+ /// </summary>
+ /// <param name="profilePictureId">The profile picture's Id</param>
+ /// <param name="authorization">JWT Bearer Token</param>
+ /// <returns>The URL of the profile picture</returns>
+ [HttpGet]
+ [AllowAnonymous]
+ public async Task<IActionResult> ReadProfilePicture(Guid profilePictureId, [FromHeader] string authorization)
+ {
+ string profilePicURL = await this._profilePictureService.GetProfilePictureById(profilePictureId);
+ return new OkObjectResult(new { ProfilePictureURL = profilePicURL} );
+ }
/// <summary>
/// Alter the profile picture of a user
/// </summary>
/// <param name="userId">The user's Id</param>
- /// <param name="updateProfilePictureWebModel">The new profile picture</param>
+ /// <param name="profilePictureWebModel">The new profile picture</param>
/// <param name="authorization">JWT Bearer Token</param>
- /// <returns>???</returns>
+ /// <returns>The URL of the new profile picture</returns>
[HttpPut]
- [Route("ProfilePicture")]
[Authorize(Roles = "User,Admin")]
- public async Task<IActionResult> UpdateProfilePicture(Guid userId, [FromForm] UpdateProfilePictureWebModel updateProfilePictureWebModel, [FromHeader] string authorization)
+ public async Task<IActionResult> UpdateProfilePicture(Guid userId, [FromForm] ProfilePictureWebModel profilePictureWebModel, [FromHeader] string authorization)
{
- throw new NotImplementedException();
- // if (!await this._userService.ValidJWT(userId, authorization))
- // return new UnauthorizedResult();
-
- // UpdateProfilePictureServiceModel updateProfilePictureServiceModel = this._userMapper.Map<UpdateProfilePictureServiceModel>(updateProfilePictureWebModel);
- // updateProfilePictureServiceModel.UserId = userId;
+ if (!this._jwtService.ValidateToken(userId, authorization))
+ return new UnauthorizedResult();
- // ProfilePictureServiceModel profilePictureServiceModel = await this._userService.UpdateProfilePicture(updateProfilePictureServiceModel);
- // ProfilePictureWebModel profilePictureWebModel = this._userMapper.Map<ProfilePictureWebModel>(profilePictureServiceModel);
+ ProfilePictureServiceModel profilePictureServiceModel = this._profilePictureMapper.Map<ProfilePictureServiceModel>(profilePictureWebModel);
+ profilePictureServiceModel.UserId = userId;
- // return new AcceptedResult("UpdateProfilePicture", profilePictureWebModel);
+ string url = await this._profilePictureService.UpdateProfilePicture(profilePictureServiceModel);
+ return new OkObjectResult(new { URL = url });
}
}
}
diff --git a/src/Web/DevHive.Web/DevHive.Web.csproj b/src/Web/DevHive.Web/DevHive.Web.csproj
index 39322ae..5b3a920 100644
--- a/src/Web/DevHive.Web/DevHive.Web.csproj
+++ b/src/Web/DevHive.Web/DevHive.Web.csproj
@@ -9,25 +9,30 @@
<AllowUntrustedCertificate>true</AllowUntrustedCertificate>
</PropertyGroup>
<ItemGroup>
- <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.3" NoWarn="NU1605"/>
- <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.3" NoWarn="NU1605"/>
- <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.3">
+ <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.4" NoWarn="NU1605"/>
+ <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.4" NoWarn="NU1605"/>
+ <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.2"/>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1"/>
<PackageReference Include="AutoMapper" Version="10.1.1"/>
- <PackageReference Include="Newtonsoft.Json" Version="12.0.3"/>
- <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.3"/>
- <PackageReference Include="SonarAnalyzer.CSharp" Version="8.19.0.28253"/>
- <PackageReference Include="NSwag.AspNetCore" Version="13.10.7"/>
- <PackageReference Include="NSwag.Generation.AspNetCore" Version="13.10.7"/>
- <PackageReference Include="NSwag.Annotations" Version="13.10.7"/>
- <PackageReference Include="NSwag.Core" Version="13.10.7"/>
- <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.1.0"/>
- <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.1.0"/>
- <PackageReference Include="NSwag.SwaggerGeneration.WebApi" Version="12.3.0"/>
+ <PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
+ <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.4"/>
+ <PackageReference Include="SonarAnalyzer.CSharp" Version="8.20.0.28934"/>
+ <PackageReference Include="NSwag.AspNetCore" Version="13.10.8"/>
+ <PackageReference Include="NSwag.Generation.AspNetCore" Version="13.10.8"/>
+ <PackageReference Include="NSwag.Annotations" Version="13.10.8"/>
+ <PackageReference Include="NSwag.Core" Version="13.10.8"/>
+ <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.1.1"/>
+ <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.1.1"/>
+ <PackageReference Include="NSwag.SwaggerGeneration.WebApi" Version="12.3.1"/>
+ <PackageReference Include="Serilog.AspNetCore" Version="4.1.0"/>
+ <PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0"/>
+ <PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3"/>
+ <PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0"/>
+ <PackageReference Include="Serilog.Enrichers.Process" Version="2.0.1"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DevHive.Web.Models\DevHive.Web.Models.csproj"/>
diff --git a/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs b/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs
index f159b69..ebec5e8 100644
--- a/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs
+++ b/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs
@@ -2,7 +2,7 @@ using System;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
-using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
namespace DevHive.Web.Middleware
{
@@ -32,11 +32,10 @@ namespace DevHive.Web.Middleware
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
- return context.Response.WriteAsync(new
- {
- context.Response.StatusCode,
- exception.Message
- }.ToString());
+ // Made to resemble the formatting of property validation errors (like [MinLength(3)])
+ string message = JsonConvert.SerializeObject(new { Error = exception.Message });
+
+ return context.Response.WriteAsync(message);
}
}
}
diff --git a/src/Web/DevHive.Web/Program.cs b/src/Web/DevHive.Web/Program.cs
index fdb6889..e7c47a9 100644
--- a/src/Web/DevHive.Web/Program.cs
+++ b/src/Web/DevHive.Web/Program.cs
@@ -1,5 +1,8 @@
+using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Configuration;
+using Serilog;
namespace DevHive.Web
{
@@ -11,11 +14,32 @@ namespace DevHive.Web
public static void Main(string[] args)
{
- CreateHostBuilder(args).Build().Run();
+ var config = new ConfigurationBuilder()
+ .AddJsonFile("appsettings.json")
+ .Build();
+
+ Log.Logger = new LoggerConfiguration()
+ .ReadFrom.Configuration(config)
+ .CreateLogger();
+
+ try
+ {
+ Log.Information("Application Starting Up");
+ CreateHostBuilder(args).Build().Run();
+ }
+ catch (Exception ex)
+ {
+ Log.Fatal(ex, "The application failed to start correctly.");
+ }
+ finally
+ {
+ Log.CloseAndFlush();
+ }
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
+ .UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(opt => opt.ListenLocalhost(HTTP_PORT));
diff --git a/src/Web/DevHive.Web/Startup.cs b/src/Web/DevHive.Web/Startup.cs
index 002c718..05a75d9 100644
--- a/src/Web/DevHive.Web/Startup.cs
+++ b/src/Web/DevHive.Web/Startup.cs
@@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using DevHive.Web.Configurations.Extensions;
using Newtonsoft.Json;
+using Serilog;
namespace DevHive.Web
{
@@ -46,7 +47,8 @@ namespace DevHive.Web
if (env.IsDevelopment())
{
- app.UseDeveloperExceptionPage();
+ app.UseExceptionHandlerMiddlewareConfiguration();
+ // app.UseDeveloperExceptionPage();
}
else
{
@@ -58,6 +60,8 @@ namespace DevHive.Web
app.UseDatabaseConfiguration();
app.UseAutoMapperConfiguration();
+ app.UseSerilogRequestLogging();
+
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
diff --git a/src/Web/DevHive.Web/appsettings.json b/src/Web/DevHive.Web/appsettings.json
index fcf9805..84d534d 100644
--- a/src/Web/DevHive.Web/appsettings.json
+++ b/src/Web/DevHive.Web/appsettings.json
@@ -5,18 +5,46 @@
"audience": ""
},
"ConnectionStrings": {
- "DEV": "Server=localhost;Port=5432;Database=API;User Id=postgres;Password=;"
+ "DEV": "Server=localhost;Port=5432;Database=DevHive_API;User Id=postgres;Password=;"
},
"Cloud": {
"cloudName": "devhive",
"apiKey": "488664116365813",
"apiSecret": ""
},
- "Logging": {
- "LogLevel": {
+ "Serilog": {
+ "Using": [],
+ "LevelSwitches": {
+ "$consoleSwitch": "Verbose",
+ "$fileSwitch": "Error"
+ },
+ "MinimumLevel": {
"Default": "Information",
- "Microsoft": "Warning",
- "Microsoft.Hosting.Lifetime": "Information"
- }
+ "Override": {
+ "Microsoft": "Warning",
+ "System": "Warning"
+ }
+ },
+ "Enrich": [
+ "FromLogContext",
+ "WithMachineName",
+ "WithProcessId",
+ "WithThreadId"
+ ],
+ "WriteTo": [
+ {
+ "Name": "Console",
+ "Args": {
+ "levelSwitch": "$consoleSwitch"
+ }
+ },
+ {
+ "Name": "File",
+ "Args": {
+ "path": "./Logs/errors.log",
+ "levelSwitch": "$fileSwitch"
+ }
+ }
+ ]
}
}