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
import (
"git.yetaga.in/alazyreader/library/book"
"git.yetaga.in/alazyreader/library/media"
"github.com/gdamore/tcell"
)
@ -20,16 +20,16 @@ func NewEventError(err error) *EventError {
// save change to book
type EventBookUpdate struct {
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.SetEventNow()
return e
}
func (e *EventBookUpdate) Book() *book.Book {
func (e *EventBookUpdate) Book() *media.Book {
return e.book
}

View File

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

View File

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

View File

@ -5,43 +5,43 @@ import (
"fmt"
"sync"
"git.yetaga.in/alazyreader/library/book"
"git.yetaga.in/alazyreader/library/media"
)
type Memory struct {
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()
defer m.lock.Unlock()
if m.shelf == nil {
m.shelf = []book.Book{}
m.shelf = []media.Book{}
}
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()
defer m.lock.Unlock()
if m.shelf == nil {
m.shelf = []book.Book{}
m.shelf = []media.Book{}
}
m.shelf = append(m.shelf, *b)
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()
defer m.lock.Unlock()
if m.shelf == nil {
m.shelf = []book.Book{}
m.shelf = []media.Book{}
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")
}
func (m *Memory) DeleteBook(_ context.Context, b *book.Book) error {
func (m *Memory) DeleteBook(_ context.Context, b *media.Book) error {
m.lock.Lock()
defer m.lock.Unlock()
if m.shelf == nil {
m.shelf = []book.Book{}
m.shelf = []media.Book{}
return fmt.Errorf("book does not exist")
}

View File

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

View File

@ -5,17 +5,17 @@ import (
"io"
"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)
header, err := reader.Read()
if err != nil {
return nil, err
}
hmap := parseHeader(header)
books := []book.Book{}
books := []media.Book{}
for {
row, err := reader.Read()
@ -25,7 +25,7 @@ func CSVToBooks(r io.Reader) ([]book.Book, error) {
if err != nil {
return books, err
}
b := book.Book{
b := media.Book{
Title: row[hmap["title"]],
Authors: parseAuthors(row[hmap["author"]]),
SortAuthor: row[hmap["authorlast"]],

View File

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

View File

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