Changes from self review

This commit is contained in:
Slendy 2022-09-24 19:24:38 -05:00
parent fbe203d84a
commit af0e5bd424
No known key found for this signature in database
GPG key ID: 7288D68361B91428
2 changed files with 25 additions and 30 deletions

View file

@ -25,7 +25,6 @@ public class CollectionController : ControllerBase
this.database = database; this.database = database;
} }
[HttpGet("playlists/{playlistId:int}/slots")] [HttpGet("playlists/{playlistId:int}/slots")]
public async Task<IActionResult> GetPlaylistSlots(int playlistId) public async Task<IActionResult> GetPlaylistSlots(int playlistId)
{ {
@ -67,13 +66,7 @@ public class CollectionController : ControllerBase
return this.Ok(this.GetUserPlaylists(token.UserId)); return this.Ok(this.GetUserPlaylists(token.UserId));
} }
this.Request.Body.Position = 0; Playlist? newPlaylist = await this.getPlaylistFromBody();
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));
if (newPlaylist == null) return this.BadRequest(); if (newPlaylist == null) return this.BadRequest();
@ -96,15 +89,9 @@ public class CollectionController : ControllerBase
} }
} }
if (!string.IsNullOrWhiteSpace(newPlaylist.Name)) if (!string.IsNullOrWhiteSpace(newPlaylist.Name)) targetPlaylist.Name = newPlaylist.Name;
{
targetPlaylist.Name = newPlaylist.Name;
}
if (!string.IsNullOrWhiteSpace(newPlaylist.Description)) if (!string.IsNullOrWhiteSpace(newPlaylist.Description)) targetPlaylist.Description = newPlaylist.Description;
{
targetPlaylist.Description = newPlaylist.Description;
}
await this.database.SaveChangesAsync(); await this.database.SaveChangesAsync();
@ -136,18 +123,12 @@ public class CollectionController : ControllerBase
if (playlistCount > ServerConfiguration.Instance.UserGeneratedContentLimits.ListsQuota) return this.BadRequest(); if (playlistCount > ServerConfiguration.Instance.UserGeneratedContentLimits.ListsQuota) return this.BadRequest();
this.Request.Body.Position = 0; Playlist? playlist = await this.getPlaylistFromBody();
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));
if (playlist == null) return this.BadRequest(); if (playlist == null) return this.BadRequest();
playlist.CreatorId = token.UserId; playlist.CreatorId = token.UserId;
SanitizationHelper.SanitizeStringsInClass(playlist);
this.database.Playlists.Add(playlist); this.database.Playlists.Add(playlist);
await this.database.SaveChangesAsync(); await this.database.SaveChangesAsync();
@ -262,4 +243,19 @@ public class CollectionController : ControllerBase
) )
); );
} }
private async Task<Playlist?> 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;
}
} }

View file

@ -207,10 +207,10 @@ public class ListController : ControllerBase
if (pageSize <= 0) return this.BadRequest(); if (pageSize <= 0) return this.BadRequest();
User? targetUser = await this.database.Users.FirstOrDefaultAsync(u => u.Username == username); int targetUserId = await this.database.Users.Where(u => u.Username == username).Select(u => u.UserId).FirstOrDefaultAsync();
if (targetUser == null) return this.StatusCode(403, ""); if (targetUserId == 0) return this.StatusCode(403, "");
IEnumerable<Playlist> heartedPlaylists = this.database.Playlists.Where(p => p.CreatorId == targetUser.UserId) IEnumerable<Playlist> heartedPlaylists = this.database.Playlists.Where(p => p.CreatorId == targetUserId)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.AsEnumerable(); .AsEnumerable();
@ -221,7 +221,7 @@ public class ListController : ControllerBase
( (
LbpSerializer.TaggedStringElement("favouritePlaylists", response, new Dictionary<string, object> LbpSerializer.TaggedStringElement("favouritePlaylists", response, new Dictionary<string, object>
{ {
{ "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) }, { "hint_start", pageStart + Math.Min(pageSize, 30) },
}) })
); );
@ -233,15 +233,14 @@ public class ListController : ControllerBase
GameToken? token = await this.database.GameTokenFromRequest(this.Request); GameToken? token = await this.database.GameTokenFromRequest(this.Request);
if (token == null) return this.StatusCode(403, ""); if (token == null) return this.StatusCode(403, "");
User? user = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == token.UserId); string username = await this.database.UsernameFromGameToken(token);
if (user == null) return this.BadRequest();
Playlist? playlist = await this.database.Playlists.FirstOrDefaultAsync(s => s.PlaylistId == playlistId); Playlist? playlist = await this.database.Playlists.FirstOrDefaultAsync(s => s.PlaylistId == playlistId);
if (playlist == null) return this.NotFound(); if (playlist == null) return this.NotFound();
await this.database.HeartPlaylist(token.UserId, playlist); 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}")] [HttpPost("unfavourite/slot/{playlistId:int}")]