diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/CollectionController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/CollectionController.cs index a401f965..f712acf2 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/CollectionController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/CollectionController.cs @@ -25,7 +25,6 @@ public class CollectionController : ControllerBase this.database = database; } - [HttpGet("playlists/{playlistId:int}/slots")] public async Task GetPlaylistSlots(int playlistId) { @@ -67,13 +66,7 @@ public class CollectionController : ControllerBase return this.Ok(this.GetUserPlaylists(token.UserId)); } - this.Request.Body.Position = 0; - string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync(); - - - string rootElement = bodyString.Contains("levels") ? "levels" : "playlist"; // I hate lbp3 - XmlSerializer serializer = new(typeof(Playlist), new XmlRootAttribute(rootElement)); - Playlist? newPlaylist = (Playlist?)serializer.Deserialize(new StringReader(bodyString)); + Playlist? newPlaylist = await this.getPlaylistFromBody(); if (newPlaylist == null) return this.BadRequest(); @@ -96,15 +89,9 @@ public class CollectionController : ControllerBase } } - if (!string.IsNullOrWhiteSpace(newPlaylist.Name)) - { - targetPlaylist.Name = newPlaylist.Name; - } + if (!string.IsNullOrWhiteSpace(newPlaylist.Name)) targetPlaylist.Name = newPlaylist.Name; - if (!string.IsNullOrWhiteSpace(newPlaylist.Description)) - { - targetPlaylist.Description = newPlaylist.Description; - } + if (!string.IsNullOrWhiteSpace(newPlaylist.Description)) targetPlaylist.Description = newPlaylist.Description; await this.database.SaveChangesAsync(); @@ -136,18 +123,12 @@ public class CollectionController : ControllerBase if (playlistCount > ServerConfiguration.Instance.UserGeneratedContentLimits.ListsQuota) return this.BadRequest(); - this.Request.Body.Position = 0; - string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync(); - - XmlSerializer serializer = new(typeof(Playlist), new XmlRootAttribute("playlist")); - Playlist? playlist = (Playlist?)serializer.Deserialize(new StringReader(bodyString)); + Playlist? playlist = await this.getPlaylistFromBody(); if (playlist == null) return this.BadRequest(); playlist.CreatorId = token.UserId; - SanitizationHelper.SanitizeStringsInClass(playlist); - this.database.Playlists.Add(playlist); await this.database.SaveChangesAsync(); @@ -262,4 +243,19 @@ public class CollectionController : ControllerBase ) ); } + + private async Task getPlaylistFromBody() + { + this.Request.Body.Position = 0; + string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync(); + + string rootElement = bodyString.Contains("levels") ? "levels" : "playlist"; + XmlSerializer serializer = new(typeof(Playlist), new XmlRootAttribute(rootElement)); + Playlist? playlist = (Playlist?)serializer.Deserialize(new StringReader(bodyString)); + + SanitizationHelper.SanitizeStringsInClass(playlist); + + return playlist; + } + } \ No newline at end of file diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ListController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ListController.cs index 6abe73d1..aee2d425 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ListController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ListController.cs @@ -207,10 +207,10 @@ public class ListController : ControllerBase if (pageSize <= 0) return this.BadRequest(); - User? targetUser = await this.database.Users.FirstOrDefaultAsync(u => u.Username == username); - if (targetUser == null) return this.StatusCode(403, ""); + int targetUserId = await this.database.Users.Where(u => u.Username == username).Select(u => u.UserId).FirstOrDefaultAsync(); + if (targetUserId == 0) return this.StatusCode(403, ""); - IEnumerable heartedPlaylists = this.database.Playlists.Where(p => p.CreatorId == targetUser.UserId) + IEnumerable heartedPlaylists = this.database.Playlists.Where(p => p.CreatorId == targetUserId) .Skip(Math.Max(0, pageStart - 1)) .Take(Math.Min(pageSize, 30)) .AsEnumerable(); @@ -221,7 +221,7 @@ public class ListController : ControllerBase ( LbpSerializer.TaggedStringElement("favouritePlaylists", response, new Dictionary { - { "total", this.database.HeartedPlaylists.Count(p => p.UserId == targetUser.UserId) }, + { "total", this.database.HeartedPlaylists.Count(p => p.UserId == targetUserId) }, { "hint_start", pageStart + Math.Min(pageSize, 30) }, }) ); @@ -233,15 +233,14 @@ public class ListController : ControllerBase GameToken? token = await this.database.GameTokenFromRequest(this.Request); if (token == null) return this.StatusCode(403, ""); - User? user = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == token.UserId); - if (user == null) return this.BadRequest(); + string username = await this.database.UsernameFromGameToken(token); Playlist? playlist = await this.database.Playlists.FirstOrDefaultAsync(s => s.PlaylistId == playlistId); if (playlist == null) return this.NotFound(); await this.database.HeartPlaylist(token.UserId, playlist); - return await this.GetFavouritePlaylists(user.Username, 1, 30); + return await this.GetFavouritePlaylists(username, 1, 30); } [HttpPost("unfavourite/slot/{playlistId:int}")]