db

package
v0.0.0-...-1cbc9ae Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	QuizTypeTranslations        = "translations"
	QuizTypeReverseTranslations = "rTranslations"
	QuizTypeMeanings            = "meanings"

	QuizTypeDefault = QuizTypeTranslations
)

types of quizzes

Variables

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

ErrNotFound is returned when object not found

Functions

func GenerateID

func GenerateID() string

GenerateID generates new uuid and encodes it to base64

Types

type BoltStorage

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

BoltStorage is a storage implementation using BoltDB

func NewBoltStorage

func NewBoltStorage(db *bolt.DB) (*BoltStorage, error)

NewBoltStorage creates BoltStorage instance and initialize buckets

func (*BoltStorage) Get

func (b *BoltStorage) Get(word string) (DictionaryItem, error)

Get dictionary item from database

func (*BoltStorage) GetQuiz

func (b *BoltStorage) GetQuiz(id string) (Quiz, error)

GetQuiz returns quiz by ID`

func (*BoltStorage) GetUser

func (b *BoltStorage) GetUser(user UserID) (User, error)

GetUser returns user by id

func (*BoltStorage) GetUserDictionary

func (b *BoltStorage) GetUserDictionary(user UserID) (map[UserDictionaryItem]DictionaryItem, error)

GetUserDictionary returns dictionary items for user

func (*BoltStorage) GetUserItem

func (b *BoltStorage) GetUserItem(user UserID, word string) (UserDictionaryItem, error)

GetUserItem returns user dictionary item

func (*BoltStorage) Save

func (b *BoltStorage) Save(item DictionaryItem) error

Save dictionary item to database

func (*BoltStorage) SaveQuiz

func (b *BoltStorage) SaveQuiz(quiz Quiz) error

SaveQuiz saves quiz to database

func (*BoltStorage) SaveUser

func (b *BoltStorage) SaveUser(user User) error

SaveUser saves user to database

func (*BoltStorage) SaveUserItem

func (b *BoltStorage) SaveUserItem(item UserDictionaryItem) error

SaveUserItem saves user dictionary item to database

type DictionaryItem

type DictionaryItem struct {
	Word      string
	Phonetics struct {
		Text  string
		Audio string
	}
	Meanings     []meaning
	Translations []translation
}

DictionaryItem hold data for a single dictionary item

func NewDictionaryItem

func NewDictionaryItem(
	word string,
	dictionaryResponse []dictionaryapi.WordResponse,
	translations map[string]yandexdictionary.TranslationResponse,
) DictionaryItem

NewDictionaryItem creates new dictionary item from dictionary & translations responses

type InMemoryStorage

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

InMemoryStorage is a storage implementation using in-memory maps

func NewInMemoryStorage

func NewInMemoryStorage() *InMemoryStorage

NewInMemoryStorage creates new empty in-memory storage

func (*InMemoryStorage) Get

func (d *InMemoryStorage) Get(word string) (DictionaryItem, error)

Get returns dictionary item by word

func (*InMemoryStorage) GetQuiz

func (d *InMemoryStorage) GetQuiz(id string) (Quiz, error)

GetQuiz returns quiz by ID

func (*InMemoryStorage) GetUser

func (d *InMemoryStorage) GetUser(id UserID) (User, error)

GetUser returns user by ID

func (*InMemoryStorage) GetUserDictionary

func (d *InMemoryStorage) GetUserDictionary(user UserID) (map[UserDictionaryItem]DictionaryItem, error)

GetUserDictionary returns map of user dictionary items

func (*InMemoryStorage) GetUserItem

func (d *InMemoryStorage) GetUserItem(user UserID, word string) (UserDictionaryItem, error)

GetUserItem returns item from user dictionary

func (*InMemoryStorage) Save

func (d *InMemoryStorage) Save(item DictionaryItem) error

Save saves dictionary item

func (*InMemoryStorage) SaveQuiz

func (d *InMemoryStorage) SaveQuiz(q Quiz) error

SaveQuiz saves quiz

func (*InMemoryStorage) SaveUser

func (d *InMemoryStorage) SaveUser(item User) error

SaveUser saves user

func (*InMemoryStorage) SaveUserItem

func (d *InMemoryStorage) SaveUserItem(item UserDictionaryItem) error

SaveUserItem saves UserDictionaryItem

type Quiz

type Quiz struct {
	ID          string
	User        UserID
	Word        string
	DisplayWord string
	Language    string
	Type        string
	Choices     []QuizItem
	Created     time.Time
	Result      *QuizResult
}

Quiz holds data for a single quiz`

