Skip to content

Commit

Permalink
Fixup album update parameter bug
Browse files Browse the repository at this point in the history
  • Loading branch information
davewalker5 committed Nov 15, 2023
1 parent abe14d3 commit c16565f
Show file tree
Hide file tree
Showing 34 changed files with 802 additions and 75 deletions.
7 changes: 6 additions & 1 deletion src/MusicCatalogue.Api/Controllers/AlbumsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace MusicCatalogue.Api.Controllers
[Route("[controller]")]
public class AlbumsController : Controller
{
private const string OtherGenre = "Other";

private readonly IMusicCatalogueFactory _factory;

public AlbumsController(IMusicCatalogueFactory factory)
Expand Down Expand Up @@ -77,13 +79,16 @@ public async Task<ActionResult<IEnumerable<Album>>> GetAlbumsByArtistAsync(int a
[Route("")]
public async Task<ActionResult<Album>> UpdateAlbumAsync([FromBody] Album template)
{
// Make sure the "other" Genre exists as a fallback for album updates where no genre is given
var otherGenre = _factory.Genres.AddAsync(OtherGenre);

// Attempt the update
var album = await _factory.Albums.UpdateAsync(
template.Id,
template.ArtistId,
template.GenreId ?? otherGenre.Id,
template.Title,
template.Released,
template.Genre,
template.CoverUrl,
template.IsWishListItem,
template.Purchased,
Expand Down
6 changes: 3 additions & 3 deletions src/MusicCatalogue.Api/MusicCatalogue.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ReleaseVersion>1.14.0.0</ReleaseVersion>
<FileVersion>1.14.0.0</FileVersion>
<ProductVersion>1.14.0</ProductVersion>
<ReleaseVersion>1.15.0.0</ReleaseVersion>
<FileVersion>1.15.0.0</FileVersion>
<ProductVersion>1.15.0</ProductVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
47 changes: 47 additions & 0 deletions src/MusicCatalogue.Data/MigrationUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Reflection;

namespace MusicCatalogue.Data
{
public static class MigrationUtilities
{
/// <summary>
/// Get the namespace for the migration SQL scripts
/// </summary>
/// <returns></returns>
public static string GetSqlScriptNamespace()
{
// The assumption is that there will be a SQL folder at the same level as this class
// where the SQL script resources are held
var dataAssemblyNamespace = MethodBase.GetCurrentMethod()?.DeclaringType?.Namespace ?? "";
var sqlScriptNamespace = $"{dataAssemblyNamespace}.Sql";
return sqlScriptNamespace;
}

/// <summary>
/// Read and return the contents of an embedded resource containing a SQL migration script
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string ReadMigrationSqlScript(string name)
{
string content = "";

// Get the resource name
var sqlScriptNamespace = GetSqlScriptNamespace();
var sqlResourceName = $"{sqlScriptNamespace}.{name}";

// Get the name of the resource and a resource stream for reading it
var assembly = Assembly.GetExecutingAssembly();
var resourceStream = assembly.GetManifestResourceStream(sqlResourceName);

// Open a stream reader to read the file content
using (var reader = new StreamReader(resourceStream!))
{
// Read the file content
content = reader.ReadToEnd();
}

return content;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c16565f

Please sign in to comment.