snippets

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound error used to signal higher level about sql.ErrNoRows error

Functions

func ConvertPaginationToSQLExpression

func ConvertPaginationToSQLExpression(pagination service.Pagination) string

ConvertPaginationToSQLExpression converts service.Pagination to SQL expression

func MakeCreateSnippetEndpoint

func MakeCreateSnippetEndpoint(s Service) endpoint.Endpoint

MakeCreateSnippetEndpoint creates a new go-kit Endpoint for POST /snippets method

func MakeDeleteSnippetEndpoint

func MakeDeleteSnippetEndpoint(s Service) endpoint.Endpoint

MakeDeleteSnippetEndpoint creates a new go-kit Endpoint for DELETE /snippets/{snippet_id} method

func MakeGetSnippetEndpoint

func MakeGetSnippetEndpoint(s Service) endpoint.Endpoint

MakeGetSnippetEndpoint creates a new go-kit Endpoint for GET /snippets/{snippet_id} method

func MakeListSnippetsEndpoint

func MakeListSnippetsEndpoint(s Service) endpoint.Endpoint

MakeListSnippetsEndpoint creates a new go-kit Endpoint for GET /snippets method

func MakeSnippetsHandler

func MakeSnippetsHandler(s Service, l log.Logger) chi.Router

MakeSnippetsHandler initialize all endpoints for route /snippets

func NewPagination

func NewPagination(limit uint, offset uint, total uint) service.Pagination

NewPagination creates new service.Pagination with default maximum limit

Types

type ListSnippetsRequest

type ListSnippetsRequest struct {
	Limit  uint
	Offset uint
}

ListSnippetsRequest represents a request struct for GET /snippets?limit=<x>&offset=<y> method

type PGStorage

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

PGStorage implements storage interface and provides methods to manipulate data in PostgreSQL storage

func NewPGStorage

func NewPGStorage(conn *sql.DB) *PGStorage

NewPGStorage returns a new instance of PGStorage

func (*PGStorage) Create

func (pg *PGStorage) Create(ctx context.Context, snippet Snippet) (uint, error)

Create saves a single snippet to storage

func (*PGStorage) Get

func (pg *PGStorage) Get(ctx context.Context, id uint) (Snippet, error)

Get returns a single snippet from storage

func (*PGStorage) List

func (pg *PGStorage) List(ctx context.Context, pagination service.Pagination) ([]Snippet, error)

List returns a list of snippets from storage

func (*PGStorage) SoftDelete

func (pg *PGStorage) SoftDelete(ctx context.Context, id uint) error

SoftDelete set `expires_at` to `now()`, so snippet is considered deleted

func (*PGStorage) Total

func (pg *PGStorage) Total(ctx context.Context) (uint, error)

Total counts a total number of snippets

type Service

type Service interface {
	Get(ctx context.Context, id uint) (Snippet, *service.Error)
	Create(ctx context.Context, snippet Snippet) (Snippet, *service.Error)
	List(ctx context.Context, limit uint, offset uint) ([]Snippet, service.Pagination, *service.Error)
	SoftDelete(ctx context.Context, id uint) *service.Error
}

Service is used to manipulate data over snippets

type Snippet

type Snippet struct {
	ID        uint
	Title     string
	Content   string
	CreatedAt time.Time
	UpdatedAt time.Time
	ExpiresAt time.Time
}

Snippet model struct

type SnippetService

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

SnippetService represents service struct. It holds storage and logger.

func NewService

func NewService(storage Storage, logger log.Logger) *SnippetService

NewService returns new instance of SnippetService

func (*SnippetService) Create

func (s *SnippetService) Create(ctx context.Context, snippet Snippet) (Snippet, *service.Error)

Create creates a single snippet

func (*SnippetService) Get

func (s *SnippetService) Get(ctx context.Context, id uint) (Snippet, *service.Error)

Get returns a single snippet

func (*SnippetService) List

func (s *SnippetService) List(ctx context.Context, limit uint, offset uint) ([]Snippet, service.Pagination, *service.Error)

List returns a list of snippets and a pagination struct

func (*SnippetService) SoftDelete

func (s *SnippetService) SoftDelete(ctx context.Context, id uint) *service.Error

SoftDelete mark a single snippet as deleted

type Storage

type Storage interface {
	Get(ctx context.Context, id uint) (Snippet, error)
	Create(ctx context.Context, snippet Snippet) (uint, error)
	List(ctx context.Context, pagination service.Pagination) ([]Snippet, error)
	SoftDelete(ctx context.Context, id uint) error
	Total(ctx context.Context) (uint, error)
}

Storage is used to manipulate data in DB

Jump to

Keyboard shortcuts

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