recipes

package
v0.4.7 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// SERVINGS keyword used as part of the url
	SERVINGS = "servings"
	// RECIPE keyword used as part of the url
	RECIPE = "recipe"
	// NAME keyword used as part of the url
	NAME = "name"
	// INGREDIENT keyword used as part of the url
	INGREDIENT = "ingredient"
	// DESCRIPTION keyword used as part of the url
	DESCRIPTION = "description"
)
View Source
const (
	//DATABASE name
	DATABASE = "recipes-manager"
	//RECIPES index
	RECIPES = "recipes"
	//PICTURES index
	PICTURES = "pics"
)
View Source
const (
	//NoAmountIngredient is the amount value for ingredients when this field is not used
	NoAmountIngredient = -1.0
)
View Source
const (
	//NotSupportedError informs clients about operations that are not supported by a Recipes provider
	NotSupportedError = "recipe operation not supported"
)

Variables

This section is empty.

Functions

func AddRecipesAPIToHandler

func AddRecipesAPIToHandler(handler core.Handler, recipes RecipeDB)

AddRecipesAPIToHandler constructs an API for recipes

func RecipeToBsonM

func RecipeToBsonM(searchQuery *RecipeSearchFilter) bson.M

RecipeToBsonM converts a RecipeSearchFilter to a search query (bson.M)

Types

type API

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

API for recipes

type Ingredients

type Ingredients struct {
	//Name of the ingredient
	Name string `json:"name"`
	//Amount needed in a recipe of an ingredient
	Amount float64 `json:"amount"`
	//Unit of the Amount
	Unit string `json:"unit"`
}

Ingredients of a recipe

type MongoRecipeDB

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

MongoRecipeDB implements the Recipe interface to read and write Recipes to and from a Mongo DB

func (*MongoRecipeDB) AddPicture

func (m *MongoRecipeDB) AddPicture(pic *RecipePicture) error

AddPicture to the database

func (*MongoRecipeDB) Clear

func (m *MongoRecipeDB) Clear()

Clear drops all collections

func (*MongoRecipeDB) Close

func (m *MongoRecipeDB) Close() error

Close the connection to the database

func (*MongoRecipeDB) Get

func (m *MongoRecipeDB) Get(id RecipeID) *Recipe

Get a recipe by ID

func (*MongoRecipeDB) GetByName

func (m *MongoRecipeDB) GetByName(name string) (*Recipe, error)

GetByName a recipe from the database

func (*MongoRecipeDB) IDs

func (m *MongoRecipeDB) IDs(searchQuery *RecipeSearchFilter) RecipeList

IDs lists all ids of all recipes

func (*MongoRecipeDB) Insert

func (m *MongoRecipeDB) Insert(recipe *Recipe) error

Insert a recipe into the database

func (*MongoRecipeDB) List

func (m *MongoRecipeDB) List() (recipes []*Recipe)

List all recipes from the db

func (*MongoRecipeDB) Num

func (m *MongoRecipeDB) Num() int64

Num counts the number of recipes in the db

func (*MongoRecipeDB) Picture

func (m *MongoRecipeDB) Picture(id RecipeID, name string) *RecipePicture

Picture returns a specific picture with a specific name for a specific recipe

func (*MongoRecipeDB) Pictures

func (m *MongoRecipeDB) Pictures(id RecipeID) map[string]*RecipePicture

Pictures returns all pictures for a given recipe

func (*MongoRecipeDB) Ping

func (m *MongoRecipeDB) Ping() error

Ping MongoDB

func (*MongoRecipeDB) Random

func (m *MongoRecipeDB) Random() *Recipe

Random picture will be returned

func (*MongoRecipeDB) Remove

func (m *MongoRecipeDB) Remove(id RecipeID) error

Remove removes a recipe by id

func (*MongoRecipeDB) RemoveByName

func (m *MongoRecipeDB) RemoveByName(name string) error

RemoveByName a recipe by name

func (*MongoRecipeDB) StartDB

