Skip to content

Commit

Permalink
Added Vogen and strongly typed values
Browse files Browse the repository at this point in the history
Added other possible tenants
Fixed NexusModsUserName upsert
  • Loading branch information
Aragas committed Sep 5, 2023
1 parent 00cabed commit 0f19b13
Show file tree
Hide file tree
Showing 155 changed files with 2,515 additions and 750 deletions.
2 changes: 1 addition & 1 deletion src/BUTR.Site.NexusMods.Client/Models/DemoUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace BUTR.Site.NexusMods.Client.Models;
public static class DemoUser
{
private static readonly ProfileModel _profile =
new(31179975, "Pickysaurus", "demo@demo.com", "https://forums.nexusmods.com/uploads/profile/photo-31179975.png", true, true, ApplicationRoles.User, null, null, null, true);
new(31179975, "Pickysaurus", "demo@demo.com", "https://forums.nexusmods.com/uploads/profile/photo-31179975.png", true, true, ApplicationRoles.User, null, null, null, true, new Dictionary<string, string> { {"1", "Bannerlord"} });
private static readonly List<NexusModsModModel> _mods = new()
{
new(1, "Demo Mod 1", ImmutableArray<int>.Empty, ImmutableArray<int>.Empty, ImmutableArray<string>.Empty, ImmutableArray<string>.Empty),
Expand Down
2 changes: 1 addition & 1 deletion src/BUTR.Site.NexusMods.Client/Pages/Basic/Profile.razor
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ else
@<Card Shadow="@Shadow.Small">
<CardBody TextAlignment="@TextAlignment.Center">
<CardImage Width="@Width.Is50" Source="@user.ProfileUrl.Replace(@"{BASE}", _navigationManager.BaseUri)" Border="@Border.RoundedCircle" Alt="@("Profile Image")"/>
<CardTitle Size="5"><Blazorise.Link To="@user.Url">@user.Name</Blazorise.Link></CardTitle>
<CardTitle Size="5"><Blazorise.Link To="@user.Url()">@user.Name</Blazorise.Link></CardTitle>
<CardText TextColor="@TextColor.Secondary">@user.Role</CardText>
@if (user.IsPremium)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@

private async Task<bool> DoUserAllowMod(AllowUserModel model)
{
if (!NexusModsUtils.TryParse(model.UserUrl, out _, out var nexusModsId) && !ushort.TryParse(model.UserUrl, out nexusModsId))
if (!NexusModsUtils.TryParse(model.UserUrl, out _, out var nexusModsId) && !uint.TryParse(model.UserUrl, out nexusModsId))
return false;

return await _userClient.ToModuleManualLinkAsync(nexusModsId, model.ModuleId) is { HumanReadableError: var error } && string.IsNullOrEmpty(error);
return await _userClient.ToModuleManualLinkAsync((int) nexusModsId, model.ModuleId) is { HumanReadableError: var error } && string.IsNullOrEmpty(error);
}
private async Task<bool> DoUserDisallowMod(NexusModsUserToModuleManualLinkModel model)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@

private async Task<bool> DoManualLink(ManualLinkModel model)
{
if (!NexusModsUtils.TryParse(model.NexusModsUrl, out _, out var nexusModsId) && !ushort.TryParse(model.NexusModsUrl, out nexusModsId))
if (!NexusModsUtils.TryParse(model.NexusModsUrl, out _, out var nexusModsId) && !uint.TryParse(model.NexusModsUrl, out nexusModsId))
return false;

return await _modClient.ToModuleManualLinkAsync(model.ModuleId, nexusModsId) is { HumanReadableError: var error } && string.IsNullOrEmpty(error);
return await _modClient.ToModuleManualLinkAsync(model.ModuleId, (int) nexusModsId) is { HumanReadableError: var error } && string.IsNullOrEmpty(error);
}
private async Task<bool> DoManualUnlink(NexusModsModToModuleModel model)
{
Expand Down
3 changes: 2 additions & 1 deletion src/BUTR.Site.NexusMods.Client/Pages/Tools/Articles.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@page "/articles"

@inject INexusModsArticleClient _articlesClient
@inject TenantProvider _tenantProvider
@inject IJSRuntime _jsRuntime;

<Card Margin="@Margin.Is4" Overflow="@Overflow.Auto">
Expand Down Expand Up @@ -43,7 +44,7 @@
private async Task OnClick(NexusModsArticleModel? mod)
{
if (mod is not null)
await _jsRuntime.InvokeVoidAsync("open", mod.Url, "_blank");
await _jsRuntime.InvokeVoidAsync("open", mod.Url(TenantUtils.FromTenantToGameDomain(await _tenantProvider.GetTenantAsync())!), "_blank");
}

private async Task<ICollection<string>> GetAutocompleteValues(string filter) => (await _articlesClient.AutocompleteAsync(filter)).Data ?? Array.Empty<string>();
Expand Down
5 changes: 3 additions & 2 deletions src/BUTR.Site.NexusMods.Client/Pages/Tools/ExposedMods.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@page "/exposed-mods"

@inject IExposedModsClient _exposedModsClient
@inject TenantProvider _tenantProvider
@inject IJSRuntime _jsRuntime;

<Card Margin="@Margin.Is4" Overflow="@Overflow.Auto">
Expand Down Expand Up @@ -36,10 +37,10 @@
private async Task OnClick(ExposedNexusModsModModel? mod)
{
if (mod is not null)
await _jsRuntime.InvokeVoidAsync("open", mod.Url, "_blank");
await _jsRuntime.InvokeVoidAsync("open", mod.Url(TenantUtils.FromTenantToGameDomain(await _tenantProvider.GetTenantAsync())!), "_blank");
}

private async Task<ICollection<string>> GetAutocompleteValues(string filter) => (await _exposedModsClient.AutocompleteAsync(filter)).Data ?? Array.Empty<string>();
private async Task<ICollection<string>> GetAutocompleteValues(string filter) => (await _exposedModsClient.AutocompleteAsync(new(filter))).Data ?? Array.Empty<string>();

private static IEnumerable<Filtering> GetFilters(IEnumerable<DataGridColumnInfo> columnInfos)
{
Expand Down
6 changes: 3 additions & 3 deletions src/BUTR.Site.NexusMods.Client/Pages/User/AllowUserMod.razor
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@

private async Task<bool> DoUserAllowMod(AllowUserModel model)
{
if (!NexusModsUtils.TryParse(model.UserUrl, out _, out var nexusModsUserId) && !ushort.TryParse(model.UserUrl, out nexusModsUserId))
if (!NexusModsUtils.TryParse(model.UserUrl, out _, out var nexusModsUserId) && !uint.TryParse(model.UserUrl, out nexusModsUserId))
return false;

if (!NexusModsUtils.TryParse(model.ModUrl, out _, out var nexusModsModId) && !ushort.TryParse(model.ModUrl, out nexusModsModId))
if (!NexusModsUtils.TryParse(model.ModUrl, out _, out var nexusModsModId) && !uint.TryParse(model.ModUrl, out nexusModsModId))
return false;

return await _userClient.ToNexusModsModManualLinkAsync(nexusModsUserId, nexusModsModId) is { HumanReadableError: var error } && string.IsNullOrEmpty(error);
return await _userClient.ToNexusModsModManualLinkAsync((int) nexusModsUserId, (int) nexusModsModId) is { HumanReadableError: var error } && string.IsNullOrEmpty(error);
}
private async Task<bool> DoUserDisallowMod(NexusModsUserToNexusModsModManualLinkModel model)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
private async Task<PagingStreamingData<CrashReportModel>?> GetCrashReports(int page, int pageSize, ICollection<Filtering> filterings, ICollection<Sorting> sortings, CancellationToken ct = default) =>
await _crashReportsClient.PaginatedStreamingAsync(new(page, pageSize, filterings, sortings), ct);

private async Task<ICollection<string>> GetAutocompleteValues(string filter) => await _crashReportsClient.AutocompleteAsync(filter) is { Data: { } data } ? data : Array.Empty<string>();
private async Task<ICollection<string>> GetAutocompleteValues(string filter) => await _crashReportsClient.AutocompleteAsync(new(filter)) is { Data: { } data } ? data : Array.Empty<string>();

private IEnumerable<Filtering> GetFilters(IEnumerable<DataGridColumnInfo> columnInfos)
{
Expand Down
9 changes: 5 additions & 4 deletions src/BUTR.Site.NexusMods.Client/Pages/User/Mods.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
@attribute [Authorize]
@page "/mods"

@inject IJSRuntime _jsRuntime;
@inject INexusModsUserClient _userClient
@inject INexusModsModClient _modClient
@inject TenantProvider _tenantProvider
@inject IJSRuntime _jsRuntime;

<Card Margin="@Margin.Is4">
<CardHeader>
Expand Down Expand Up @@ -106,10 +107,10 @@

private async Task<bool> DoLinkMod(LinkModModel model)
{
if (!NexusModsUtils.TryParse(model.ModUrl, out _, out var modId) && !ushort.TryParse(model.ModUrl, out modId))
if (!NexusModsUtils.TryParse(model.ModUrl, out _, out var modId) && !uint.TryParse(model.ModUrl, out modId))
return false;

return await _userClient.ToNexusModsModLinkAsync(modId) is { HumanReadableError: var error } && string.IsNullOrEmpty(error);
return await _userClient.ToNexusModsModLinkAsync((int) modId) is { HumanReadableError: var error } && string.IsNullOrEmpty(error);
}

private async Task<bool> DoUnlinkMod(NexusModsModModel model)
Expand All @@ -126,7 +127,7 @@
private async Task OnClick(NexusModsModModel? mod)
{
if (mod is not null)
await _jsRuntime.InvokeVoidAsync("open", mod.Url, "_blank");
await _jsRuntime.InvokeVoidAsync("open", mod.Url(TenantUtils.FromTenantToGameDomain(await _tenantProvider.GetTenantAsync())!), "_blank");
}

private async Task OnDelete(ButtonRowContext<NexusModsModModel?> context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
{
var tenant = await _tenantProvider.GetTenantAsync();

request.Headers.Add("Tenant", ((int) tenant).ToString());
request.Headers.Add("Tenant", tenant);
return await base.SendAsync(request, ct);
}
}
10 changes: 4 additions & 6 deletions src/BUTR.Site.NexusMods.Client/Services/TenantProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Blazored.LocalStorage;

using BUTR.Site.NexusMods.Shared;

using System.Threading;
using System.Threading.Tasks;

Expand All @@ -16,13 +14,13 @@ public TenantProvider(ILocalStorageService localStorage)
_localStorage = localStorage;
}

public async Task<Tenant> GetTenantAsync()
public async Task<string> GetTenantAsync()
{
if (! await _localStorage.ContainKeyAsync("tenant"))
await SetTenantAsync(Tenant.Bannerlord);
await SetTenantAsync("1");

return await _localStorage.GetItemAsync<Tenant>("tenant", CancellationToken.None);
return await _localStorage.GetItemAsStringAsync("tenant", CancellationToken.None);
}

public async Task SetTenantAsync(Tenant tenant) => await _localStorage.SetItemAsync("tenant", tenant, CancellationToken.None);
public async Task SetTenantAsync(string tenant) => await _localStorage.SetItemAsStringAsync("tenant", tenant, CancellationToken.None);
}
54 changes: 32 additions & 22 deletions src/BUTR.Site.NexusMods.Client/Shared/Header.razor
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@
</AuthorizeView>
</BarStart>

<AuthorizeView Roles="@($"{ApplicationRoles.User},{ApplicationRoles.Moderator},{ApplicationRoles.Administrator}")">
<Authorized>
@if (_currentTenant != null)
{
<BarEnd Style="margin-left: 0!important;">
<BarItem>
<BarDropdown>
<BarDropdownToggle>
@_currentTenant
</BarDropdownToggle>
<BarDropdownMenu Background="@Background.Dark">
@foreach (var (tenant, name) in _user?.AvailableTenants ?? new Dictionary<string, string>())
{
if (_currentTenant == name) continue;

<BarDropdownItem Background="@Background.Dark">
<BarLink Clicked="async () => await SwitchTenantAsync(tenant)">@name</BarLink>
</BarDropdownItem>
}
</BarDropdownMenu>
</BarDropdown>
</BarItem>
</BarEnd>
}
</Authorized>
</AuthorizeView>

<BarEnd>
<AuthorizeView Roles="@($"{ApplicationRoles.Moderator}")">
<Authorized>
Expand Down Expand Up @@ -120,27 +147,6 @@
</BarDropdown>
</BarItem>
</BarEnd>
<AuthorizeView Roles="@($"{ApplicationRoles.User},{ApplicationRoles.Moderator},{ApplicationRoles.Administrator}")">
<Authorized>
<BarEnd Style="margin-left: 0!important;">
<BarItem>
<BarDropdown RightAligned>
<BarDropdownToggle>
Tenant
</BarDropdownToggle>
<BarDropdownMenu Background="@Background.Dark">
@foreach (var val in Enum.GetValues<Tenant>())
{
<BarDropdownItem Background="@Background.Dark">
<BarLink Clicked="async () => await SwitchTenantAsync(val)">@val</BarLink>
</BarDropdownItem>
}
</BarDropdownMenu>
</BarDropdown>
</BarItem>
</BarEnd>
</Authorized>
</AuthorizeView>
<BarEnd Style="margin-left: 0!important;">
<AuthorizeView>
<Authorized>
Expand Down Expand Up @@ -180,6 +186,7 @@

private const string FallbackIcon = "images/default_profile.webp";
private ProfileModel? _user;
private string? _currentTenant;

protected override async Task OnInitializedAsync()
{
Expand All @@ -204,9 +211,12 @@
_user = response.Data;
StateHasChanged();
}

var currentTenantId = await _tenantProvider.GetTenantAsync();
_currentTenant = TenantUtils.FromTenantToName(currentTenantId);
}

private async Task SwitchTenantAsync(Tenant value)
private async Task SwitchTenantAsync(string value)
{
await _tenantProvider.SetTenantAsync(value);
_navigationManager.NavigateTo(_navigationManager.Uri, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<PackageReference Include="SharpCompress" Version="0.33.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="Vogen" Version="3.0.21" />
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="8.0.0-preview.7.23375.4-01" />
</ItemGroup>

Expand Down
3 changes: 0 additions & 3 deletions src/BUTR.Site.NexusMods.Server/Contexts/AppDbContextWrite.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using BUTR.Site.NexusMods.Server.Models.Database;
using BUTR.Site.NexusMods.Server.Options;
using BUTR.Site.NexusMods.Server.Services;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
Expand All @@ -10,8 +9,6 @@
using System.Threading;
using System.Threading.Tasks;

using Z.BulkOperations;

namespace BUTR.Site.NexusMods.Server.Contexts;

public sealed class AppDbContextWrite : BaseAppDbContext, IAppDbContextWrite
Expand Down
4 changes: 4 additions & 0 deletions src/BUTR.Site.NexusMods.Server/Contexts/BaseAppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using BUTR.Site.NexusMods.Server.Options;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Options;

using System;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class BaseAppDbContext : DbContext
public required DbSet<CrashReportIgnoredFileEntity> CrashReportIgnoredFileIds { get; set; }

public required DbSet<NexusModsUserEntity> NexusModsUsers { get; set; }
public required DbSet<NexusModsUserToNameEntity> NexusModsUserToName { get; set; }
public required DbSet<NexusModsUserToCrashReportEntity> NexusModsUserToCrashReports { get; set; }
public required DbSet<NexusModsUserToNexusModsModEntity> NexusModsUserToNexusModsMods { get; set; }
public required DbSet<NexusModsUserToModuleEntity> NexusModsUserToModules { get; set; }
Expand Down Expand Up @@ -126,6 +128,8 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.ReplaceService<IModelCacheKeyFactory, TenantModelCacheKeyFactory>();

optionsBuilder
.UseNpgsql(IsReadOnly && !string.IsNullOrEmpty(_options.Replica) ? _options.Replica : _options.Main, opt =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public AutocompleteEntityConfiguration(ITenantContextAccessor tenantContextAcces

protected override void ConfigureModel(EntityTypeBuilder<AutocompleteEntity> builder)
{
builder.Property(x => x.AutocompleteId).HasColumnName("autocomplete_id").ValueGeneratedOnAdd();
builder.Property<int>("AutocompleteId").HasColumnName("autocomplete_id").ValueGeneratedOnAdd();
builder.Property(x => x.Type).HasColumnName("type");
builder.Property(x => x.Value).HasColumnName("value");
builder.ToTable("autocomplete", "autocomplete").HasKey(x => new { x.TenantId, x.AutocompleteId });
builder.ToTable("autocomplete", "autocomplete").HasKey(nameof(AutocompleteEntity.TenantId), "AutocompleteId");

builder.HasIndex(x => x.Type);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BUTR.Site.NexusMods.Server.Models.Database;
using BUTR.Site.NexusMods.Server.Models;
using BUTR.Site.NexusMods.Server.Models.Database;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
Expand All @@ -18,7 +19,7 @@ protected BaseEntityConfigurationWithTenant(ITenantContextAccessor tenantContext

protected override void ConfigureModel(EntityTypeBuilder<TEntity> builder)
{
builder.Property(x => x.TenantId).HasColumnName("tenant");
builder.Property(x => x.TenantId).HasColumnName("tenant").HasConversion<TenantId.EfCoreValueConverter>();
builder.HasQueryFilter(x => x.TenantId.Equals(_tenantContextAccessor.Current));

builder.HasOne<TenantEntity>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BUTR.Site.NexusMods.Server.Models.Database;
using BUTR.Site.NexusMods.Server.Models;
using BUTR.Site.NexusMods.Server.Models.Database;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
Expand All @@ -11,13 +12,13 @@ public CrashReportEntityConfiguration(ITenantContextAccessor tenantContextAccess

protected override void ConfigureModel(EntityTypeBuilder<CrashReportEntity> builder)
{
builder.Property(x => x.CrashReportId).HasColumnName("crash_report_id").ValueGeneratedNever();
builder.Property(x => x.Version).HasColumnName("version");
builder.Property(x => x.GameVersion).HasColumnName("game_version");
builder.Property<string>(nameof(ExceptionTypeEntity.ExceptionTypeId)).HasColumnName("exception_type_id");
builder.Property(x => x.CrashReportId).HasColumnName("crash_report_id").HasConversion<CrashReportId.EfCoreValueConverter>().ValueGeneratedNever();
builder.Property(x => x.Version).HasColumnName("version").HasConversion<CrashReportVersion.EfCoreValueConverter>();
builder.Property(x => x.GameVersion).HasColumnName("game_version").HasConversion<GameVersion.EfCoreValueConverter>();
builder.Property<ExceptionTypeId>(nameof(ExceptionTypeEntity.ExceptionTypeId)).HasColumnName("exception_type_id").HasConversion<ExceptionTypeId.EfCoreValueConverter>();
builder.Property(x => x.Exception).HasColumnName("exception");
builder.Property(x => x.CreatedAt).HasColumnName("created_at");
builder.Property(x => x.Url).HasColumnName("url");
builder.Property(x => x.Url).HasColumnName("url").HasConversion<CrashReportUrl.EfCoreValueConverter>();
builder.ToTable("crash_report", "crashreport").HasKey(x => new { x.TenantId, x.CrashReportId });

builder.HasOne(x => x.ExceptionType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BUTR.Site.NexusMods.Server.Models.Database;
using BUTR.Site.NexusMods.Server.Models;
using BUTR.Site.NexusMods.Server.Models.Database;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
Expand All @@ -11,7 +12,7 @@ public CrashReportIgnoredFileIdEntityConfiguration(ITenantContextAccessor tenant

protected override void ConfigureModel(EntityTypeBuilder<CrashReportIgnoredFileEntity> builder)
{
builder.Property(x => x.Value).HasColumnName("crash_report_file_ignored_id").ValueGeneratedNever();
builder.Property(x => x.Value).HasColumnName("crash_report_file_ignored_id").HasConversion<CrashReportFileId.EfCoreValueConverter>().ValueGeneratedNever();
builder.ToTable("crash_report_file_ignored", "crashreport").HasKey(x => new { x.TenantId, x.Value });

base.ConfigureModel(builder);
Expand Down
Loading

0 comments on commit 0f19b13

Please sign in to comment.