Skip to content

Commit

Permalink
Backend Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasAunvik committed Apr 22, 2024
1 parent 2760cdd commit c593446
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 61 deletions.
3 changes: 2 additions & 1 deletion Application/Skills/Commands/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public async Task<SkillDto> Handle(Command request, CancellationToken cancellati
Name = request.Name,
Description = request.Description
}
]
],
Detail = new SkillDetail(),
};

await _context.Skills.AddAsync(skill, cancellationToken);
Expand Down
6 changes: 1 addition & 5 deletions Application/Skills/Commands/Edit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ public async Task<SkillDto> 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();
}
Expand Down
10 changes: 3 additions & 7 deletions Azure/AzureConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.Storage.Blobs;
using Microsoft.Extensions.Azure;
Expand Down Expand Up @@ -39,17 +40,12 @@ string sasToken

public static IServiceCollection RegisterAzureBlobServices(
this IServiceCollection serviceCollection,
string blobAccountName,
string blobSasToken
string blobConnectionString
) {
serviceCollection.AddScoped<AzureUpload>();
serviceCollection.AddAzureClients(clientBuilder =>
{
clientBuilder.AddBlobServiceClient(
GetBlobServiceConnectionString(blobAccountName, blobSasToken)
);
clientBuilder.UseCredential(new DefaultAzureCredential());
clientBuilder.AddBlobServiceClient(blobConnectionString);
});
return serviceCollection;
}
Expand Down
2 changes: 1 addition & 1 deletion Web/Controllers/ModeratorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task<ActionResult<List<ModeratorDto>>> AddMod(ModeratorCreate.Comma
}

[HttpPut("{id}")]
public async Task<ActionResult<List<ModeratorDto>>> AddMod(int id, ModeratorEdit.Command command)
public async Task<ActionResult<List<ModeratorDto>>> EditMod(int id, ModeratorEdit.Command command)
{
command.ModeratorId = id;
return await Mediator.Send(command);
Expand Down
1 change: 1 addition & 0 deletions Web/Controllers/SkillController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public async Task<ActionResult<SkillDto>> 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<ActionResult<SkillDto>> EditSkill(int id, SkillEdit.Command skill)
{
Expand Down
8 changes: 8 additions & 0 deletions Web/Objects/DiscordUserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
38 changes: 18 additions & 20 deletions Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliveBot.Database;
using NextjsStaticHosting.AspNetCore;
using Serilog;
using Microsoft.AspNetCore.Authentication.Cookies;
using CliveBot.Web.Events;
Expand Down Expand Up @@ -46,43 +45,46 @@
builder.Services.RegisterMediatR();

var azureConfig = builder.Configuration.GetSection("Azure");
var blobAccountName = azureConfig.GetValue<string>("BlobAccountName") ?? "";
var blobSasToken = azureConfig.GetValue<string>("BlobSasToken") ?? "";
var blobConnectionString = azureConfig.GetValue<string>("BlobConnectionString") ?? "";

builder.Services.RegisterAzureBlobServices(
blobAccountName, blobSasToken
);
builder.Services.RegisterAzureBlobServices(blobConnectionString);

var discordLogin = builder.Configuration.GetSection("DiscordLogin");
var discordClientId = discordLogin.GetValue<string>("ClientId") ?? "";
var discordClientSecret = discordLogin.GetValue<string>("ClientSecret") ?? "";

var cookies = builder.Configuration.GetSection("Cookies");
var cookieDomain = cookies.GetValue<string>("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 =>
{
o.AddCustomPolicies();
});
builder.Services.AddPolicyHandlers();

builder.Services.Configure<NextjsStaticHostingOptions>(
builder.Configuration.GetSection("NextjsStaticHosting")
);
builder.Services.AddNextjsStaticHosting();

builder.Services.AddControllers();

builder.Services.AddSwaggerGen(options =>
Expand Down Expand Up @@ -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";
Expand All @@ -149,9 +147,9 @@
options.RoutePrefix = "api/swagger";
});

app.MapNextjsStaticHtmls();
app.UseRouting();

app.UseNextjsStaticHosting();
app.MapControllers();

try
{
Expand Down
29 changes: 2 additions & 27 deletions Web/Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
<UserSecretsId>6e72770f-ea7b-4f5e-8e14-9a8d17fdaf06</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand All @@ -31,8 +32,8 @@
<PackageReference Include="AspNet.Security.OAuth.Discord" Version="8.0.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.ApplicationInsights.PerfCounterCollector" Version="2.22.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.4" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.22.0" />
<PackageReference Include="NextjsStaticHosting.AspNetCore" Version="0.9.3-preview" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
Expand All @@ -44,30 +45,4 @@
<ProjectReference Include="..\Azure\Azure.csproj" />
<ProjectReference Include="..\Database\Database.csproj" />
</ItemGroup>

<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
<!-- Ensure Node.js is installed -->
<Exec Command="node --version" ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
</Exec>
<Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
<Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
</Target>

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npx next build" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npx next export" />

<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)out\**" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
</Project>

0 comments on commit c593446

Please sign in to comment.