From 0af47751cf5bf60c8e6e3a220af1e16b6337fd4b Mon Sep 17 00:00:00 2001 From: florpan Date: Thu, 18 Sep 2025 17:57:00 +0200 Subject: [PATCH] snyggade till hardcover. la til author lookup --- Server/Business/Clients/HardcoverClient.cs | 154 +++++++++++++++++++++ Server/Controllers/BibblanController.cs | 94 +++---------- Server/Program.cs | 1 + 3 files changed, 173 insertions(+), 76 deletions(-) create mode 100644 Server/Business/Clients/HardcoverClient.cs diff --git a/Server/Business/Clients/HardcoverClient.cs b/Server/Business/Clients/HardcoverClient.cs new file mode 100644 index 0000000..98b4e60 --- /dev/null +++ b/Server/Business/Clients/HardcoverClient.cs @@ -0,0 +1,154 @@ +using GraphQL; +using GraphQL.Client.Abstractions; +using static Microsoft.EntityFrameworkCore.DbLoggerCategory; + +namespace Bibblan.Business.Clients +{ + public class Publisher + { + public string Name { get; set; } + } + public class Image + { + public string Url { get; set; } + } + + public class Author + { + public long Id { get; set; } + public string Name { get; set; } + public string Name_Personal { get; set; } + public Image Image { get; set; } + public string Bio { get; set; } + public int Book_Count { get; set; } + public DateTime? Born_Date { get; set; } + public DateTime? Death_Date { get; set; } + public int? Gender_Id{ get; set; } + public Dictionary> Identifiers { get; set; } + public string State { get; set; } + } + + + public class Contributor + { + public Author Author { get; set; } + } + + public class Edition + { + public long Id { get; set; } + public string Title { get; set; } + public string Edition_Format { get; set; } + public int? Pages { get; set; } + public DateTime? Release_Date { get; set; } + public string Isbn_10 { get; set; } + public string Isbn_13 { get; set; } + public Publisher Publisher { get; set; } + public List Contributions { get; set; } + public Image Image { get; set; } + } + + public class EditionsCollectionType + { + public List Editions { get; set; } + } + + public class AuthorsCollectionType + { + public List Authors { get; set; } + } + + public class HardcoverClient + { + private readonly IGraphQLClient _client; + public HardcoverClient(IGraphQLClient client) + { + _client = client; + } + + + public async Task> LookupBook(string title, string? author = null) + { + var query = new GraphQLRequest + { + Query = @" + query ($title: String!, $author: String) { + editions( + where: {title: {_eq: $title}, _and: {contributions: {author: {name: {_eq: $author}}}}} + ) { + id + title + edition_format + pages + release_date + isbn_10 + isbn_13 + publisher { + name + } + contributions { + author { + id + name + name_personal + image { + url + } + bio + books_count + born_date + death_date + gender_id + identifiers + state + } + } + image { + url + } + } + }", + Variables = new + { + title, + author + } + }; + var response = await _client.SendQueryAsync(query); + return response.Data.Editions; + } + + public async Task> LookupAuthor(string name) + { + //order_by: {books_count: desc} + //limit: 1 + var query = new GraphQLRequest + { + Query = @"query ($name: String){ + authors(where: {name: {_eq: $name}}) { + id + name + name_personal + image { + url + } + bio + books_count + born_date + death_date + gender_id + identifiers + state + } + }", + Variables = new + { + name + } + }; + var response = await _client.SendQueryAsync(query); + return response.Data.Authors; + } + + } +} diff --git a/Server/Controllers/BibblanController.cs b/Server/Controllers/BibblanController.cs index 1d3805c..7a0e054 100644 --- a/Server/Controllers/BibblanController.cs +++ b/Server/Controllers/BibblanController.cs @@ -1,46 +1,12 @@ -using Bibblan.Business.Services; +using Bibblan.Business.Clients; +using Bibblan.Business.Services; using GraphQL; using GraphQL.Client.Abstractions; using Microsoft.AspNetCore.Mvc; namespace Bibblan.Controllers { - public class Publisher - { - public string Name { get; set; } - } - public class Image - { - public string Url { get; set; } - } - public class Author - { - public string Name { get; set; } - } - public class Contributor - { - public Author Author { get; set; } - } - - public class Edition - { - public long Id { get; set; } - public string Title { get; set; } - public string Edition_Format { get; set; } - public int? Pages { get; set; } - public DateTime? Release_Date { get; set; } - public string Isbn_10 { get; set; } - public string Isbn_13 { get; set; } - public Publisher Publisher { get; set; } - public List Contributions { get; set; } - public Image Image { get; set; } - } - - public class EditionsCollectionType - { - public List Editions { get; set; } - } [ApiController] [Route("api/[controller]")] @@ -48,13 +14,13 @@ namespace Bibblan.Controllers { DatabaseService _db; CalibreService _calibre; - private readonly IGraphQLClient _client; + HardcoverClient _hardCover; - public BibblanController(DatabaseService databaseService, CalibreService calibre, IGraphQLClient client) + public BibblanController(DatabaseService databaseService, CalibreService calibre, HardcoverClient hcclient) { _db = databaseService; _calibre = calibre; - _client = client; + _hardCover = hcclient; } [HttpGet("cover")] @@ -140,45 +106,21 @@ namespace Bibblan.Controllers var tags = _db.GetTags(filter).ToList(); return Ok(tags); } + //test - [HttpGet("lookup")] - public async Task HardcoverLookup(string title, string author) + [HttpGet("lookupbook")] + public async Task LookupBook(string title, string author) { - var query = new GraphQLRequest - { - Query = @" - query SomeNameHere($title: String!, $author: String!) { - editions( - where: {title: {_eq: $title}, _and: {contributions: {author: {name: {_eq: $author}}}}} - ) { - id - title - edition_format - pages - release_date - isbn_10 - isbn_13 - publisher { - name - } - contributions { - author { - name - } - } - image { - url - } - } - }", - Variables = new - { - title, - author - } - }; - var response = await _client.SendQueryAsync(query); - return Ok(response.Data.Editions); + var result = await _hardCover.LookupBook(title, author); + return Ok(result); } + + [HttpGet("lookupauthor")] + public async Task LookupAuthor(string name) + { + var result = await _hardCover.LookupAuthor(name); + return Ok(result); + } + } } diff --git a/Server/Program.cs b/Server/Program.cs index 8623ee3..9ae91bb 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -35,6 +35,7 @@ builder.Services.AddScoped(s => new GraphQLHttpClient(new GraphQ HttpMessageHandler = s.GetRequiredService(), EndPoint = new Uri(config.HardcoverApiUrl), }, new NewtonsoftJsonSerializer())); +builder.Services.AddScoped(); //databases builder.Services.AddDbContext(options => options.UseSqlite($"Data Source={config.CalibreDb};Mode=ReadOnly;"));