diff --git a/Server/Bibblan.csproj b/Server/Bibblan.csproj index 5f08839..83d2b18 100644 --- a/Server/Bibblan.csproj +++ b/Server/Bibblan.csproj @@ -11,6 +11,8 @@ + + 9.*-* diff --git a/Server/Business/Clients/HardcoverAuthenticationHandler.cs b/Server/Business/Clients/HardcoverAuthenticationHandler.cs new file mode 100644 index 0000000..c36e4be --- /dev/null +++ b/Server/Business/Clients/HardcoverAuthenticationHandler.cs @@ -0,0 +1,26 @@ +using Bibblan.Models; +using Microsoft.Extensions.Options; +using System.Net.Http.Headers; + +namespace Bibblan.Business.Clients +{ + public class HardcoverAuthenticationHandler : DelegatingHandler + { + public BibblanOptions settings; + + public HardcoverAuthenticationHandler(IOptions options) : base() + { + settings = options.Value; + InnerHandler = new HttpClientHandler(); + } + + protected override async Task SendAsync( + HttpRequestMessage request, + CancellationToken cancellationToken) + { + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", settings.HardcoverApiToken); + + return await base.SendAsync(request, cancellationToken); + } + } +} diff --git a/Server/Controllers/BibblanController.cs b/Server/Controllers/BibblanController.cs index bc3d052..1d3805c 100644 --- a/Server/Controllers/BibblanController.cs +++ b/Server/Controllers/BibblanController.cs @@ -1,19 +1,60 @@ 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]")] public class BibblanController : ControllerBase { DatabaseService _db; CalibreService _calibre; + private readonly IGraphQLClient _client; - public BibblanController(DatabaseService databaseService, CalibreService calibre) + public BibblanController(DatabaseService databaseService, CalibreService calibre, IGraphQLClient client) { _db = databaseService; _calibre = calibre; + _client = client; } [HttpGet("cover")] @@ -99,5 +140,45 @@ namespace Bibblan.Controllers var tags = _db.GetTags(filter).ToList(); return Ok(tags); } + //test + [HttpGet("lookup")] + public async Task HardcoverLookup(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); + } } } diff --git a/Server/Models/BibblanOptions.cs b/Server/Models/BibblanOptions.cs index aab21f3..de066a8 100644 --- a/Server/Models/BibblanOptions.cs +++ b/Server/Models/BibblanOptions.cs @@ -7,5 +7,7 @@ public string CalibreDb { get; set; } = String.Empty; public string CalibreRoot { get; set; } = String.Empty; public string BibblanConnection { get; set; } = String.Empty; + public string HardcoverApiToken { get; set; } = string.Empty; + public string HardcoverApiUrl { get; set; } = string.Empty; } } diff --git a/Server/Program.cs b/Server/Program.cs index b5db4ad..8623ee3 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -1,10 +1,15 @@ +using Bibblan.Business.Clients; using Bibblan.Business.Services; using Bibblan.Models; using Bibblan.ViewModels; using Dapper; +using GraphQL.Client.Abstractions; +using GraphQL.Client.Http; +using GraphQL.Client.Serializer.Newtonsoft; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using System.ComponentModel.DataAnnotations.Schema; +using System.Net.Http; var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddJsonFile($"appsettings.user.json", true, true); @@ -23,7 +28,15 @@ builder.Services.AddControllers(); // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi builder.Services.AddOpenApi(); +//hardcover registration +builder.Services.AddTransient();//måste vara transient +builder.Services.AddScoped(s => new GraphQLHttpClient(new GraphQLHttpClientOptions +{ + HttpMessageHandler = s.GetRequiredService(), + EndPoint = new Uri(config.HardcoverApiUrl), +}, new NewtonsoftJsonSerializer())); +//databases builder.Services.AddDbContext(options => options.UseSqlite($"Data Source={config.CalibreDb};Mode=ReadOnly;")); builder.Services.AddDbContext(options => options.UseNpgsql(config.BibblanConnection)); diff --git a/Server/appsettings.json b/Server/appsettings.json index 7b38c6f..7ff10c8 100644 --- a/Server/appsettings.json +++ b/Server/appsettings.json @@ -9,6 +9,8 @@ "Bibblan": { "CalibreDb": "metadata.db", "CalibreRoot": "c:\\my_books\\", - "BibblanConnection": "Server=localhost;Port=5432;Database=bibblan;User Id=bibblanuser;Password=1234567;" + "BibblanConnection": "Server=localhost;Port=5432;Database=bibblan;User Id=bibblanuser;Password=1234567;", + "HardcoverApiUrl": "https://api.hardcover.app/v1/graphql", + "HardcoverApiToken": "" } }