Skip to content

Commit

Permalink
Performance improvement for mini details page.
Browse files Browse the repository at this point in the history
  • Loading branch information
anaximander23 committed Jan 13, 2020
1 parent 81be349 commit 288328f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 38 deletions.
26 changes: 10 additions & 16 deletions MiniIndex/Pages/Minis/Details.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page
@using MiniIndex.Models;
@model MiniIndex.Pages.Minis.DetailsModel
@inject SignInManager<IdentityUser> SignInManager
@{
Expand Down Expand Up @@ -42,8 +43,6 @@
<h2>@Html.DisplayFor(model => model.Mini.Name) by <a href="/Creators/[email protected]">@Html.DisplayFor(model => model.Mini.Creator.Name)</a></h2>
<hr />



<div class="row">
<div class="col-md-4" align="center">
<div class="row">
Expand Down Expand Up @@ -127,7 +126,6 @@
}
</div>
<div id="AddedTags">

</div>
</div>
</div>
Expand All @@ -147,19 +145,18 @@

<div id="UnusedTags">

@if (User.IsInRole("Moderator") && Model.SimilarTags.Count > 0)
@if (User.IsInRole("Moderator") && Model.RecommendedTags.Any())
{
<div class="add-tag-div">
<h3 class="add-tag-header">Recommended Tags</h3>
@foreach (var Tag in Model.SimilarTags)
@foreach (var Tag in Model.RecommendedTags)
{
<a href="#" id="@Tag.ID" class="btn btn-outline-success add-tag" style="margin-top:5px;">+ @Tag.TagName</a>
}
</div>
<hr/>
<hr />
}


<div class="add-tag-div">
<h3 class="add-tag-header">Genre</h3>
@foreach (var Tag in Model.UnusedTags.Where(m => m.Category.ToString() == "Genre"))
Expand All @@ -179,12 +176,12 @@
@{var PreviousCategory = "";}
<div class="add-tag-div">


@foreach (var Tag in Model.UnusedTags.Where(m => !string.IsNullOrEmpty(m.Category.ToString())
&& m.Category.ToString() != "Genre"
&& m.Category.ToString() != "OtherDescription"
&& m.Category.ToString() != "CreatureName"
&& m.Category.ToString() != "Use"))
@foreach (var Tag in Model.UnusedTags
.Where(m => !string.IsNullOrEmpty(m.Category.ToString())
&& m.Category != TagCategory.Genre
&& m.Category != TagCategory.OtherDescription
&& m.Category != TagCategory.CreatureName
&& m.Category != TagCategory.Use))
{
@if (Tag.Category.ToString() != PreviousCategory)
{
Expand Down Expand Up @@ -219,10 +216,7 @@
}
</div>
<a href="#" id="AddNewTag" class="btn btn-success" style="margin-top:5px;">+ Add as new tag</a>

</div>
</div>
}


</div>
26 changes: 4 additions & 22 deletions MiniIndex/Pages/Minis/Details.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public DetailsModel(UserManager<IdentityUser> userManager, MiniIndexContext cont
public List<Tag> UnusedTags { get; set; }
public List<Tag> MiscTags { get; set; }
public List<Tag> WordMatchingTags { get; set; }
public List<Tag> SimilarTags { get; set; }
public List<Tag> RecommendedTags { get; set; }
public bool IsStarred { get; set; }

public async Task<IActionResult> OnGetAsync(int? id)
Expand Down Expand Up @@ -73,29 +73,11 @@ public async Task<IActionResult> OnGetAsync(int? id)

if (User.IsInRole("Moderator"))
{
string[] NameSplit = Mini.Name.ToLower().Split(' ');
string[] nameSplit = Mini.Name.ToUpperInvariant().Split(' ');

WordMatchingTags = _context.Tag
.Where(t => NameSplit.Contains(t.TagName.ToLower()))
RecommendedTags = UnusedTags
.Where(t => nameSplit.Contains(t.TagName.ToUpperInvariant()))
.ToList();

//TODO - I'm sure someone who knows LINQ could clean this up a lot.
if (WordMatchingTags.Count > 0)
{
List<Mini> SimilarMinis = _context.Mini
.Include(m => m.MiniTags)
.ThenInclude(mt => mt.Tag)
.AsEnumerable()
.Where(m => m.MiniTags.Select(mt => mt.Tag).Intersect(WordMatchingTags).Any())
.ToList();

List<MiniTag> SimilarMiniTags = SimilarMinis.SelectMany(m => m.MiniTags).ToList();
SimilarTags = SimilarMiniTags.GroupBy(mt => mt.Tag).Select(grp => grp.First().Tag).Except(Mini.MiniTags.Select(mt => mt.Tag)).ToList();
}
else
{
SimilarTags = new List<Tag>();
}
}

return Page();
Expand Down

0 comments on commit 288328f

Please sign in to comment.