func (m *MongoRecipeDB) StartDB() error

StartDB initializes the database connection

func (*MongoRecipeDB) StopDB

func (m *MongoRecipeDB) StopDB() (err error)

StopDB closes the connection to the db

func (*MongoRecipeDB) Update

func (m *MongoRecipeDB) Update(id RecipeID, recipe *Recipe) error

Update a recipe with a given recipe id

type Recipe

type Recipe struct {
	ID          RecipeID      `json:"id"`
	Name        string        `json:"name"`
	Ingredients []Ingredients `json:"components"`
	Description string        `json:"description"`
	PictureLink []string      `json:"pictureLink"`
	Servings    int8          `json:"servings"`
}

Recipe model

func NewInvalidRecipe

func NewInvalidRecipe() *Recipe

NewInvalidRecipe returns an empty Recipe object. The ID of the returned Recipe is InvalidRecipeID.

func NewRecipe

func NewRecipe(id RecipeID) *Recipe

NewRecipe creates a new Recipe with a given id

func (*Recipe) JSON

func (r *Recipe) JSON() []byte

JSON returns the encoded version of the recipe. If an error occurs, '{}' is returned.

func (*Recipe) ScaleBy

func (r *Recipe) ScaleBy(factor float64)

ScaleBy a factor (of servings) all ingredients of the recipe

func (*Recipe) ScaleTo

func (r *Recipe) ScaleTo(servings int8)

ScaleTo a desired number of servings

func (*Recipe) String

func (r *Recipe) String() string

String (JSON) representation of the recipe

type RecipeDB

type RecipeDB interface {
	io.Closer
	Recipes
	Ping() error
	Clear()
}

RecipeDB is the interface that all DB implementations have to expose

func NewDatabaseClient

func NewDatabaseClient() (RecipeDB, error)

NewDatabaseClient builds a client to communicate with a database

type RecipeID

type RecipeID string

RecipeID is a data type that provides a unique id for each recipe

func InvalidRecipeID

func InvalidRecipeID() RecipeID

InvalidRecipeID should not be used for any valid Recipe

func NewRecipeID

func NewRecipeID() RecipeID

NewRecipeID returns a random recipe id

func NewRecipeIDFromString

func NewRecipeIDFromString(recipeID string) (result RecipeID)

NewRecipeIDFromString converts a string to a recipe id and returns this recipe id. Returns the InvalidRecipeID iff the recipe id cannot be converted

func (RecipeID) String

func (r RecipeID) String() string

String converts a RecipeID to string

type RecipeList added in v0.4.0

type RecipeList struct {
	Recipes []string `json:"recipes"`
}

RecipeList models a list of recipes by ID

type RecipePicture

type RecipePicture struct {
	ID      RecipeID `json:"id"`
	Name    string   `json:"name"`
	Picture string   `json:"picture"`
}

RecipePicture model

func NewInvalidRecipePicture

func NewInvalidRecipePicture() *RecipePicture

NewInvalidRecipePicture returns an invalid picture

type RecipeSearchFilter

type RecipeSearchFilter struct {
	Name        string   `json:"name"`
	Ingredient  []string `json:"ingredients"`
	Description string   `json:"description"`
}

RecipeSearchFilter models a search query to filter recipes

type Recipes

type Recipes interface {
	List() []*Recipe
	IDs(filterQuery *RecipeSearchFilter) RecipeList
	Num() int64
	Get(id RecipeID) *Recipe
	GetByName(name string) (*Recipe, error)
	Picture(id RecipeID, name string) *RecipePicture
	Pictures(id RecipeID) map[string]*RecipePicture
	Random() *Recipe
	Insert(recipe *Recipe) error
	Update(id RecipeID, recipe *Recipe) error
	AddPicture(pic *RecipePicture) error
	Remove(id RecipeID) error
	RemoveByName(name string) error
}

Recipes interface is an abstraction for the provider of a collection of recipes, i.e., a data-base or a cache

Jump to

Keyboard shortcuts

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