models

package
v0.0.0-...-9b15d4d Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrNotFound is returned when a resource cannot be found
	// in the database
	ErrNotFound modelError = "models: resource not found"

	// ErrUserNotFound is returned when a resource cannot be found
	// in the database
	ErrUserNotFound modelError = "models: resource not found"
	// ErrIncorrectPassword is returned when a password does not match the value
	// in the database
	ErrIncorrectPassword modelError = "models: incorrect password provided"
	// ErrPasswordTooShort is returned when a password does not meet
	// length requirements
	ErrPasswordTooShort modelError = `models: password provided was too short.
	 It must be at least 8 characters`
	// ErrPasswordRequired is returned when a password is not provided
	// on Create
	ErrPasswordRequired modelError = `models: password is required`
	// ErrPasswordHashRequired is returned when a password hash is not provided
	ErrPasswordHashRequired modelError = `models: password hash is required`
	// ErrInvalidEmail is returned when a password does not match our
	// email regexp
	ErrInvalidEmail modelError = "models: email address is not valid"
	// ErrEmailRequired is returned when an email is the empty string
	ErrEmailRequired modelError = "models: email address is required"
	// ErrEmailAlreadyTaken is returned on Update or Create if
	// an email address is already in use
	ErrEmailAlreadyTaken modelError = "models: email address is already taken"

	// ErrInvalidID is returned when an invalid ID is provided
	// to a method like Delete
	ErrInvalidID privateError = "models: ID provided was invalid"
	// ErrRememberTooShort is returned when a password does not meet
	// length requirements
	ErrRememberTooShort privateError = `models: remember provided was too short.\n
	It must be at least 32 bytes`
	// ErrRememberHashRequired is returned when a create or update is attempted and
	// a remember token hash is not provided
	ErrRememberHashRequired privateError = `models: remember hash is required`
	// ErrUserIDRequired when user id is not provided on create (gallery)
	ErrUserIDRequired privateError = `models: user id is required`
	// ErrTitleRequired  when title is not provided on create (gallery)
	ErrTitleRequired modelError = `models: title is required`
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Content

type Content struct {
	URL       string    `json:"url"`
	Country   string    `json:"country"`
	Location  string    `json:"location"`
	Content   string    `json:"content"`
	Embedding []float64 `json:"embedding"`
	Latitude  float64   `json:"latitude"`
	Longitude float64   `json:"longitude"`
}

Content implements the content table for storing travel content data - this consists of the source URL, the content, and the embedding

type ContentDB

type ContentDB interface {
	ByCountryAndSimilarity(country string, embedding []float64) ([]Content, error)
	BySimilarity(embedding []float64) ([]Content, error)
	Countries() ([]string, error)

	// Methods for altering contents
	Create(content *Content) error
	Update(content *Content) error
	Delete(url string) error
	DbCloser
}

type ContentService

type ContentService interface {
	ContentDB
}

ContentService is a set of methods used to manipulate and work with the content model

func NewContentService

func NewContentService(db *badger.DB, similarityThreshold float64) ContentService

NewContentService initialises a ContentService object with an open connection to the db.

type DbCloser

type DbCloser interface {
	CloseDB() error
}

type Services

type Services struct {
	User    UserService
	Content ContentService
}

Services contains the services which need db connection

func NewServices

func NewServices(cfgs ...ServicesConfig) (*Services, error)

NewServices initialises all services with a single db connection

func (*Services) Close

func (s *Services) Close() error

Close closes the database connections.

type ServicesConfig

type ServicesConfig func(*Services) error

ServicesConfig allows for dynamic adding of services

func WithContent

func WithContent(similarityThreshold float64) ServicesConfig

WithContent returns a ServicesConfig object that sets content

func WithUser

func WithUser(hmacKey, pepper string) ServicesConfig

WithUser returns a ServicesConfig object that sets a user

type User

type User struct {
	Email        string `json:"email"`
	Password     string
	PasswordHash string `json:"passwordHash"`
	Remember     string
	RememberHash string `json:"rememberHash"`
}

User implements the User table for storing account data - this consists of the Email and hashed password

type UserDB

type UserDB interface {
	ByEmail(email string) (*User, error)
	// For cookies
	ByRemember(token string) (*User, error)

	// Methods for altering users
	Create(user *User) error
	Update(user *User) error
	Delete(email string) error
	DbCloser
}

UserDB is userd to interact with the users database. For pretty much all single user queries: 1) If the user is found, we will return a nil error 2) If the user is not found, we will return ErrNotFound 3) If any other error occurs, we will return and error with more information about what went wrong. This may not be generated by the errors package

For single user queries any error by ErrNotFound should probably result in a 500 error

type UserService

type UserService interface {
	// Authenticate will verify the provided email address and
	// password are correct. If they are correct, the user corresponding
	// to that email will be returned. Otherwise you will recieve either:
	// ErrNotFound, ErrInvalidPassword, or another error if something
	// goes wrong
	Authenticate(email, password string) (*User, error)
	UserDB
}

UserService is a set of methods used to manipulate and work with the user model

func NewUserService

func NewUserService(db *badger.DB, hmacKey, pepper string) UserService

NewUserService initialises a UserService object with an open connection to the db.

type UserValidator

type UserValidator interface {
	UserDB
}

UserValidator allows for validation of the User object

func NewUserValidator

func NewUserValidator(db UserDB, hmac hash.HMAC, pepper string) UserValidator

NewUserValidator initialises a UserService object with an open connection to the db.

Jump to

Keyboard shortcuts

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