From c59344607888f69b7673604a7a62b69ba597234b Mon Sep 17 00:00:00 2001 From: Thomas Aunvik Date: Mon, 22 Apr 2024 23:59:25 +0200 Subject: [PATCH] Backend Changes --- Application/Skills/Commands/Create.cs | 3 +- Application/Skills/Commands/Edit.cs | 6 +--- Azure/AzureConfiguration.cs | 10 ++----- Web/Controllers/ModeratorController.cs | 2 +- Web/Controllers/SkillController.cs | 1 + Web/Objects/DiscordUserDto.cs | 8 ++++++ Web/Program.cs | 38 ++++++++++++-------------- Web/Web.csproj | 29 ++------------------ 8 files changed, 36 insertions(+), 61 deletions(-) diff --git a/Application/Skills/Commands/Create.cs b/Application/Skills/Commands/Create.cs index c4321f1..e8f2428 100644 --- a/Application/Skills/Commands/Create.cs +++ b/Application/Skills/Commands/Create.cs @@ -52,7 +52,8 @@ public async Task Handle(Command request, CancellationToken cancellati Name = request.Name, Description = request.Description } - ] + ], + Detail = new SkillDetail(), }; await _context.Skills.AddAsync(skill, cancellationToken); diff --git a/Application/Skills/Commands/Edit.cs b/Application/Skills/Commands/Edit.cs index a24c2b1..c5e50a3 100644 --- a/Application/Skills/Commands/Edit.cs +++ b/Application/Skills/Commands/Edit.cs @@ -71,11 +71,7 @@ public async Task Handle(Command request, CancellationToken cancellati skill.IconUrl = request.IconUrl; skill.PreviewImageUrl = request.PreviewImageUrl; - var result = await _context.SaveChangesAsync(cancellationToken); - if (result == 0) - { - throw new RestException(HttpStatusCode.InternalServerError, "Database failed to save data"); - } + await _context.SaveChangesAsync(cancellationToken); return skill.ConvertDto(); } diff --git a/Azure/AzureConfiguration.cs b/Azure/AzureConfiguration.cs index 58c188c..50afdb0 100644 --- a/Azure/AzureConfiguration.cs +++ b/Azure/AzureConfiguration.cs @@ -1,4 +1,5 @@ using Azure; +using Azure.Core; using Azure.Identity; using Azure.Storage.Blobs; using Microsoft.Extensions.Azure; @@ -39,17 +40,12 @@ string sasToken public static IServiceCollection RegisterAzureBlobServices( this IServiceCollection serviceCollection, - string blobAccountName, - string blobSasToken + string blobConnectionString ) { serviceCollection.AddScoped(); serviceCollection.AddAzureClients(clientBuilder => { - clientBuilder.AddBlobServiceClient( - GetBlobServiceConnectionString(blobAccountName, blobSasToken) - ); - - clientBuilder.UseCredential(new DefaultAzureCredential()); + clientBuilder.AddBlobServiceClient(blobConnectionString); }); return serviceCollection; } diff --git a/Web/Controllers/ModeratorController.cs b/Web/Controllers/ModeratorController.cs index 384f04b..4f3768d 100644 --- a/Web/Controllers/ModeratorController.cs +++ b/Web/Controllers/ModeratorController.cs @@ -23,7 +23,7 @@ public async Task>> AddMod(ModeratorCreate.Comma } [HttpPut("{id}")] - public async Task>> AddMod(int id, ModeratorEdit.Command command) + public async Task>> EditMod(int id, ModeratorEdit.Command command) { command.ModeratorId = id; return await Mediator.Send(command); diff --git a/Web/Controllers/SkillController.cs b/Web/Controllers/SkillController.cs index c52f90b..775a8c3 100644 --- a/Web/Controllers/SkillController.cs +++ b/Web/Controllers/SkillController.cs @@ -74,6 +74,7 @@ public async Task> EditSkill(SkillCreate.Command skill) [HttpPut("{id}")] [ModAuthorize(ManageSkillInfo: true)] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(SkillDto))] + [ProducesResponseType(StatusCodes.Status304NotModified, Type = typeof(SkillDto))] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task> EditSkill(int id, SkillEdit.Command skill) { diff --git a/Web/Objects/DiscordUserDto.cs b/Web/Objects/DiscordUserDto.cs index 33c1f02..6b9538c 100644 --- a/Web/Objects/DiscordUserDto.cs +++ b/Web/Objects/DiscordUserDto.cs @@ -7,4 +7,12 @@ public class DiscordUserDto public required string Discriminator { get; set; } public string? Avatar { get; set; } } + + public class DiscordRequestUser + { + public required string Id { get; set; } + public required string Username { get; set; } + public required string Email { get; set; } + public string? Avatar { get; set; } + } } diff --git a/Web/Program.cs b/Web/Program.cs index 8ab805d..9f12736 100644 --- a/Web/Program.cs +++ b/Web/Program.cs @@ -1,5 +1,4 @@ using CliveBot.Database; -using NextjsStaticHosting.AspNetCore; using Serilog; using Microsoft.AspNetCore.Authentication.Cookies; using CliveBot.Web.Events; @@ -46,31 +45,39 @@ builder.Services.RegisterMediatR(); var azureConfig = builder.Configuration.GetSection("Azure"); -var blobAccountName = azureConfig.GetValue("BlobAccountName") ?? ""; -var blobSasToken = azureConfig.GetValue("BlobSasToken") ?? ""; +var blobConnectionString = azureConfig.GetValue("BlobConnectionString") ?? ""; -builder.Services.RegisterAzureBlobServices( - blobAccountName, blobSasToken -); +builder.Services.RegisterAzureBlobServices(blobConnectionString); var discordLogin = builder.Configuration.GetSection("DiscordLogin"); var discordClientId = discordLogin.GetValue("ClientId") ?? ""; var discordClientSecret = discordLogin.GetValue("ClientSecret") ?? ""; +var cookies = builder.Configuration.GetSection("Cookies"); +var cookieDomain = cookies.GetValue("Domain"); + builder.Services.AddAuthentication( - CookieAuthenticationDefaults.AuthenticationScheme - ).AddDiscord(options => { + CookieAuthenticationDefaults.AuthenticationScheme + ) + .AddDiscord(options => + { options.AccessDeniedPath = "/error/accessdenied"; options.ClientId = discordClientId; options.ClientSecret = discordClientSecret; }) - .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, (options) => { + .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, (options) => + { options.EventsType = typeof(XCookieAuthEvents); options.AccessDeniedPath = "/error/accessdenied"; options.Cookie.SameSite = SameSiteMode.Strict; options.Cookie.SecurePolicy = CookieSecurePolicy.Always; options.Cookie.HttpOnly = false; // Needs for JavaScript + if (cookieDomain != null) + { + options.Cookie.Domain = cookieDomain; + } }); + //.AddBearerToken(); builder.Services.AddAuthorization(o => { @@ -78,11 +85,6 @@ }); builder.Services.AddPolicyHandlers(); -builder.Services.Configure( - builder.Configuration.GetSection("NextjsStaticHosting") -); -builder.Services.AddNextjsStaticHosting(); - builder.Services.AddControllers(); builder.Services.AddSwaggerGen(options => @@ -133,12 +135,8 @@ app.UseHttpsRedirection(); app.UseHealthChecks("/healthz"); -app.UseRouting(); - app.UseAuthorization(); -app.MapControllers(); - app.UseSwagger(options => { options.RouteTemplate = "api/swagger/{documentname}/swagger.json"; @@ -149,9 +147,9 @@ options.RoutePrefix = "api/swagger"; }); -app.MapNextjsStaticHtmls(); +app.UseRouting(); -app.UseNextjsStaticHosting(); +app.MapControllers(); try { diff --git a/Web/Web.csproj b/Web/Web.csproj index 7a56015..aac806f 100644 --- a/Web/Web.csproj +++ b/Web/Web.csproj @@ -12,6 +12,7 @@ true $(NoWarn);1591 + 6e72770f-ea7b-4f5e-8e14-9a8d17fdaf06 @@ -31,8 +32,8 @@ + - @@ -44,30 +45,4 @@ - - - - - - - - - - - - - - - - - - - - - - %(DistFiles.Identity) - PreserveNewest - - -