Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions Controller/ArtistController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@ public async Task<IActionResult> GetById([FromQuery] string? id)
}
return BadRequest("Provide either id for the artist");
}

[HttpGet("name")]
public async Task<IActionResult> GetByName([FromQuery] string? name)
{
if (!string.IsNullOrWhiteSpace(name))
{
var result = await mediator.Send(new ArtistQuery.GetByName(name));
return result is null ? NotFound() : Ok(result);
}
return BadRequest("Provide either name for the artist");
}

[HttpGet("wikidata")]
public async Task<IActionResult> GetWikidataById([FromQuery] string? id)
Expand Down
21 changes: 21 additions & 0 deletions Controller/ArtistInfoController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using HollyJukeBox.QueryModels;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace HollyJukeBox.Controller;

[ApiController]
[Route("api/artistinfo")]
public class ArtistInfoController(IMediator mediator) : ControllerBase
{
[HttpGet]
public async Task<IActionResult> GetById([FromQuery] string? id)
{
if (!string.IsNullOrWhiteSpace(id))
{
var result = await mediator.Send(new ArtistInfoQuery.GetById(id));
return result is null ? NotFound() : Ok(result);
}
return BadRequest("Provide either id for the artist");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace HollyJukeBox.Controller;
/// Handles album-related data access
/// </summary>
[ApiController]
[Route("api/album")]
public class AlbumController(IMediator mediator) : ControllerBase
[Route("api/coverart")]
public class CoverArtController(IMediator mediator) : ControllerBase
{
[HttpGet]
public async Task<IActionResult> GetById([FromQuery] string? id)
{
if (!string.IsNullOrWhiteSpace(id))
{
var result = await mediator.Send(new AlbumQuery.GetById(id));
var result = await mediator.Send(new CoverArtQuery.GetById(id));
return result is null ? NotFound() : Ok(result);
}

Expand Down
80 changes: 80 additions & 0 deletions Data/DbInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Dapper;
using Microsoft.Data.Sqlite;

namespace HollyJukeBox.Data;

public class DbInitializer
{
private SqliteConnection connection;

public DbInitializer(IConfiguration configuration)
{
connection = new SqliteConnection(configuration.GetConnectionString("HollyJukeBoxDb"));
}

public async Task EnsureTablesCreatedAsync()
{
await connection.ExecuteAsync(@"
CREATE TABLE IF NOT EXISTS Artist (
Id TEXT PRIMARY KEY,
Name TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS Releases (
Id TEXT PRIMARY KEY,
Title TEXT NOT NULL,
FirstReleaseDate TEXT,
ArtistId TEXT NOT NULL,
FOREIGN KEY (ArtistId) REFERENCES Artist(Id)
);

CREATE TABLE IF NOT EXISTS Relations (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
ArtistId TEXT NOT NULL,
Type TEXT NOT NULL,
UrlId TEXT NOT NULL,
FOREIGN KEY (ArtistId) REFERENCES Artist(Id),
FOREIGN KEY (UrlId) REFERENCES Urls(Id)
);

CREATE TABLE IF NOT EXISTS Urls (
Id TEXT PRIMARY KEY,
Resource TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS CoverArt (
Id TEXT PRIMARY KEY
);

CREATE TABLE IF NOT EXISTS Images (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
CoverArtId TEXT NOT NULL,
Image TEXT NOT NULL,
FOREIGN KEY (CoverArtId) REFERENCES CoverArt(Id)
);

CREATE TABLE IF NOT EXISTS ImageTypes (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
ImageId INTEGER NOT NULL,
Type TEXT NOT NULL,
FOREIGN KEY (ImageId) REFERENCES Images(Id)
);

CREATE TABLE IF NOT EXISTS ArtistInfo (
Id TEXT PRIMARY KEY,
Name TEXT NOT NULL,
Description TEXT
);

CREATE TABLE IF NOT EXISTS AlbumInfo (
Id TEXT PRIMARY KEY,
Title TEXT NOT NULL,
FirstReleaseDate TEXT,
ImageFront TEXT,
ImageBack TEXT,
ArtistInfoId TEXT NOT NULL,
FOREIGN KEY (ArtistInfoId) REFERENCES ArtistInfo(Id)
);
");
}
}
11 changes: 0 additions & 11 deletions Endpoints/AlbumEndPoint.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Endpoints/ArtistEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@

public class ArtistEndPoint(IOptions<ApiSettings> options, HttpClient client) : IArtistEndPoint
{
public async Task<ArtistDto> GetById(string id) => await client.GetFromJsonAsync<ArtistDto>(

Check warning on line 9 in Endpoints/ArtistEndPoint.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 9 in Endpoints/ArtistEndPoint.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
options.Value.MusicBrainzUrl + $"artist/{id}?inc=release-groups+url-rels&fmt=json");

public async Task<ArtistsDto> GetByName(string name) => await client.GetFromJsonAsync<ArtistsDto>(
options.Value.MusicBrainzUrl + $"artist?query={name}&fmt=json");

public async Task<WikiDataDto> GetWikiData(string id) => await client.GetFromJsonAsync<WikiDataDto>(

Check warning on line 12 in Endpoints/ArtistEndPoint.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 12 in Endpoints/ArtistEndPoint.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
options.Value.WikiDataUrl + $"?action=wbgetentities&ids={id}&format=json&props=sitelinks");

public async Task<WikipediaSummaryDto> GetWikipediaSummary(string artist) => await client.GetFromJsonAsync<WikipediaSummaryDto>(

Check warning on line 15 in Endpoints/ArtistEndPoint.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 15 in Endpoints/ArtistEndPoint.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
options.Value.WikipediaSummery + $"{artist}");
Expand Down
23 changes: 23 additions & 0 deletions Endpoints/CoverArtEndPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Net;
using HollyJukeBox.Models;
using HollyJukeBox.Services;
using Microsoft.Extensions.Options;

namespace HollyJukeBox.Endpoints;

public class CoverArtEndPoint(IOptions<ApiSettings> options, HttpClient client) : ICoverArtEndPoint
{
public async Task<CoverArtDto> GetById(string id)
{
var response = await client.GetAsync(options.Value.CoverArtArchiveUrl + $"release-group/{id}");
if (response.StatusCode != HttpStatusCode.OK)
{
return new CoverArtDto{
Id = id,
Images = new List<Images>()
};
}

return await response.Content.ReadFromJsonAsync<CoverArtDto>();

Check warning on line 21 in Endpoints/CoverArtEndPoint.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 21 in Endpoints/CoverArtEndPoint.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}
}
8 changes: 0 additions & 8 deletions Endpoints/IAlbumEndPoint.cs

This file was deleted.

1 change: 0 additions & 1 deletion Endpoints/IArtistEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace HollyJukeBox.Endpoints;
public interface IArtistEndPoint
{
public Task<ArtistDto> GetById(string id);
public Task<ArtistsDto> GetByName(string name);
public Task<WikiDataDto> GetWikiData(string id);
public Task<WikipediaSummaryDto> GetWikipediaSummary(string enwikiTitle);
}
8 changes: 8 additions & 0 deletions Endpoints/ICoverArtEndPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using HollyJukeBox.Models;

namespace HollyJukeBox.Endpoints;

public interface ICoverArtEndPoint
{
public Task<CoverArtDto> GetById(string id);
}
13 changes: 0 additions & 13 deletions Handler/AlbumHandler.cs

This file was deleted.

35 changes: 25 additions & 10 deletions Handler/ArtistHandler.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
using HollyJukeBox.Endpoints;
using HollyJukeBox.Models;
using HollyJukeBox.QueryModels;
using HollyJukeBox.Repository;
using HollyJukeBox.Services;
using MediatR;

namespace HollyJukeBox.Handler;

public class ArtistHandler(IArtistEndPoint artistEndPoint) :
public class ArtistHandler(
IArtistEndPoint artistEndPoint,
IMemoryCashingService memoryCashingService,
IArtistRepository artistRepository) :
IRequestHandler<ArtistQuery.GetById, ArtistDto>,
IRequestHandler<ArtistQuery.GetByName, ArtistDto>,
IRequestHandler<ArtistQuery.GetWikiData, WikiDataDto>,
IRequestHandler<ArtistQuery.GetWikipediaSummary, WikipediaSummaryDto>
{
public async Task<ArtistDto> Handle(ArtistQuery.GetById query, CancellationToken cancellationToken)
=> await artistEndPoint.GetById(query.Id);
public async Task<ArtistDto> Handle(ArtistQuery.GetById request, CancellationToken cancellationToken)
{
var artist = memoryCashingService.Get<ArtistDto>($"artistDto:{request.Id}");

if (artist is not null)
{
return artist;
}

public async Task<ArtistDto> Handle(ArtistQuery.GetByName query, CancellationToken cancellationToken)
{
var result = await artistEndPoint.GetByName(query.Name);
var artist = result.Artists.First(x => string.Equals(x.Name, query.Name, StringComparison.OrdinalIgnoreCase));
artist = await artistEndPoint.GetById(artist.Id);
return artist;
artist = await artistRepository.GetByIdAsync(request.Id);
if (artist is not null)
{
memoryCashingService.Store($"artistDto:{request.Id}", artist);
return artist;
}

artist = await artistEndPoint.GetById(request.Id);
memoryCashingService.Store($"artistDto:{request.Id}", artist);
await artistRepository.SaveAsync(artist);
return artist;
}

public async Task<WikiDataDto> Handle(ArtistQuery.GetWikiData request, CancellationToken cancellationToken)
Expand Down
Loading
Loading