database

package
v0.0.0-...-a64592e Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database struct {
	*sql.DB
	// contains filtered or unexported fields
}

func NewDatabase

func NewDatabase(dataSourceName string) *Database

func (*Database) FindByName

func (db *Database) FindByName(ctx context.Context, name string, page uint64) (*Result, error)

Returns the products in the database that contain the keyword

func (*Database) Insert

func (db *Database) Insert(ctx context.Context, p *Product) (sql.Result, error)

Insert product to database, returns error from DB.Exec

type Page

type Page struct {
	Page       uint64     `json:"page"`       // The current page
	TotalPages int        `json:"totalPages"` // Total number of pages
	LastPage   string     `json:"lastPage"`   // Url used to navigate to page n-1
	NextPage   string     `json:"nextPage"`   // Url used to navigate to page n+1
	TotalItems int        `json:"totalItems"` // The total amount of products that match the keyword
	Count      int        `json:"count"`      // The amount of rows returned by this query
	Products   []*Product `json:"products"`   // A slice of *Product
}

func (*Page) ToJSON

func (p *Page) ToJSON(w io.Writer) error

Writes the page as JSON to w

type Product

type Product struct {
	Id                   int     `json:"id"`
	Vendor               string  `json:"vendor"`
	Brand                string  `json:"brand"`
	Name                 string  `json:"name"`
	Price                float64 `json:"price"`
	Image                string  `json:"image"`
	Size                 string  `json:"size"`
	PricePerHundredGrams string  `json:"pricePerHundredGrams"`
}

type Result

type Result struct {
	TotalItems  int        // The total amount of products that match the keyword
	Count       int        // The amount of rows returned by this query
	Products    []*Product // A slice of *Product
	SearchQuery string     // The keyword contained in the product name
	// contains filtered or unexported fields
}

func (*Result) Paginated

func (r *Result) Paginated() *Page

Transform the database result to a pagination response

Jump to

Keyboard shortcuts

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