ProjectLighthouse/ProjectLighthouse.Servers.Website/Pages/SlotsPage.cshtml.cs
Josh fdf1988a34
Implement online story features and photos taken in levels (#389)
* Initial commit to support developer slots

* Remove hearting story levels, prevent race condition in adding dev slots, and remove LastContactHelper local db object.

* Fix photos taken in pod showing wrong level.

* Add support for pod and create mode photos

* Add time display to photos and added photo display to level page

* Add pagination to in game photos

* Update in pod description

* Fix migration

* Adjust wording of photos taken on local slots

* Set slot default type to User

Fixes old slots being set to developer slots

* Apply suggestions

* Add player count to developer slots

Co-authored-by: Jayden <jvyden@jvyden.xyz>
2022-08-01 19:46:29 +00:00

81 lines
No EOL
3 KiB
C#

#nullable enable
using System.Text;
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Levels;
using LBPUnion.ProjectLighthouse.PlayerData;
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages;
public class SlotsPage : BaseLayout
{
public int PageAmount;
public int PageNumber;
public int SlotCount;
public List<Slot> Slots = new();
public string? SearchValue;
public SlotsPage(Database database) : base(database)
{}
public async Task<IActionResult> OnGet([FromRoute] int pageNumber, [FromQuery] string? name)
{
if (string.IsNullOrWhiteSpace(name)) name = "";
string? targetAuthor = null;
GameVersion? targetGame = null;
StringBuilder finalSearch = new();
foreach (string part in name.Split(" "))
{
if (part.Contains("by:"))
{
targetAuthor = part.Replace("by:", "");
}
else if (part.Contains("game:"))
{
if (part.Contains('1')) targetGame = GameVersion.LittleBigPlanet1;
else if (part.Contains('2')) targetGame = GameVersion.LittleBigPlanet2;
else if (part.Contains('3')) targetGame = GameVersion.LittleBigPlanet3;
else if (part.Contains('v')) targetGame = GameVersion.LittleBigPlanetVita;
}
else
{
finalSearch.Append(part);
}
}
this.SearchValue = name.Trim();
this.SlotCount = await this.Database.Slots.Include(p => p.Creator)
.Where(p => p.Type == SlotType.User)
.Where(p => p.Name.Contains(finalSearch.ToString()))
.Where(p => p.Creator != null && (targetAuthor == null || string.Equals(p.Creator.Username.ToLower(), targetAuthor.ToLower())))
.Where(p => targetGame == null || p.GameVersion == targetGame)
.CountAsync();
this.PageNumber = pageNumber;
this.PageAmount = Math.Max(1, (int)Math.Ceiling((double)this.SlotCount / ServerStatics.PageSize));
if (this.PageNumber < 0 || this.PageNumber >= this.PageAmount) return this.Redirect($"/slots/{Math.Clamp(this.PageNumber, 0, this.PageAmount - 1)}");
this.Slots = await this.Database.Slots.Include(p => p.Creator)
.Where(p => p.Type == SlotType.User)
.Where(p => p.Name.Contains(finalSearch.ToString()))
.Where(p => p.Creator != null && (targetAuthor == null || string.Equals(p.Creator.Username.ToLower(), targetAuthor.ToLower())))
.Where(p => targetGame == null || p.GameVersion == targetGame)
.OrderByDescending(p => p.FirstUploaded)
.Skip(pageNumber * ServerStatics.PageSize)
.Take(ServerStatics.PageSize)
.ToListAsync();
return this.Page();
}
}