database

package
v0.0.0-...-9d33fa8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectToDB

func ConnectToDB(dbPath string) (*sql.DB, error)

func Init

func Init(dbPath string) *sql.DB

Init initializes the database connection and runs the CreateTable function for us in one place, helping declutter the main function.

Types

type Book

type Book struct {
	// all field tags must be exported through capitalization
	// if they are to be used in the JSON encodings
	ID int `json:"id,omitempty"`
	// `omitempty` excludes the field from the JSON encoding if its empty
	ISBN   int    `json:"isbn,omitempty"`
	Title  string `json:"title"`
	Author string `json:"author"`
	Genres string `json:"genres"`
	Year   int    `json:"year,omitempty"`
}

Book contains the properties that the books stored in the DB have.

type BookShelf

type BookShelf struct {
	DB *sql.DB
}

BookShelf is the struct that implements BookStore here. The 'Book' struct is used to store data when performing operations on the BookShelf. So, a BookShelf stores many books and acts as a small-scale representation of what a BookStore would be.

func NewShelf

func NewShelf(db *sql.DB) *BookShelf

func (*BookShelf) AddBook

func (b *BookShelf) AddBook(decoder *json.Decoder) (*Book, error)

AddBook receives the book in as POST body and adds it to the database

func (*BookShelf) DeleteBook

func (b *BookShelf) DeleteBook(bookID int) (sql.Result, error)

DeleteBook receives the book to deleted as POST body and remvoes it from the database.

func (*BookShelf) GetBook

func (b *BookShelf) GetBook(bookID int) (*Book, error)

GetBook returns a book from the database given its ID

func (*BookShelf) GetBooks

func (b *BookShelf) GetBooks() ([]Book, error)

GetBooks returns all books from the database

func (*BookShelf) UpdateBook

func (b *BookShelf) UpdateBook(decoder *json.Decoder, bookID int) (*Book, error)

UpdateBook receives the book to be updated as POST body and updates it in the database.

type BookStore

type BookStore interface {
	GetBook(int) (*Book, error)
	GetBooks() ([]Book, error)
	AddBook(*json.Decoder) (*Book, error)
	UpdateBook(*json.Decoder, int) (*Book, error)
	DeleteBook(int) (sql.Result, error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL