Implement property dependency injection for the website (#806)

* Remove most non DI usages of DbContext

* Optimize website queries and refactor startup to use top level statements

* Remove unused functions in UserEntity and SlotEntity

* Optimize LBP1 LevelTags
This commit is contained in:
Josh 2023-06-20 00:02:24 -05:00 committed by GitHub
commit e43397ac6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 333 additions and 430 deletions

View file

@ -8,14 +8,12 @@ using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Servers.Website.Types;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Logging;
using LBPUnion.ProjectLighthouse.Types.Maintenance;
using Microsoft.AspNetCore.Mvc;
namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages.Admin;
public class AdminPanelPage : BaseLayout
{
public List<ICommand> Commands = MaintenanceHelper.Commands;
public AdminPanelPage(DatabaseContext database) : base(database)
{ }
@ -40,7 +38,7 @@ public class AdminPanelPage : BaseLayout
args = command + " " + args;
string[] split = args.Split(" ");
List<LogLine> runCommand = await MaintenanceHelper.RunCommand(split);
List<LogLine> runCommand = await MaintenanceHelper.RunCommand(this.HttpContext.RequestServices, split);
return this.Redirect($"~/admin?log={CryptoHelper.ToBase64(runCommand.ToLogString())}");
}

View file

@ -1,10 +1,12 @@
@page "/debug/roomVisualizer"
@using LBPUnion.ProjectLighthouse.Database
@using LBPUnion.ProjectLighthouse.Extensions
@using LBPUnion.ProjectLighthouse.Helpers
@using LBPUnion.ProjectLighthouse.Types.Entities.Profile
@using LBPUnion.ProjectLighthouse.Types.Matchmaking.Rooms
@using LBPUnion.ProjectLighthouse.Types.Users
@model LBPUnion.ProjectLighthouse.Servers.Website.Pages.Debug.RoomVisualizerPage
@inject DatabaseContext Database
@{
Layout = "Layouts/BaseLayout";
@ -59,7 +61,7 @@
#nullable enable
if (version == GameVersion.LittleBigPlanet1 || version == GameVersion.LittleBigPlanetPSP || version == GameVersion.Unknown) continue;
FindBestRoomResponse? response = RoomHelper.FindBestRoom(null, version, null, null, null);
FindBestRoomResponse? response = RoomHelper.FindBestRoom(Database, null, version, null, null, null);
string text = response == null ? "No room found." : "Room " + response.RoomId;
<p><b>Best room for @version.ToPrettyString()</b>: @text</p>

View file

@ -29,7 +29,7 @@ public class BaseLayout : PageModel
public BaseLayout(DatabaseContext database)
{
this.Database = database;
this.NavigationItems.Add(new PageNavigationItem(BaseLayoutStrings.HeaderUsers, "/users/0", "user friends"));
this.NavigationItems.Add(new PageNavigationItem(BaseLayoutStrings.HeaderPhotos, "/photos/0", "camera"));
this.NavigationItems.Add(new PageNavigationItem(BaseLayoutStrings.HeaderSlots, "/slots/0", "globe americas"));

View file

@ -1,9 +1,11 @@
@using System.Web
@using System.IO
@using LBPUnion.ProjectLighthouse.Database
@using LBPUnion.ProjectLighthouse.Localization
@using LBPUnion.ProjectLighthouse.Servers.Website.Extensions
@using LBPUnion.ProjectLighthouse.Types.Entities.Interaction
@using LBPUnion.ProjectLighthouse.Types.Entities.Profile
@inject DatabaseContext Database
@{
string language = (string?)ViewData["Language"] ?? LocalizationManager.DefaultLang;
@ -50,7 +52,7 @@
int yourThumb = commentAndReaction.Value?.Rating ?? 0;
DateTimeOffset timestamp = DateTimeOffset.FromUnixTimeSeconds(comment.Timestamp / 1000).ToLocalTime();
StringWriter messageWriter = new();
HttpUtility.HtmlDecode(comment.GetCommentMessage(), messageWriter);
HttpUtility.HtmlDecode(comment.GetCommentMessage(Database), messageWriter);
string decodedMessage = messageWriter.ToString();
string? url = Url.RouteUrl(ViewContext.RouteData.Values);

View file

@ -1,14 +1,15 @@
@using LBPUnion.ProjectLighthouse.Database
@using LBPUnion.ProjectLighthouse.Localization
@using LBPUnion.ProjectLighthouse.Servers.Website.Extensions
@model LBPUnion.ProjectLighthouse.Types.Entities.Profile.UserEntity
@using LBPUnion.ProjectLighthouse.Types.Entities.Profile
@model UserEntity
@inject DatabaseContext Database
@{
string language = (string?)ViewData["Language"] ?? LocalizationManager.DefaultLang;
string timeZone = (string?)ViewData["TimeZone"] ?? TimeZoneInfo.Local.Id;
bool includeStatus = (bool?)ViewData["IncludeStatus"] ?? false;
await using DatabaseContext database = DatabaseContext.CreateNewInstance();
string userStatus = includeStatus ? Model.GetStatus(database).ToTranslatedString(language, timeZone) : "";
string userStatus = includeStatus ? Model.GetStatus(Database).ToTranslatedString(language, timeZone) : "";
}
<a href="/user/@Model.UserId" title="@userStatus" class="user-link">

View file

@ -4,9 +4,9 @@
@using LBPUnion.ProjectLighthouse.Types.Entities.Profile
@using LBPUnion.ProjectLighthouse.Types.Moderation.Cases
@model LBPUnion.ProjectLighthouse.Types.Entities.Moderation.ModerationCaseEntity
@inject DatabaseContext Database
@{
DatabaseContext database = DatabaseContext.CreateNewInstance();
string color = "blue";
string timeZone = (string?)ViewData["TimeZone"] ?? TimeZoneInfo.Local.Id;
@ -70,7 +70,7 @@
@if (Model.Type.AffectsLevel())
{
SlotEntity? slot = await Model.GetSlotAsync(database);
SlotEntity? slot = await Model.GetSlotAsync(Database);
if (slot != null)
{
<p><strong>Affected level:</strong> <a href="/slot/@slot.SlotId">@slot.Name</a></p>
@ -78,7 +78,7 @@
}
else if (Model.Type.AffectsUser())
{
UserEntity? user = await Model.GetUserAsync(database);
UserEntity? user = await Model.GetUserAsync(Database);
if (user != null)
{
<p><strong>Affected user:</strong> <a href="/user/@user.UserId">@user.Username</a></p>

View file

@ -7,12 +7,11 @@
@using LBPUnion.ProjectLighthouse.Types.Users
@using Microsoft.EntityFrameworkCore
@model LBPUnion.ProjectLighthouse.Types.Entities.Level.SlotEntity
@inject DatabaseContext Database
@{
UserEntity? user = (UserEntity?)ViewData["User"];
await using DatabaseContext database = DatabaseContext.CreateNewInstance();
string slotName = HttpUtility.HtmlDecode(string.IsNullOrEmpty(Model!.Name) ? "Unnamed Level" : Model.Name);
bool isMobile = (bool?)ViewData["IsMobile"] ?? false;
@ -26,8 +25,8 @@
if (user != null)
{
isQueued = await database.QueuedLevels.FirstOrDefaultAsync(h => h.SlotId == Model.SlotId && h.UserId == user.UserId) != null;
isHearted = await database.HeartedLevels.FirstOrDefaultAsync(h => h.SlotId == Model.SlotId && h.UserId == user.UserId) != null;
isQueued = await Database.QueuedLevels.AnyAsync(h => h.SlotId == Model.SlotId && h.UserId == user.UserId);
isHearted = await Database.HeartedLevels.AnyAsync(h => h.SlotId == Model.SlotId && h.UserId == user.UserId);
}
string callbackUrl = (string)ViewData["CallbackUrl"]!;
@ -81,15 +80,24 @@
}
<div class="cardStatsUnderTitle">
<i class="pink heart icon" title="Hearts"></i> <span>@Model.Hearts</span>
@{
var slotStats = await Database.Slots.Where(s => s.SlotId == Model.SlotId).Select(_ => new
{
HeartCount = Database.HeartedLevels.Count(h => h.SlotId == Model.SlotId),
ThumbsUp = Database.RatedLevels.Count(r => r.SlotId == Model.SlotId && r.Rating == 1),
ThumbsDown = Database.RatedLevels.Count(r => r.SlotId == Model.SlotId && r.Rating == -1),
RatingLbp1 = Database.RatedLevels.Where(r => r.SlotId == Model.SlotId).Average(r => (double?)r.RatingLBP1) ?? 3.0,
}).OrderBy(_ => 1).FirstAsync();
}
<i class="pink heart icon" title="Hearts"></i> <span>@slotStats.HeartCount</span>
<i class="blue play icon" title="Plays"></i> <span>@Model.PlaysUnique</span>
<i class="green thumbs up icon" title="Yays"></i> <span>@Model.Thumbsup</span>
<i class="red thumbs down icon" title="Boos"></i> <span>@Model.Thumbsdown</span>
<i class="green thumbs up icon" title="Yays"></i> <span>@slotStats.ThumbsUp</span>
<i class="red thumbs down icon" title="Boos"></i> <span>@slotStats.ThumbsDown</span>
@if (Model.GameVersion == GameVersion.LittleBigPlanet1)
{
<i class="yellow star icon" title="Star Rating"></i>
<span>@(Math.Round(Model.RatingLBP1 * 10) / 10)</span>
<span>@(Math.Round(slotStats.RatingLbp1 * 10) / 10)</span>
}
</div>
@if (Model.Creator != null)

View file

@ -1,7 +1,9 @@
@using LBPUnion.ProjectLighthouse.Database
@using LBPUnion.ProjectLighthouse.Localization
@using LBPUnion.ProjectLighthouse.Servers.Website.Extensions;
@using Microsoft.EntityFrameworkCore
@model LBPUnion.ProjectLighthouse.Types.Entities.Profile.UserEntity
@inject DatabaseContext Database
@{
bool showLink = (bool?)ViewData["ShowLink"] ?? false;
@ -42,21 +44,22 @@
</h1>
}
@{
await using DatabaseContext context = DatabaseContext.CreateNewInstance();
int hearts = Model.GetHeartCount(context);
int comments = Model.GetCommentCount(context);
int levels = Model.GetUsedSlotCount(context);
int photos = Model.GetUploadedPhotoCount(context);
var stats = await Database.Users.Where(u => u.UserId == Model.UserId).Select(_ => new
{
HeartCount = Database.HeartedProfiles.Count(hp => hp.HeartedUserId == Model.UserId),
CommentCount = Database.Comments.Count(c => c.PosterUserId == Model.UserId),
LevelCount = Database.Slots.Count(s => s.CreatorId == Model.UserId),
PhotoCount = Database.Photos.Count(p => p.CreatorId == Model.UserId),
}).OrderBy(_ => 1).FirstAsync();
}
<span>
<i>@Model.GetStatus(context).ToTranslatedString(language, timeZone)</i>
<i>@Model.GetStatus(Database).ToTranslatedString(language, timeZone)</i>
</span>
<div class="cardStatsUnderTitle">
<i class="pink heart icon" title="Hearts"></i> <span>@hearts</span>
<i class="blue comment icon" title="Comments"></i> <span>@comments</span>
<i class="green upload icon" title="Uploaded Levels"></i><span>@levels</span>
<i class="purple camera icon" title="Uploaded Photos"></i><span>@photos</span>
<i class="pink heart icon" title="Hearts"></i> <span>@stats.HeartCount</span>
<i class="blue comment icon" title="Comments"></i> <span>@stats.CommentCount</span>
<i class="green upload icon" title="Uploaded Levels"></i><span>@stats.LevelCount</span>
<i class="purple camera icon" title="Uploaded Photos"></i><span>@stats.PhotoCount</span>
</div>
</div>
</div>

View file

@ -10,7 +10,6 @@ namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages;
public class PhotosPage : BaseLayout
{
public int PageAmount;
public int PageNumber;

View file

@ -9,6 +9,7 @@
@using LBPUnion.ProjectLighthouse.Types.Moderation.Cases
@using LBPUnion.ProjectLighthouse.Types.Users
@model LBPUnion.ProjectLighthouse.Servers.Website.Pages.SlotPage
@inject DatabaseContext Database
@{
Layout = "Layouts/BaseLayout";
@ -55,7 +56,7 @@
string[] authorLabels;
if (Model.Slot?.GameVersion == GameVersion.LittleBigPlanet1)
{
authorLabels = Model.Slot.LevelTags(DatabaseContext.CreateNewInstance());
authorLabels = Model.Slot.LevelTags(Database);
}
else
{