domain

package
v0.0.0-...-cddfa53 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

revive:disable until there is a central package.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCartInvalidUserID    = errors.New("invalid user id cart")
	ErrCartInvalidProductID = errors.New("invalid product id cart")
	ErrNoCartsFound         = errors.New("no carts found")
)
View Source
var (
	ErrDuplicatedCategory = errors.New("duplicated category")
	ErrNoCategoryFound    = errors.New("no categories found")
	ErrCategoryConflict   = errors.New("update conflict error")
)
View Source
var (
	ErrInvalidProductCategory = errors.New("invalid category")
	ErrDuplicatedProduct      = errors.New("duplicated product")
	ErrNoProductsFound        = errors.New("products not found")
	ErrProductConflict        = errors.New("update conflict error")
)
View Source
var (
	// ErrInvalidToken returns when requested token is not found in store.
	ErrInvalidToken = errors.New("invalid user token")
	ErrNoTokenFound = errors.New("token not found")
)
View Source
var (
	ErrDuplicatedUserEmail = errors.New("duplicated email")
	ErrNoUsersFound        = errors.New("no users to list")
)
View Source
var (
	ErrNoSearchResult = errors.New("requested resource not found")
)

Functions

func CompareHashAndPassword

func CompareHashAndPassword(hashedPassword []byte, password []byte) error

CompareHashAndPassword compares hash and plaintext password.

func GenerateHashedPassword

func GenerateHashedPassword(password []byte) ([]byte, error)

GenerateHashedPassword generates hashed password.

func HashToken

func HashToken(s string) []byte

HashToken hashes token.

Types

type Cart

type Cart struct {
	ID        int `json:"id"`
	ProductID int `json:"product_id" db:"product_id"`
	Quantity  int `json:"quantity" db:"quantity"`
	UserID    int `json:"user_id" db:"user_id"`
}

Cart represents carts model.

type CartCreate

type CartCreate struct {
	UserID    int `json:"user_id"`
	ProductID int `json:"product_id"`
	Quantity  int `json:"quantity"`
}

CartCreate represents carts model for POST requests.

func (CartCreate) CreateModel

func (c CartCreate) CreateModel() Cart

CreateModel set input values to a new struct and return a new instance.

func (CartCreate) Validate

func (c CartCreate) Validate() error

Validate validates update products.

type CartFilter

type CartFilter struct {
	ID     int `json:"id"`
	UserID int `json:"user_id"`

	Limit  int    `json:"limit"`
	Offset int    `json:"offset"`
	Sort   string `json:"sort"`
}

CartFilter represents filters passed to List.

type CartService

type CartService interface {
	GetByUser(ctx context.Context, userID int) ([]Cart, error)
	GetByID(ctx context.Context, ID int) (Cart, error)
	List(ctx context.Context, filter CartFilter) ([]Cart, error)
	Create(ctx context.Context, cart *Cart) error
	Update(ctx context.Context, ID int, cart CartUpdate) (Cart, error)
	Delete(ctx context.Context, userID int) error
}

CartService represents a service for managing carts.

type CartUpdate

type CartUpdate struct {
	ProductID *int `json:"product_id"`
	Quantity  *int `json:"quantity"`
}

CartUpdate represents carts model for PATCH requests.

func (CartUpdate) UpdateModel

func (c CartUpdate) UpdateModel(cart *Cart)

UpdateModel checks whether carts input are not nil and set values.

func (CartUpdate) Validate

func (c CartUpdate) Validate() error

Validate validates update products.

type Category

type Category struct {
	ID          int       `json:"id"`
	Name        string    `json:"name"`
	Description string    `json:"description"`
	CreatedAt   time.Time `json:"-" db:"created_at"`
	UpdatedAt   time.Time `json:"-" db:"updated_at"`
	Version     int       `json:"version"`
}

Category represents categories model.

type CategoryCreate

type CategoryCreate struct {
	Name        string `json:"name" validate:"required"`
	Description string `json:"description" validate:"required"`
}

CategoryCreate represents categories model for POST requests.

func (CategoryCreate) CreateModel

func (c CategoryCreate) CreateModel() Category

CreateModel set input values to a new struct and return a new instance.

func (CategoryCreate) Validate

func (c CategoryCreate) Validate() error

Validate validates POST requests model.

type CategoryFilter

type CategoryFilter struct {
	ID   int    `json:"int"`
	Name string `json:"name"`
	Sort string `json:"sort"`

	Limit  int `json:"limit"`
	Offset int `json:"offset"`
}

CategoryFilter represents filters passed to List.

type CategoryService

type CategoryService interface {
	Create(ctx context.Context, category *Category) error
	GetByID(ctx context.Context, ID int) (Category, error)
	Update(ctx context.Context, ID int, category CategoryUpdate) (Category, error)
	Delete(ctx context.Context, ID int) error
	List(ctx context.Context, filter CategoryFilter) ([]Category, error)
}

CategoryService represents a service for managing categories.

type CategoryUpdate

type CategoryUpdate struct {
	Name        *string `json:"name" validate:"omitempty,required"`
	Description *string `json:"description" validate:"omitempty,required"`
	Version     int     `json:"version" validate:"required"`
}

CategoryUpdate represents categories model for PATCH requests.

func (CategoryUpdate) UpdateModel

func (c CategoryUpdate) UpdateModel(category *Category)

UpdateModel checks whether input are not nil and set values.

func (CategoryUpdate) Validate

func (c CategoryUpdate) Validate() error

Validate validates PATCH requests model.

type Product

