rename book package to media

This commit is contained in:
David 2022-03-05 10:58:15 -05:00
parent c9b70f02e7
commit 98584bbef6
8 changed files with 41 additions and 38 deletions

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"git.yetaga.in/alazyreader/library/book" "git.yetaga.in/alazyreader/library/media"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
) )
@ -20,16 +20,16 @@ func NewEventError(err error) *EventError {
// save change to book // save change to book
type EventBookUpdate struct { type EventBookUpdate struct {
tcell.EventTime tcell.EventTime
book *book.Book book *media.Book
} }
func NewEventBookUpdate(b *book.Book) *EventBookUpdate { func NewEventBookUpdate(b *media.Book) *EventBookUpdate {
e := &EventBookUpdate{book: b} e := &EventBookUpdate{book: b}
e.SetEventNow() e.SetEventNow()
return e return e
} }
func (e *EventBookUpdate) Book() *book.Book { func (e *EventBookUpdate) Book() *media.Book {
return e.book return e.book
} }

View File

@ -9,10 +9,10 @@ import (
"strings" "strings"
"sync" "sync"
"git.yetaga.in/alazyreader/library/book"
"git.yetaga.in/alazyreader/library/config" "git.yetaga.in/alazyreader/library/config"
"git.yetaga.in/alazyreader/library/database" "git.yetaga.in/alazyreader/library/database"
"git.yetaga.in/alazyreader/library/importer" "git.yetaga.in/alazyreader/library/importer"
"git.yetaga.in/alazyreader/library/media"
"git.yetaga.in/alazyreader/library/ui" "git.yetaga.in/alazyreader/library/ui"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/kelseyhightower/envconfig" "github.com/kelseyhightower/envconfig"
@ -120,7 +120,7 @@ func main() {
}() }()
// book list and options menu (left column) // book list and options menu (left column)
l := ui.NewList(Titles(state.Get("library").([]book.Book)), 0) l := ui.NewList(Titles(state.Get("library").([]media.Book)), 0)
menu := ui.NewBox( menu := ui.NewBox(
"library", "library",
[]string{"˄˅ select", "⏎ edit", "(n)ew", "(i)mport", "(q)uit"}, []string{"˄˅ select", "⏎ edit", "(n)ew", "(i)mport", "(q)uit"},
@ -131,7 +131,7 @@ func main() {
ui.StyleActive, ui.StyleActive,
false, false,
) )
activeBookDetails := ui.NewBookDetails(&book.Book{}) activeBookDetails := ui.NewBookDetails(&media.Book{})
// book display (right column) // book display (right column)
activeBook := ui.NewBox( activeBook := ui.NewBox(
@ -307,7 +307,7 @@ func main() {
default: default:
} }
// repaint // repaint
l.SetMembers(Titles(state.Get("library").([]book.Book))) l.SetMembers(Titles(state.Get("library").([]media.Book)))
container.Draw(screen) container.Draw(screen)
popup.Draw(screen) popup.Draw(screen)
errorPopup.Draw(screen) errorPopup.Draw(screen)
@ -315,7 +315,7 @@ func main() {
} }
} }
func Titles(lb []book.Book) []ui.ListKeyValue { func Titles(lb []media.Book) []ui.ListKeyValue {
r := []ui.ListKeyValue{} r := []ui.ListKeyValue{}
for i := range lb { for i := range lb {
r = append(r, ui.ListKeyValue{ r = append(r, ui.ListKeyValue{
@ -326,11 +326,11 @@ func Titles(lb []book.Book) []ui.ListKeyValue {
return r return r
} }
func GetBookByID(id int, lb []book.Book) *book.Book { func GetBookByID(id int, lb []media.Book) *media.Book {
for i := range lb { for i := range lb {
if lb[i].ID == id { if lb[i].ID == id {
return &lb[i] return &lb[i]
} }
} }
return &book.Book{} return &media.Book{}
} }

View File

@ -8,10 +8,10 @@ import (
"net/http" "net/http"
"strings" "strings"
"git.yetaga.in/alazyreader/library/book"
"git.yetaga.in/alazyreader/library/config" "git.yetaga.in/alazyreader/library/config"
"git.yetaga.in/alazyreader/library/database" "git.yetaga.in/alazyreader/library/database"
"git.yetaga.in/alazyreader/library/frontend" "git.yetaga.in/alazyreader/library/frontend"
"git.yetaga.in/alazyreader/library/media"
"github.com/kelseyhightower/envconfig" "github.com/kelseyhightower/envconfig"
) )
@ -23,7 +23,7 @@ func max(a, b int) int {
} }
type Library interface { type Library interface {
GetAllBooks(context.Context) ([]book.Book, error) GetAllBooks(context.Context) ([]media.Book, error)
} }
type Router struct { type Router struct {

View File

@ -5,43 +5,43 @@ import (
"fmt" "fmt"
"sync" "sync"
"git.yetaga.in/alazyreader/library/book" "git.yetaga.in/alazyreader/library/media"
) )
type Memory struct { type Memory struct {
lock sync.Mutex lock sync.Mutex
shelf []book.Book shelf []media.Book
} }
func (m *Memory) GetAllBooks(_ context.Context) ([]book.Book, error) { func (m *Memory) GetAllBooks(_ context.Context) ([]media.Book, error) {
m.lock.Lock() m.lock.Lock()
defer m.lock.Unlock() defer m.lock.Unlock()
if m.shelf == nil { if m.shelf == nil {
m.shelf = []book.Book{} m.shelf = []media.Book{}
} }
return m.shelf, nil return m.shelf, nil
} }
func (m *Memory) AddBook(_ context.Context, b *book.Book) error { func (m *Memory) AddBook(_ context.Context, b *media.Book) error {
m.lock.Lock() m.lock.Lock()
defer m.lock.Unlock() defer m.lock.Unlock()
if m.shelf == nil { if m.shelf == nil {
m.shelf = []book.Book{} m.shelf = []media.Book{}
} }
m.shelf = append(m.shelf, *b) m.shelf = append(m.shelf, *b)
return nil return nil
} }
func (m *Memory) UpdateBook(_ context.Context, old, new *book.Book) error { func (m *Memory) UpdateBook(_ context.Context, old, new *media.Book) error {
m.lock.Lock() m.lock.Lock()
defer m.lock.Unlock() defer m.lock.Unlock()
if m.shelf == nil { if m.shelf == nil {
m.shelf = []book.Book{} m.shelf = []media.Book{}
return fmt.Errorf("book does not exist") return fmt.Errorf("book does not exist")
} }
@ -58,12 +58,12 @@ func (m *Memory) UpdateBook(_ context.Context, old, new *book.Book) error {
return fmt.Errorf("book does not exist") return fmt.Errorf("book does not exist")
} }
func (m *Memory) DeleteBook(_ context.Context, b *book.Book) error { func (m *Memory) DeleteBook(_ context.Context, b *media.Book) error {
m.lock.Lock() m.lock.Lock()
defer m.lock.Unlock() defer m.lock.Unlock()
if m.shelf == nil { if m.shelf == nil {
m.shelf = []book.Book{} m.shelf = []media.Book{}
return fmt.Errorf("book does not exist") return fmt.Errorf("book does not exist")
} }

View File

@ -10,7 +10,7 @@ import (
"strings" "strings"
"time" "time"
"git.yetaga.in/alazyreader/library/book" "git.yetaga.in/alazyreader/library/media"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
@ -142,12 +142,12 @@ func (m *MySQL) RunMigrations(ctx context.Context) (int, int, error) {
return latestMigrationRan, migrationsRun, err return latestMigrationRan, migrationsRun, err
} }
func (m *MySQL) GetAllBooks(ctx context.Context) ([]book.Book, error) { func (m *MySQL) GetAllBooks(ctx context.Context) ([]media.Book, error) {
if m.connection == nil { if m.connection == nil {
return nil, fmt.Errorf("uninitialized mysql client") return nil, fmt.Errorf("uninitialized mysql client")
} }
books := []book.Book{} books := []media.Book{}
rows, err := m.connection.QueryContext(ctx, ` rows, err := m.connection.QueryContext(ctx, `
SELECT id, SELECT id,
title, title,
@ -173,7 +173,7 @@ func (m *MySQL) GetAllBooks(ctx context.Context) ([]book.Book, error) {
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
b := book.Book{} b := media.Book{}
var authors string var authors string
err := rows.Scan( err := rows.Scan(
&b.ID, &b.Title, &authors, &b.ID, &b.Title, &authors,
@ -192,7 +192,7 @@ func (m *MySQL) GetAllBooks(ctx context.Context) ([]book.Book, error) {
return books, nil return books, nil
} }
func (m *MySQL) AddBook(ctx context.Context, b *book.Book) error { func (m *MySQL) AddBook(ctx context.Context, b *media.Book) error {
if m.connection == nil { if m.connection == nil {
return fmt.Errorf("uninitialized mysql client") return fmt.Errorf("uninitialized mysql client")
} }
@ -232,7 +232,7 @@ func (m *MySQL) AddBook(ctx context.Context, b *book.Book) error {
return nil return nil
} }
func (m *MySQL) UpdateBook(ctx context.Context, old, new *book.Book) error { func (m *MySQL) UpdateBook(ctx context.Context, old, new *media.Book) error {
if m.connection == nil { if m.connection == nil {
return fmt.Errorf("uninitialized mysql client") return fmt.Errorf("uninitialized mysql client")
} }

View File

@ -5,17 +5,17 @@ import (
"io" "io"
"strings" "strings"
"git.yetaga.in/alazyreader/library/book" "git.yetaga.in/alazyreader/library/media"
) )
func CSVToBooks(r io.Reader) ([]book.Book, error) { func CSVToBooks(r io.Reader) ([]media.Book, error) {
reader := csv.NewReader(r) reader := csv.NewReader(r)
header, err := reader.Read() header, err := reader.Read()
if err != nil { if err != nil {
return nil, err return nil, err
} }
hmap := parseHeader(header) hmap := parseHeader(header)
books := []book.Book{} books := []media.Book{}
for { for {
row, err := reader.Read() row, err := reader.Read()
@ -25,7 +25,7 @@ func CSVToBooks(r io.Reader) ([]book.Book, error) {
if err != nil { if err != nil {
return books, err return books, err
} }
b := book.Book{ b := media.Book{
Title: row[hmap["title"]], Title: row[hmap["title"]],
Authors: parseAuthors(row[hmap["author"]]), Authors: parseAuthors(row[hmap["author"]]),
SortAuthor: row[hmap["authorlast"]], SortAuthor: row[hmap["authorlast"]],

View File

@ -1,4 +1,4 @@
package book package media
type Book struct { type Book struct {
ID int `json:"-"` ID int `json:"-"`
@ -19,3 +19,6 @@ type Book struct {
OnLoan string `json:"onLoan"` OnLoan string `json:"onLoan"`
CoverURL string `json:"coverURL"` CoverURL string `json:"coverURL"`
} }
type Record struct {
}

View File

@ -4,7 +4,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"git.yetaga.in/alazyreader/library/book" "git.yetaga.in/alazyreader/library/media"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
) )
@ -361,19 +361,19 @@ func (l *List) SetMembers(lkv []ListKeyValue) {
type BookDetails struct { type BookDetails struct {
x, y int x, y int
h, w int h, w int
book *book.Book book *media.Book
style tcell.Style style tcell.Style
visible bool visible bool
} }
func NewBookDetails(b *book.Book) *BookDetails { func NewBookDetails(b *media.Book) *BookDetails {
return &BookDetails{ return &BookDetails{
book: b, book: b,
visible: true, visible: true,
} }
} }
func (l *BookDetails) SetBook(b *book.Book) { func (l *BookDetails) SetBook(b *media.Book) {
l.book = b l.book = b
} }