service

package
v0.0.9-alpha Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package service provides methods to work with pastes and users. Methods of this package do not log or print out anything, they return errors instead. It is up to the user of the Service to handle the errors and provide useful information to the end user.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPasteNotFound    = errors.New("paste not found")
	ErrUserNotFound     = errors.New("user not found")
	ErrPasteIsPrivate   = errors.New("paste is private")
	ErrPasteHasPassword = errors.New("paste has password")
	ErrWrongPassword    = errors.New("paste password is incorrect")
	ErrStoreFailure     = errors.New("store opertation failed")
	ErrEmptyBody        = errors.New("body is empty")
	ErrWrongPrivacy     = errors.New("privacy is wrong")
	ErrWrongDuration    = errors.New("wrong duration format")
)

ErrPasteNotFound and other common errors.

Functions

This section is empty.

Types

type PasteRequest

type PasteRequest struct {
	Title           string `json:"title" form:"title"`
	Body            string `json:"body" form:"body" binding:"required"`
	Expires         string `json:"expires" form:"expires" binding:"required"`
	DeleteAfterRead bool   `json:"delete_after_read" form:"delete_after_read" binding:"-"`
	Privacy         string `json:"privacy" form:"privacy" binding:"required"`
	Password        string `json:"password" form:"password"`
	Syntax          string `json:"syntax" form:"syntax" binding:"required"`
	UserID          string `json:"user_id"`
}

PasteRequest is an input to Create method, normally comes from a web form.

type Service

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

Service type provides method to work with pastes and users.

func New

func New(store store.Interface) *Service

New returns new Service with provided store as a back-end storage.

func NewWithMemDB

func NewWithMemDB() *Service

NewWithMemDB returns new Service with memory as a store.

func NewWithPostgres

func NewWithPostgres(conn string) (*Service, error)

NewWithPostgres returns new Service with postgres db as a store.

func (Service) GetCount

func (s Service) GetCount() (pastes, users int64)

GetCount returns total count of pastes and users.

func (Service) GetOrUpdateUser

func (s Service) GetOrUpdateUser(usr store.User) (store.User, error)

GetOrUpdateUser saves the user in the store and returns it.

func (Service) GetPaste

func (s Service) GetPaste(url string, uid string, pwd string) (store.Paste, error)

GetPaste returns a paste given encoded URL. If the paste is private GetPaste will check that it belongs to the user with provided uid. If password is given and the paste has password GetPaste will check that the password is correct.

func (Service) NewPaste

func (s Service) NewPaste(pr PasteRequest) (store.Paste, error)

NewPaste creates new Paste from the request and saves it in the store. Paste.Body is mandatory, Paste.Expires is default to never, Paste.Privacy must be on of ["private","public","unlisted"]. If password is provided it is stored as a hash.

func (Service) UserPastes

func (s Service) UserPastes(uid string) ([]store.Paste, error)

UserPastes returns a list of the last 10 paste for a user.

Jump to

Keyboard shortcuts

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