func NewQuiz

func NewQuiz(user UserID, word string, displayWord string, lang string, items []QuizItem, qType string) Quiz

NewQuiz creates new quiz

func (*Quiz) SetResult

func (q *Quiz) SetResult(choice int, s Storage) error

SetResult sets checks if choice is correct and saves result

type QuizItem

type QuizItem struct {
	Word    string
	Text    string
	Correct bool
}

QuizItem holds data for a single quiz item

type QuizResult

type QuizResult struct {
	Choice  int
	Correct bool
}

QuizResult holds data for a quiz result

type RedisStorage

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

RedisStorage is a storage implementation using redis

func NewRedisStorage

func NewRedisStorage(url string) (*RedisStorage, error)

NewRedisStorage creates RedisStorage with given url

func (*RedisStorage) Get

func (s *RedisStorage) Get(word string) (DictionaryItem, error)

Get word from redis

func (*RedisStorage) GetQuiz

func (s *RedisStorage) GetQuiz(id string) (Quiz, error)

GetQuiz from redis

func (*RedisStorage) GetUser

func (s *RedisStorage) GetUser(id UserID) (User, error)

GetUser from redis

func (*RedisStorage) GetUserDictionary

func (s *RedisStorage) GetUserDictionary(user UserID) (map[UserDictionaryItem]DictionaryItem, error)

GetUserDictionary from redis

func (*RedisStorage) GetUserItem

func (s *RedisStorage) GetUserItem(user UserID, word string) (UserDictionaryItem, error)

GetUserItem returns user item from redis

func (*RedisStorage) Save

func (s *RedisStorage) Save(item DictionaryItem) error

Save word to redis

func (*RedisStorage) SaveQuiz

func (s *RedisStorage) SaveQuiz(q Quiz) error

SaveQuiz to redis

func (*RedisStorage) SaveUser

func (s *RedisStorage) SaveUser(user User) error

SaveUser to redis

func (*RedisStorage) SaveUserItem

func (s *RedisStorage) SaveUserItem(item UserDictionaryItem) error

SaveUserItem to redis

type Storage

type Storage interface {
	// Get dictionary item by word
	Get(string) (DictionaryItem, error)
	// Save dictionary item to DB
	Save(DictionaryItem) error

	// GetUser returns user by ID
	GetUser(UserID) (User, error)
	// SaveUser saves user to DB
	SaveUser(User) error

	// GetUserDictionary returns item from user dictionary
	GetUserItem(UserID, string) (UserDictionaryItem, error)
	// SaveUserItem saves UserDictionaryItem
	SaveUserItem(UserDictionaryItem) error
	// GetUserDictionary returns map of user dictionary items
	GetUserDictionary(UserID) (map[UserDictionaryItem]DictionaryItem, error)

	// SaveQuiz saves quiz to DB
	SaveQuiz(Quiz) error
	// GetQuiz returns quiz by ID
	GetQuiz(string) (Quiz, error)
}

Storage defines method provided by database interfaces

type User

type User struct {
	ID       UserID
	IsAdmin  bool
	Username string
	Language string
	Config   UserConfig
}

User holds user data

type UserConfig

type UserConfig struct {
	QuizType *string
}

UserConfig holds user config params

type UserDictionaryItem

type UserDictionaryItem struct {
	Word     string
	User     UserID
	Created  time.Time
	LastQuiz *time.Time
}

UserDictionaryItem hold data for a single user dictionary item

type UserID

type UserID int64

UserID is a type for users ID

Jump to

Keyboard shortcuts

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