rework template load/generation + add generic error on template error
This commit is contained in:
+46
-14
@@ -29,14 +29,41 @@ type IStore interface {
|
||||
|
||||
var _ IStore = (*BookStore)(nil)
|
||||
|
||||
//go:embed sql/create_books_table.sql
|
||||
var sqlCreateTable string
|
||||
|
||||
//go:embed sql/insert_book.sql
|
||||
var sqlInsertBook string
|
||||
|
||||
//go:embed sql/search_books.sql
|
||||
var sqlSearchBooks string
|
||||
const (
|
||||
sqlSearchBooks = `
|
||||
select
|
||||
b.*
|
||||
from
|
||||
books b
|
||||
where
|
||||
lower(b.name) like lower(?)
|
||||
or lower(b.description) like lower(?)
|
||||
or lower(b.editor) like lower(?)
|
||||
or lower(b.year) like lower(?)
|
||||
union
|
||||
select
|
||||
b2.*
|
||||
from books b2, json_each(b2.authors)
|
||||
where lower(json_each.value) like lower(?)
|
||||
union
|
||||
select
|
||||
b3.*
|
||||
from books b3, json_each(b3.keywords)
|
||||
where lower(json_each.value) like lower(?)
|
||||
`
|
||||
sqlInsertBook = "insert into books(name, description, editor, authors, year, keywords, path) values (?,?,?,?,?,?,?)"
|
||||
sqlCreateBookTable = `
|
||||
create table if not exists books (
|
||||
name text primary key,
|
||||
description text,
|
||||
editor text not null,
|
||||
authors jsonb not null,
|
||||
year int not null,
|
||||
keywords jsonb,
|
||||
path text not null
|
||||
)
|
||||
`
|
||||
)
|
||||
|
||||
type BookMetadata struct {
|
||||
Name string
|
||||
@@ -45,7 +72,7 @@ type BookMetadata struct {
|
||||
Authors []string
|
||||
Year uint16
|
||||
Keywords []string
|
||||
Path string
|
||||
path string
|
||||
}
|
||||
|
||||
func (bm *BookMetadata) getAuthors() (string, error) {
|
||||
@@ -98,10 +125,14 @@ func (bm *BookMetadata) intoStmtValues() ([]any, error) {
|
||||
authors,
|
||||
bm.Year,
|
||||
keywords,
|
||||
bm.Path,
|
||||
bm.path,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (bm *BookMetadata) GetPath() string {
|
||||
return bm.path
|
||||
}
|
||||
|
||||
type BookStore struct {
|
||||
processing *sync.WaitGroup
|
||||
dir string
|
||||
@@ -128,7 +159,7 @@ func (bs *BookStore) init(dir string) {
|
||||
log.Fatal().Err(err).Msg("unable initialize sqlite3 database")
|
||||
}
|
||||
|
||||
if _, err := db.Exec(sqlCreateTable); err != nil {
|
||||
if _, err := db.Exec(sqlCreateBookTable); err != nil {
|
||||
log.Fatal().Err(err).Msg("unable to create books table")
|
||||
}
|
||||
|
||||
@@ -179,9 +210,9 @@ func (bs *BookStore) Save(bm *BookMetadata, content io.ReadCloser) error {
|
||||
|
||||
defer content.Close()
|
||||
|
||||
bm.Path = filepath.Join(bs.dir, bm.getFormattedName())
|
||||
bm.path = filepath.Join(bs.dir, bm.getFormattedName())
|
||||
|
||||
dst, err := os.Create(bm.Path)
|
||||
dst, err := os.Create(bm.path)
|
||||
if err != nil {
|
||||
log.Err(err).Msg(ErrFileCreation.Error())
|
||||
return ErrFileCreation
|
||||
@@ -220,13 +251,14 @@ func (bs *BookStore) Search(value string) ([]BookMetadata, error) {
|
||||
var bm BookMetadata
|
||||
var authors string
|
||||
var keyword *string
|
||||
if err := rows.Scan(&bm.Name, &bm.Description, &bm.Editor, &authors, &bm.Year, &keyword, &bm.Path); err != nil {
|
||||
if err := rows.Scan(&bm.Name, &bm.Description, &bm.Editor, &authors, &bm.Year, &keyword, &bm.path); err != nil {
|
||||
log.Err(err).Msg("unable to scan row")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var authorsSlice []string
|
||||
if err := json.Unmarshal([]byte(authors), &authorsSlice); err != nil {
|
||||
log.Err(err).Msg("unable to unmarshal authors into slice")
|
||||
return nil, err
|
||||
}
|
||||
bm.Authors = authorsSlice
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
create table if not exists books (
|
||||
name text primary key,
|
||||
description text,
|
||||
editor text not null,
|
||||
authors jsonb not null,
|
||||
year int not null,
|
||||
keywords jsonb,
|
||||
path text not null
|
||||
)
|
||||
@@ -1,2 +0,0 @@
|
||||
insert into books(name, description, editor, authors, year, keywords, path)
|
||||
values (?,?,?,?,?,?,?)
|
||||
@@ -1,20 +0,0 @@
|
||||
select
|
||||
b.*
|
||||
from
|
||||
books b
|
||||
where
|
||||
lower(b.name) like lower(?)
|
||||
or lower(b.description) like lower(?)
|
||||
or lower(b.editor) like lower(?)
|
||||
or lower(b.year) like lower(?)
|
||||
union
|
||||
select
|
||||
b2.*
|
||||
from books b2, json_each(b2.authors)
|
||||
where lower(json_each.value) like lower(?)
|
||||
union
|
||||
select
|
||||
b3.*
|
||||
from books b3, json_each(b3.keywords)
|
||||
where lower(json_each.value) like lower(?)
|
||||
|
||||
Reference in New Issue
Block a user