book

package
v0.0.0-...-b69cb46 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is used when a specific Product is requested but does not exist.
	ErrNotFound = errors.New("not found")

	// ErrInvalidID occurs when an ID is not in a valid form.
	ErrInvalidID = errors.New("ID is not in its proper form")
)

Functions

This section is empty.

Types

type Info

type Info struct {
	ID            string `db:"book_id" json:"book_id"`
	AuthorID      string `db:"author_id" json:"author_id,omitempty"`
	AuthorName    string `db:"author_name" json:"author_name"`
	Title         string `db:"title" json:"title"`
	PublishedDate string `db:"published_date" json:"published_date"`
	ImageUrl      string `db:"image_url" json:"image_url"`
	Description   string `db:"description" json:"description"`
}

Info is the book model.

func (Info) ToDto

func (b Info) ToDto() Info

ToDto formats the database model for client consumption.

type Infos

type Infos []Info

func (Infos) ToDto

func (bs Infos) ToDto() []Info

ToDto formats the database model for client consumption.

type NewBook

type NewBook struct {
	AuthorName    string `json:"author_name" validate:"required,alpha_space"`
	Title         string `json:"title" validate:"required"`
	PublishedDate string `json:"published_date" validate:"required,date"`
	ImageUrl      string `json:"image_url" validate:"required,url,max=255"`
	Description   string `json:"description" validate:"required,max=255"`
}

NewBook is the required data for creating a new book.

type Repo

type Repo interface {
	Query() (Infos, error)
	QueryByID(bookID string) (Info, error)
	QueryByTitle(bookTitle string) (Info, error)
	Create(book NewBook) (Info, error)
	Delete(bookID string) error
}

Repo is the interface for the book repository.

type RepositoryDb

type RepositoryDb struct {
	Db *sqlx.DB
}

BookRepositoryDb defines the repository for the book service.

func New

func New(db *sqlx.DB) *RepositoryDb

New returns a pointer to a book repo.

func (*RepositoryDb) Create

func (r *RepositoryDb) Create(book NewBook) (Info, error)

Create adds a new book to the database. It returns the created book with fields like ID and Author_ID populated.

func (*RepositoryDb) Delete

func (r *RepositoryDb) Delete(bookID string) error

Delete removes a book by ID from the database.

func (*RepositoryDb) Query

func (r *RepositoryDb) Query() (Infos, error)

Query retrieves all books from the database.

func (*RepositoryDb) QueryByID

func (r *RepositoryDb) QueryByID(bookID string) (Info, error)

QuerybyID retrieves a book by ID from the database.

func (*RepositoryDb) QueryByTitle

func (r *RepositoryDb) QueryByTitle(bookTitle string) (Infos, error)

QuerybyTitle retrieves a book by quering the title from the database.

Jump to

Keyboard shortcuts

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