Skip to content

Commit

Permalink
Use out param for Is{Submission,Publication}Page extensions (#1938)
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshiRulz authored Aug 11, 2024
1 parent 927b758 commit 433f9cc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 44 deletions.
44 changes: 12 additions & 32 deletions TASVideos.Core/Extensions/WikiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public static bool UserCanEditWikiPage(string? pageName, string? userName, IRead
|| userPermissions.Contains(PermissionTo.EditPublicationMetaData);
}

if (IsPublicationPage(pageName).HasValue)
if (IsPublicationPage(pageName, out _))
{
return userPermissions.Contains(PermissionTo.EditPublicationMetaData);
}

if (IsSubmissionPage(pageName).HasValue)
if (IsSubmissionPage(pageName, out _))
{
return userPermissions.Contains(PermissionTo.EditSubmissions);
}
Expand Down Expand Up @@ -184,46 +184,26 @@ public static bool IsGameResourcesPage(string? pageName)
&& pageName.StartsWith("GameResources/");
}

public static int? IsPublicationPage(string? pageName)
public static bool IsPublicationPage(string? pageName, out int id)
{
if (string.IsNullOrWhiteSpace(pageName))
{
return null;
}

if (pageName.StartsWith(LinkConstants.PublicationWikiPage))
if (!string.IsNullOrWhiteSpace(pageName) && pageName.StartsWith(LinkConstants.PublicationWikiPage))
{
var result = int.TryParse(
pageName.Replace(LinkConstants.PublicationWikiPage, ""), out int id);

if (result)
{
return id;
}
return int.TryParse(pageName[LinkConstants.PublicationWikiPage.Length..], out id);
}

return null;
id = default;
return false;
}

public static int? IsSubmissionPage(string? pageName)
public static bool IsSubmissionPage(string? pageName, out int id)
{
if (string.IsNullOrWhiteSpace(pageName))
{
return null;
}

if (pageName.StartsWith(LinkConstants.SubmissionWikiPage))
if (!string.IsNullOrWhiteSpace(pageName) && pageName.StartsWith(LinkConstants.SubmissionWikiPage))
{
var result = int.TryParse(
pageName.Replace(LinkConstants.SubmissionWikiPage, ""), out int id);

if (result)
{
return id;
}
return int.TryParse(pageName[LinkConstants.SubmissionWikiPage.Length..], out id);
}

return null;
id = default;
return false;
}

/// <summary>
Expand Down
14 changes: 6 additions & 8 deletions TASVideos/WikiModules/Frames.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ public async Task<IViewComponentResult> InvokeAsync(IWikiPage? pageData, double?

private async ValueTask<double> GuessFps(string? pageName)
{
var submissionId = WikiHelper.IsSubmissionPage(pageName);
if (submissionId.HasValue)
if (WikiHelper.IsSubmissionPage(pageName, out var submissionId))
{
var sub = await db.Submissions
.Where(s => s.Id == submissionId.Value)
.Where(s => s.Id == submissionId)
.Select(s => new { s.Id, s.SystemFrameRate!.FrameRate })
.SingleOrDefaultAsync(s => s.Id == submissionId.Value);
.SingleOrDefaultAsync(s => s.Id == submissionId);

if (sub?.FrameRate is not null)
{
Expand All @@ -51,17 +50,16 @@ private async ValueTask<double> GuessFps(string? pageName)
return 60;
}

var publicationId = WikiHelper.IsPublicationPage(pageName);
if (publicationId.HasValue)
if (WikiHelper.IsPublicationPage(pageName, out var publicationId))
{
var key = CacheKey + publicationId.Value;
var key = $"{CacheKey}{publicationId}";
if (cache.TryGetValue(key, out double frameRate))
{
return frameRate;
}

var pub = await db.Publications
.Where(p => p.Id == publicationId.Value)
.Where(p => p.Id == publicationId)
.Select(p => new { p.Id, p.SystemFrameRate!.FrameRate })
.SingleOrDefaultAsync();

Expand Down
22 changes: 18 additions & 4 deletions tests/TASVideos.Test/Extensions/WikiHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,15 @@ public void IsGameResourcesPage(string pageName, bool expected)
[DataRow("InternalSystem/PublicationContent/MNotANumber", null)]
public void IsPublicationPage(string pageName, int? expected)
{
var actual = WikiHelper.IsPublicationPage(pageName);
Assert.AreEqual(expected, actual);
if (expected is null)
{
Assert.IsFalse(WikiHelper.IsPublicationPage(pageName, out _), "success");
}
else
{
Assert.IsTrue(WikiHelper.IsPublicationPage(pageName, out var actual), "success");
Assert.AreEqual(expected, actual, "match");
}
}

[TestMethod]
Expand All @@ -213,8 +220,15 @@ public void IsPublicationPage(string pageName, int? expected)
[DataRow("InternalSystem/SubmissionContent/SNotANumber", null)]
public void IsSubmissionPage(string pageName, int? expected)
{
var actual = WikiHelper.IsSubmissionPage(pageName);
Assert.AreEqual(expected, actual);
if (expected is null)
{
Assert.IsFalse(WikiHelper.IsSubmissionPage(pageName, out _), "success");
}
else
{
Assert.IsTrue(WikiHelper.IsSubmissionPage(pageName, out var actual), "success");
Assert.AreEqual(expected, actual, "match");
}
}

[TestMethod]
Expand Down

0 comments on commit 433f9cc

Please sign in to comment.