type Product struct {
	ID          int       `json:"id"`
	Name        string    `json:"name"`
	Description string    `json:"description"`
	CategoryID  int       `json:"category_id" db:"category_id"`
	Price       int       `json:"price"`
	Quantity    int       `json:"quantity"`
	CreatedAt   time.Time `json:"-" db:"created_at"`
	UpdatedAt   time.Time `json:"-" db:"updated_at"`
	Version     int       `json:"version"`
}

Product represents products model.

type ProductCreate

type ProductCreate struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	CategoryID  int    `json:"category_id"`
	Price       int    `json:"price"`
	Quantity    int    `json:"quantity"`
}

ProductCreate represents products model for POST requests.

func (ProductCreate) CreateModel

func (p ProductCreate) CreateModel() Product

CreateModel set input values to a new struct and return a new instance.

func (ProductCreate) Validate

func (p ProductCreate) Validate() error

Validate validates POST requests model.

type ProductFilter

type ProductFilter struct {
	ID         int `json:"id"`
	CategoryID int `json:"category"`

	Limit  int    `json:"limit"`
	Offset int    `json:"offset"`
	Sort   string `json:"sort"`
}

ProductFilter represents filters passed to List.

type ProductService

type ProductService interface {
	Create(ctx context.Context, product *Product) error
	GetByID(ctx context.Context, ID int) (Product, error)
	Update(ctx context.Context, ID int, product ProductUpdate) (Product, error)
	Delete(ctx context.Context, ID int) error
	List(ctx context.Context, filter ProductFilter) ([]Product, error)
}

ProductService represents a service for managing products.

type ProductUpdate

type ProductUpdate struct {
	Name        *string `json:"name"`
	Description *string `json:"description"`
	CategoryID  *int    `json:"category_id"`
	Price       *int    `json:"price"`
	Quantity    *int    `json:"quantity"`
	Version     int     `json:"version"`
}

ProductUpdate represents products model for PATCH requests.

func (ProductUpdate) UpdateModel

func (p ProductUpdate) UpdateModel(product *Product)

UpdateModel checks whether products input are not nil and set values.

func (ProductUpdate) Validate

func (p ProductUpdate) Validate() error

Validate validates PATCH requests model.

type Search struct {
	Prodcuts   *Product  `json:"products,omitempty"`
	Categories *Category `json:"categories,omitempty"`
}

Search represents search results to users.

type Searcher

type Searcher interface {
	Search(ctx context.Context, query string) ([]Search, error)
}

Searcher searchs in database for asked query.

type Token

type Token struct {
	Hashed []byte    `json:"-"`
	Plain  string    `json:"plain_token" db:"-"`
	UserID int       `json:"-" db:"user_id"`
	Expiry time.Time `json:"expiry"`
}

Token represents token model.

func GenerateToken

func GenerateToken(id int, length int, expiry time.Duration) (Token, error)

GenerateToken returns generated token.

type TokenService

type TokenService interface {
	Create(ctx context.Context, token Token) error
	GetUserID(ctx context.Context, hashedToken string) (int, error)
}

TokenService represents a service for managing tokens.

type User

type User struct {
	ID       int    `json:"id"`
	Email    string `json:"email"`
	Password []byte `json:"-" db:"password_hash"`
}

User represents users model.

type UserCreate

type UserCreate struct {
	Email    string `json:"email" validate:"required,email,lte=500"`
	Password string `json:"password" validate:"required,gte=8,lte=72"`
}

UserCreate represents users model for POST requests.

func (UserCreate) CreateModel

func (u UserCreate) CreateModel(password []byte) User

CreateModel set input values and password to a new struct and return a new instance.

func (UserCreate) Validate

func (u UserCreate) Validate() error

Validate validates POST requests model.

type UserFilter

type UserFilter struct {
	Email string `json:"email"`
	ID    int    `json:"id"`

	Limit  int    `json:"limit"`
	Offset int    `json:"offset"`
	Sort   string `json:"sort"`
}

UserFilter represents filters passed to /users requests.

type UserLogin

type UserLogin struct {
	Email    string `json:"email" validate:"required"`
	Password string `json:"password" validate:"required"`
}

UserLogin represents users model for login requests.

func (UserLogin) Validate

func (u UserLogin) Validate() error

Validate validates PATCH requests model.

type UserService

type UserService interface {
	Create(ctx context.Context, user *User) error
	GetByID(ctx context.Context, ID int) (User, error)
	GetByEmail(ctx context.Context, email string) (User, error)
	Delete(ctx context.Context, ID int) error
	List(ctx context.Context, filter UserFilter) ([]User, error)
}

UserService represents a service for managing users.

type WrapCart

type WrapCart struct {
	Cart Cart `json:"cart"`
}

WrapCart wraps carts for user representation.

type WrapCartList

type WrapCartList struct {
	Carts []Cart `json:"carts"`
}

WrapCartList wraps list of carts for user representation.

type WrapCategory

type WrapCategory struct {
	Category Category `json:"category"`
}

WrapCategory wraps categories for user representation.

type WrapCategoryList

type WrapCategoryList struct {
	Categories []Category `json:"categories"`
}

WrapCategoryList wraps list of list categories for user representation.

type WrapProduct

type WrapProduct struct {
	Product Product `json:"product"`
}

WrapProduct wraps products for user representation.

type WrapProductList

type WrapProductList struct {
	Products []Product `json:"products"`
}

WrapProductList wraps list of products for user representation.

type WrapToken

type WrapToken struct {
	Token Token `json:"token"`
}

WrapToken wraps token for user representation.

type WrapUser

type WrapUser struct {
	User User `json:"user"`
}

WrapUser wraps users for user representation.

type WrapUserList

type WrapUserList struct {
	Users []User `json:"users"`
}

WrapUserList wraps list of users for user representation.

Jump to

Keyboard shortcuts

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