flashcard

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2023 License: MIT Imports: 7 Imported by: 1

README

flashcard

golangci-lint Go Reference

Description

A Go package for flashcard mainipulation and learning.

⚡️ The project is in rapid stage of development, issues and critics are welcome.

Features

  • expressive API
  • sqlite as a storage; others can be added via implementation of storage.CardRepo and storage.DeckRepo interfaces

Table of contents

Getting started

See the documentation for further details.

Installation
go get github.com/k1gabyt0/flashcard
Usage
1. Open a storage

There are 2 supported right now:

  • inmemory - doesn't persist anything on disk
  • sqlite3 - persists on disk using sqlite3
var store storage.Storage
var err error

switch storageType {
case "inmemory":
  store = inmemory.Open()
case "sqlite3":
  store, err = sqlite.Open("dbDir", "dbName")
  handleErr(err)
default:
  panic("unknown store type: " + sto rageType)
}
defer store.Close()
2. Create and use a deck manager

Create a deck manager with passed storage.

deckManager := flashcard.NewManager(store)

deckManager is an entry point for managing decks and cards.

Managing decks

Decks can only be created by deckManager:

deck, err := deckManager.CreateDeck("English")
handleErr(err)

A deck is a smart object that represents a deck of flashcards. It can handle renaming, deletion, searching and creating cards, and more.

_ = deck.Rename("Engrishu")
count, _ := deck.Count()
cards, _ := deck.ListAllCards()
_ = deck.Delete()
Managing cards

Only deck can create cards:

card, _ := deck.AddCard("cat", "кошка")
cats, _ := deck.FindCards("cat", "кошка") // len(cats) is 1

A card is a smart object that represents a flashcard. It can handle its updating, deletion, and moving between decks.

_ = card.Update("kitten", "котенок")
front := card.Front()
back := card.Back()

animalsDeck, _ := deckManager.CreateDeck("Animals")
_ = card.PutInDeck(animalsDeck)
_ = card.Delete()
Deck and card validations

Decks and cards have basic provided(via valdy package) validations.

Decks:

  • Not empty name
  • Unique name

Cards:

  • Not empty front
  • Not empty back

Single validation that is responsible for validating card's front not-emptiness looks like:


// ErrEmptyCardFront is a corresponding error for NotEmptyCardFront
var ErrEmptyCardFront = errors.New("card has empty front value")

// NotEmptyCardFront is a rule that checks front's non-emptyness.
var NotEmptyCardFront valdy.ValidationFunc[value.Card] = func(card value.Card) error {
	if card.Front == "" {
		return valdy.ErrorFrom(ErrEmptyCardFront).Wrap(
			fmt.Errorf("card's front value should not be empty"),
		)
	}
	return nil
}

Validations can be set in deckManager:

deckManager.SetCardValidations(cardValidations)
deckManager.SetDeckValidations(deckValidations)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Card

type Card interface {
	GetDeck() Deck
	GetValue() value.Card

	Front() string
	SetFront(val string) error

	Back() string
	SetBack(val string) error

	Update(front, back string) error

	PutInDeck(deck Deck) error
	Delete() error
}

Card describes flashcard, stored in some deck.

type Deck

type Deck interface {
	GetValue() value.Deck

	GetName() string
	Rename(newName string) error

	ListAllCards() ([]Card, error)
	Count() (int, error)
	FilterCards(filterFunc func(value.Card) bool) ([]Card, error)

	FindCards(front, back string) ([]Card, error)
	FindCardsByFront(front string) ([]Card, error)

	AddCard(front, back string) (Card, error)

	Clear() error
	Delete() error
}

Deck is an interface that describes all opertions that deck of flashcards can do.

type DeckManager

type DeckManager interface {
	ListAllDecks() ([]Deck, error)
	Count() (int, error)
	CreateDeck(name string) (Deck, error)
	FindDeckByName(name string) (Deck, error)
	FilterDecks(filterFunc func(value.Deck) bool) ([]Deck, error)

	GetDeckValidations() []valdy.Validation[value.Deck]
	SetDeckValidations(validations ...valdy.Validation[value.Deck])
	GetCardValidations() []valdy.Validation[value.Card]
	SetCardValidations(validations ...valdy.Validation[value.Card])
}

DeckManager is an interface that describes deck management.

func NewManager added in v0.3.1

func NewManager(storage storage.Storage) DeckManager

NewManager creates new deckManager with passed storage.

func NewManagerWithService added in v0.3.1

func NewManagerWithService(service service.FlashcardService) DeckManager

NewManagerWithService creates new deckManager with passed service.

Jump to

Keyboard shortcuts

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