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, IGraphQLClient client) { _db = databaseService; _calibre = calibre; _client = client; } [HttpGet("cover")] public IActionResult GetCover(string path) { //TODO: Bör kanske inte gå direkt mot calibres filer.. var bytes = _calibre.Cover(path); if (bytes == null) return NotFound(); return File(bytes, "image/jpeg", true); } [HttpGet("books")] public IActionResult GetBooks(string query = null) { BookFilter? filter = query != null ? new BookFilter { Query = query } : null; var books = _db.GetBooks(100,filter).ToList(); return Ok(books); } [HttpGet("books/{id}")] public IActionResult GetBook(int id) { var book = _db.GetBookDetails(id); return Ok(book); } [HttpGet("authors")] public IActionResult GetAuthors(string query = null) { BookFilter? filter = query != null ? new BookFilter { Query = query } : null; var authors = _db.GetAuthors(100, filter).ToList(); return Ok(authors); } [HttpGet("books/author/{authorid}")] public IActionResult GetBooksByAuthor(int authorid) { var authors = _db.GetBooks(100, new BookFilter { Author = authorid }).ToList(); return Ok(authors); } [HttpGet("authorcover/{authorid}")] public IActionResult GetAuthorCover(int authorid) { //TODO: fixa vid tillfälle return Ok(new { desc = "picture of banana goes here"}); } [HttpGet("series")] public IActionResult GetSeries(string query = null) { BookFilter? filter = query != null ? new BookFilter { Query = query } : null; var series = _db.GetSeries(100, filter).ToList(); return Ok(series); } [HttpGet("tags")] public IActionResult GetTags(string query = null) { BookFilter? filter = query != null ? new BookFilter { Query = query } : null; 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); } } }