product

package
v0.0.0-...-576dde9 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotExist = errors.New("resource does not exist")

Functions

func NewPostgresRepository

func NewPostgresRepository(ctx context.Context, db *sqlx.DB, log zerolog.Logger) *postgresRepository

Types

type Category

type Category struct {
	ID        int       `json:"id"`
	Name      string    `json:"name" validate:"required,min=3"`
	CreatedAt time.Time `json:"created_at,omitempty" db:"created_at"`
	UpdatedAt time.Time `json:"updated_at,omitempty" db:"updated_at"`
}

func (*Category) FromJSON

func (c *Category) FromJSON(r io.Reader) error

func (*Category) ToJSON

func (c *Category) ToJSON(w io.Writer) error

func (*Category) Validate

func (c *Category) Validate() error

type Operations

type Operations interface {
	GetProduct(tx context.Context, id int) (*Product, error)
	CreateProduct(tx context.Context, data *Product) (*Product, error)
	ListProducts(tx context.Context, limit int, offset int) (*PaginationResult[*Product], error)
	UpdateProduct(tx context.Context, id int, data *Product) (*Product, error)
	DeleteProduct(tx context.Context, id int) error
	SearchProductsByName(ctx context.Context, searchTerm string) ([]*Product, error)

	GetCategory(ctx context.Context, id int) (*Category, error)
	CreateCategory(ctx context.Context, data *Category) (*Category, error)
	ListCategories(ctx context.Context, limit int, offset int) (*PaginationResult[*Category], error)
	UpdateCategory(ctx context.Context, id int, data *Category) (*Category, error)
	SearchCategoriesByName(ctx context.Context, searchTerm string) ([]*Category, error)
}

type PaginationResult

type PaginationResult[T any] struct {
	Items  []T `json:"items"`
	Limit  int `json:"limit"`
	Offset int `json:"offset"`
	Total  int `json:"total"`
}

type Product

type Product struct {
	ID          int       `json:"id"`
	Name        string    `json:"name" validate:"required"`
	Description string    `json:"description"`
	Price       float32   `json:"price" db:"price" validate:"gt=0"`
	SKU         string    `json:"sku" db:"sku" validate:"required,len=5"`
	ImageURL    string    `json:"image_url" db:"image_url"`
	CategoryID  int       `json:"category_id" db:"category_id" validate:"required"`
	CreatedAt   time.Time `json:"created_at,omitempty" db:"created_at"`
	UpdatedAt   time.Time `json:"updated_at,omitempty" db:"updated_at"`
	DeletedAt   string    `json:"deleted_at,omitempty" db:"deleted_at"`
}

func (*Product) FromJSON

func (p *Product) FromJSON(r io.Reader) error

func (*Product) ToJSON

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

func (*Product) Validate

func (p *Product) Validate() error

type Repository

type Repository interface {
	GetProductByID(ctx context.Context, id int) (*Product, error)
	GetAllProducts(ctx context.Context, limit int, offset int) ([]*Product, error)
	CreateProduct(ctx context.Context, p *Product) (*Product, error)
	UpdateProduct(ctx context.Context, p *Product) (*Product, error)
	GetProductsByCategory(ctx context.Context, categoryID int) ([]*Product, error)
	DeleteProduct(ctx context.Context, id int) error
	CountProducts(ctx context.Context) (int, error)
	SearchProductsByName(ctx context.Context, searchTerm string) ([]*Product, error)

	GetCategoryByID(ctx context.Context, id int) (*Category, error)
	GetAllCategories(ctx context.Context, limit, offset int) ([]*Category, error)
	CreateCategory(ctx context.Context, name string) (*Category, error)
	UpdateCategory(ctx context.Context, id int, name string) (*Category, error)
	SearchCategoriesByName(ctx context.Context, searchTerm string) ([]*Category, error)
	CountCategories(ctx context.Context) (int, error)
}

type Service

type Service struct {
	// contains filtered or unexported fields
}

func NewService

func NewService(repo Repository) *Service

func (*Service) CreateCategory

func (s *Service) CreateCategory(ctx context.Context, data *Category) (*Category, error)

func (*Service) CreateProduct

func (s *Service) CreateProduct(ctx context.Context, data *Product) (*Product, error)

func (*Service) DeleteProduct

func (s *Service) DeleteProduct(ctx context.Context, id int) error

func (*Service) GetCategory

func (s *Service) GetCategory(ctx context.Context, id int) (*Category, error)

func (*Service) GetProduct

func (s *Service) GetProduct(ctx context.Context, id int) (*Product, error)

func (*Service) ListCategories

func (s *Service) ListCategories(ctx context.Context, limit int, offset int) (*PaginationResult[*Category], error)

func (*Service) ListProducts

func (s *Service) ListProducts(ctx context.Context, limit int, offset int) (*PaginationResult[*Product], error)

func (*Service) SearchCategoriesByName

func (s *Service) SearchCategoriesByName(ctx context.Context, searchTerm string) ([]*Category, error)

func (*Service) SearchProductsByName

func (s *Service) SearchProductsByName(ctx context.Context, searchTerm string) ([]*Product, error)

func (*Service) UpdateCategory

func (s *Service) UpdateCategory(ctx context.Context, id int, data *Category) (*Category, error)

func (*Service) UpdateProduct

func (s *Service) UpdateProduct(ctx context.Context, id int, data *Product) (*Product, error)

Jump to

Keyboard shortcuts

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