Add open library API call when search has no result
This commit is contained in:
@@ -4,13 +4,14 @@ import "gorm.io/gorm"
|
||||
|
||||
type Book struct {
|
||||
gorm.Model
|
||||
Title string `json:"title" gorm:"not null"`
|
||||
ISBN string `json:"isbn"`
|
||||
Summary string `json:"summary"`
|
||||
Author Author
|
||||
AuthorID uint
|
||||
AddedBy User
|
||||
AddedByID uint
|
||||
Cover StaticFile
|
||||
CoverID uint
|
||||
Title string `json:"title" gorm:"not null"`
|
||||
ISBN string `json:"isbn"`
|
||||
OpenLibraryId string `json:"openlibraryid"`
|
||||
Summary string `json:"summary"`
|
||||
Author Author
|
||||
AuthorID uint
|
||||
AddedBy User
|
||||
AddedByID uint
|
||||
Cover StaticFile
|
||||
CoverID uint
|
||||
}
|
||||
|
||||
1096
internal/openlibrary/example_openlibrary_call.json
Normal file
1096
internal/openlibrary/example_openlibrary_call.json
Normal file
File diff suppressed because it is too large
Load Diff
70
internal/openlibrary/openlibrary.go
Normal file
70
internal/openlibrary/openlibrary.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package openlibrary
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type OpenLibraryQueryResult struct {
|
||||
Books []OpenLibraryBook `json:"docs"`
|
||||
NumFound int `json:"numFound"`
|
||||
}
|
||||
|
||||
type OpenLibraryBook struct {
|
||||
Title string `json:"title"`
|
||||
Authors []string `json:"author_name"`
|
||||
OpenLibraryId string `json:"cover_edition_key"`
|
||||
}
|
||||
|
||||
func CallOpenLibrary(searchterm string, limit int, offset int) (OpenLibraryQueryResult, error) {
|
||||
var queryResult OpenLibraryQueryResult
|
||||
baseUrl := "https://openlibrary.org"
|
||||
booksApiUrl := "search.json"
|
||||
u, err := url.Parse(baseUrl)
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
u = u.JoinPath(booksApiUrl)
|
||||
if limit != 0 {
|
||||
addQueryParamInt(u, "limit", limit)
|
||||
}
|
||||
if offset != 0 {
|
||||
addQueryParamInt(u, "offset", offset)
|
||||
}
|
||||
addQueryParam(u, "q", searchterm)
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", u.String(), nil)
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("User-Agent", "PersonalLibraryManager/0.1 (artlef@protonmail.com)")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
bodyBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
bodyString := string(bodyBytes)
|
||||
decoder := json.NewDecoder(strings.NewReader(bodyString))
|
||||
err = decoder.Decode(&queryResult)
|
||||
return queryResult, err
|
||||
}
|
||||
|
||||
func addQueryParamInt(u *url.URL, paramName string, paramValue int) {
|
||||
addQueryParam(u, paramName, strconv.Itoa(paramValue))
|
||||
}
|
||||
|
||||
func addQueryParam(u *url.URL, paramName string, paramValue string) {
|
||||
q := u.Query()
|
||||
q.Set(paramName, paramValue)
|
||||
u.RawQuery = q.Encode()
|
||||
}
|
||||
27
internal/openlibrary/openlibrary_test.go
Normal file
27
internal/openlibrary/openlibrary_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package openlibrary_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/openlibrary"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCallOpenLibrary(t *testing.T) {
|
||||
result, err := openlibrary.CallOpenLibrary("man in high castle", 0, 0)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, result.NumFound, 25)
|
||||
assert.Equal(t, len(result.Books), 25)
|
||||
}
|
||||
|
||||
func TestCallOpenLibraryLimit(t *testing.T) {
|
||||
result, err := openlibrary.CallOpenLibrary("man in high castle", 10, 0)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, len(result.Books), 10)
|
||||
}
|
||||
|
||||
func TestCallOpenLibraryOffset(t *testing.T) {
|
||||
result, err := openlibrary.CallOpenLibrary("man in high castle", 0, 10)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, len(result.Books), 15)
|
||||
}
|
||||
@@ -9,13 +9,14 @@ import (
|
||||
)
|
||||
|
||||
type BookSearchGet struct {
|
||||
ID uint `json:"id"`
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
Rating int `json:"rating"`
|
||||
Read bool `json:"read"`
|
||||
WantRead bool `json:"wantread"`
|
||||
CoverPath string `json:"coverPath"`
|
||||
ID uint `json:"id"`
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
OpenLibraryId string `json:"openlibraryid"`
|
||||
Rating int `json:"rating"`
|
||||
Read bool `json:"read"`
|
||||
WantRead bool `json:"wantread"`
|
||||
CoverPath string `json:"coverPath"`
|
||||
}
|
||||
|
||||
func FetchBookSearchByAuthorGet(db *gorm.DB, userId uint, authorId uint64, limit int, offset int) ([]BookSearchGet, error) {
|
||||
@@ -69,7 +70,7 @@ func fetchBookSearchQuery(db *gorm.DB, userId uint, searchterm string) *gorm.DB
|
||||
|
||||
func fetchBookSearchQueryBuilder(db *gorm.DB, userId uint) *gorm.DB {
|
||||
query := db.Model(&model.Book{})
|
||||
query = query.Select("books.id, books.title, authors.name as author, user_books.rating, user_books.read, user_books.want_read, " + selectStaticFilesPath())
|
||||
query = query.Select("books.id, books.title, authors.name as author, books.open_library_id, user_books.rating, user_books.read, user_books.want_read, " + selectStaticFilesPath())
|
||||
query = joinAuthors(query)
|
||||
query = query.Joins("left join user_books on (user_books.book_id = books.id and user_books.user_id = ?)", userId)
|
||||
query = joinStaticFiles(query)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/openlibrary"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -33,7 +34,36 @@ func GetSearchBooksHandler(ac appcontext.AppContext) {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, books)
|
||||
var returnedBooks []query.BookSearchGet
|
||||
if len(books) > 0 {
|
||||
returnedBooks = books
|
||||
} else {
|
||||
queryResult, err := openlibrary.CallOpenLibrary(searchterm, limit, offset)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
returnedBooks = OpenLibraryBooksToBookSearchGet(queryResult.Books)
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, returnedBooks)
|
||||
}
|
||||
|
||||
func OpenLibraryBooksToBookSearchGet(OLbooks []openlibrary.OpenLibraryBook) []query.BookSearchGet {
|
||||
var books []query.BookSearchGet
|
||||
for _, b := range OLbooks {
|
||||
bookSearchGet := query.BookSearchGet{
|
||||
ID: 0,
|
||||
Title: b.Title,
|
||||
Author: b.Authors[0],
|
||||
OpenLibraryId: b.OpenLibraryId,
|
||||
Rating: 0,
|
||||
Read: false,
|
||||
WantRead: false,
|
||||
CoverPath: "",
|
||||
}
|
||||
books = append(books, bookSearchGet)
|
||||
}
|
||||
return books
|
||||
}
|
||||
|
||||
func GetSearchBooksCountHandler(ac appcontext.AppContext) {
|
||||
@@ -49,5 +79,16 @@ func GetSearchBooksCountHandler(ac appcontext.AppContext) {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, gin.H{"count": count})
|
||||
var finalCount int64
|
||||
if count > 0 {
|
||||
finalCount = count
|
||||
} else {
|
||||
queryResult, err := openlibrary.CallOpenLibrary(searchterm, 0, 0)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
finalCount = int64(queryResult.NumFound)
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, gin.H{"count": finalCount})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user