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;
}
[HttpGet("playlists/{playlistId:int}/slots")]
public async Task<IActionResult> 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<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;
}
}