diff --git a/Frontend/src/components/AuthorCard.tsx b/Frontend/src/components/AuthorCard.tsx index eaae640..c5f4501 100644 --- a/Frontend/src/components/AuthorCard.tsx +++ b/Frontend/src/components/AuthorCard.tsx @@ -10,7 +10,7 @@ const AuthorCard: Component<{ author: author }> = (props: { {props.author.name} diff --git a/Frontend/src/components/AuthorList.tsx b/Frontend/src/components/AuthorList.tsx index b41b537..343d575 100644 --- a/Frontend/src/components/AuthorList.tsx +++ b/Frontend/src/components/AuthorList.tsx @@ -5,13 +5,26 @@ import { author } from "../types/types"; const BookList: Component = () => { const [authors, setAuthors] = createSignal([]); + const [query, setQuery] = createSignal(""); + + const update = (query: string) => { + setQuery(query); + BibblanService.getAuthors(query).then(setAuthors); + }; + onMount(() => { BibblanService.getAuthors().then(setAuthors); }); return (
-

Books!

+

Authors!

+ update(e.currentTarget.value)} + />
{(item) => }
diff --git a/Frontend/src/components/BookList.tsx b/Frontend/src/components/BookList.tsx index 2992183..ffff18f 100644 --- a/Frontend/src/components/BookList.tsx +++ b/Frontend/src/components/BookList.tsx @@ -1,4 +1,4 @@ -import { createSignal, onMount, For, type Component } from "solid-js"; +import { createSignal, onMount, For, Show, type Component } from "solid-js"; import { useParams } from "@solidjs/router"; import BookCard from "./BookCard"; import BibblanService from "../services/bibblanservice"; @@ -6,7 +6,14 @@ import { book } from "../types/types"; const BookList: Component = () => { const [books, setBooks] = createSignal([]); + const [query, setQuery] = createSignal(""); const params = useParams(); + + const update = (query: string) => { + setQuery(query); + BibblanService.getBooks(params.authorid, query).then(setBooks); + }; + onMount(() => { BibblanService.getBooks(params.authorid).then(setBooks); }); @@ -14,6 +21,14 @@ const BookList: Component = () => { return (

Books!

+ + update(e.currentTarget.value)} + /> +
{(item) => }
diff --git a/Frontend/src/services/bibblanservice.ts b/Frontend/src/services/bibblanservice.ts index 7548925..1e41986 100644 --- a/Frontend/src/services/bibblanservice.ts +++ b/Frontend/src/services/bibblanservice.ts @@ -2,18 +2,27 @@ import { book, author } from "../types/types"; const BibblanService = { getBooks: async ( - authorid: string | undefined = undefined + authorid: string | undefined = undefined, + query: string | undefined = undefined ): Promise => { let url = "/api/bibblan/books"; if (authorid != undefined) { url += `/author/${authorid}`; + } else if (query != undefined && query.length > 0) { + url += `?query=${encodeURIComponent(query)}`; } const response = await fetch(url); return response.json(); }, - getAuthors: async (): Promise => { - const response = await fetch("/api/bibblan/authors"); + getAuthors: async ( + query: string | undefined = undefined + ): Promise => { + let url = "/api/bibblan/authors"; + if (query != undefined && query.length > 0) { + url += `?query=${encodeURIComponent(query)}`; + } + const response = await fetch(url); return response.json(); }, }; diff --git a/Server/Business/Services/DatabaseService.cs b/Server/Business/Services/DatabaseService.cs index 51e8b0d..998be35 100644 --- a/Server/Business/Services/DatabaseService.cs +++ b/Server/Business/Services/DatabaseService.cs @@ -10,6 +10,7 @@ namespace Bibblan.Business.Services public class BookFilter { public int? Author; + public string? Query; } public class DatabaseService @@ -29,14 +30,23 @@ namespace Bibblan.Business.Services if(filter.Author != null) { query += $"id in (select book from books_authors_link where author = {filter.Author})"; + } else if(!String.IsNullOrWhiteSpace(query)) + { + filter.Query = filter.Query.ToLowerInvariant(); + query += $"lower(title) like '%{filter.Query}%' or lower(author_sort) like '%{filter.Query}%'"; } } return conn.Query(query).Take(count).ToList(); } - public IEnumerable GetAuthors(int count) + public IEnumerable GetAuthors(int count, BookFilter filter = null) { var query = "select a.id, a.name, count(bal.*) as bookcount\r\nfrom authors a\r\nleft join books_authors_link bal on a.id = bal.author\r\ngroup by a.id , a.name "; + if(!String.IsNullOrWhiteSpace(filter?.Query)) + { + filter.Query = filter.Query.ToLowerInvariant(); + query += $" having lower(a.name) like '%{filter.Query}%'"; + } var conn = new NpgsqlConnection(settings.BibblanConnection); return conn.Query(query).Take(count).ToList(); diff --git a/Server/Controllers/BibblanController.cs b/Server/Controllers/BibblanController.cs index 77d7a8e..0bef8eb 100644 --- a/Server/Controllers/BibblanController.cs +++ b/Server/Controllers/BibblanController.cs @@ -25,16 +25,26 @@ namespace Bibblan.Controllers } [HttpGet("books")] - public IActionResult GetBooks() + public IActionResult GetBooks(string query = null) { - var authors = _db.GetBooks(100).ToList(); - return Ok(authors); + BookFilter filter = query != null ? new BookFilter + { + Query = query + } : null; + + var books = _db.GetBooks(100,filter).ToList(); + return Ok(books); } [HttpGet("authors")] - public IActionResult GetAuthors() + public IActionResult GetAuthors(string query = null) { - var authors = _db.GetAuthors(100).ToList(); + BookFilter filter = query != null ? new BookFilter + { + Query = query + } : null; + + var authors = _db.GetAuthors(100, filter).ToList(); return Ok(authors); } @@ -48,6 +58,13 @@ namespace Bibblan.Controllers 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"}); + } + } }