models

package
v2.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2017 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SortRecipeByName represents the value to use in RecipesFilter.SortBy
	// in order to sort by the recipe name
	SortRecipeByName string = "name"
	// SortRecipeByID represents the value to use in RecipesFilter.SortBy
	// in order to sort by the recipe ID
	SortRecipeByID string = "id"
	// SortRecipeByRating represents the value to use in RecipesFilter.SortBy
	// in order to sort by the recipe rating
	SortRecipeByRating string = "rating"

	// SortTagByText represents the value to use in TagsFilter.SortBy
	// in order to sort by the tag value
	SortTagByText string = "tag"
	// SortTagByFrequency represents the value to use in TagsFilter.SortBy
	// in order to sort by the number of recipes using a tag
	SortTagByFrequency string = "frequency"

	// SortByRandom represents the value to use in RecipesFilter.SortBy
	// and TagsFilter.SortBy in order to sort the results randomly
	SortByRandom string = "random"

	// SortDirAsc represents the value to use in RecipesFilter.SortDir
	// and TagsFilter.SortDir in order to sort the results in ascending order.
	SortDirAsc string = "asc"
	// SortDirDesc represents the value to use in RecipesFilter.SortDir
	// and TagsFilter.SortDir in order to sort the results in descending order.
	SortDirDesc string = "desc"
)

Variables

View Source
var ErrNotFound = errors.New("No record found matching supplied criteria")

ErrNotFound represents the error when a database record cannot be found matching the criteria specified by the caller

Functions

This section is empty.

Types

type Model

type Model struct {
	Recipes *RecipeModel
	Tags    *TagModel
	Notes   *NoteModel
	Images  *RecipeImageModel
	Users   *UserModel
	Search  *SearchModel
	// contains filtered or unexported fields
}

Model encapsulates the model layer of the application, including database access

func New

func New(cfg *conf.Config) *Model

New constructs a new Model object

func (*Model) TearDown

func (m *Model) TearDown()

TearDown closes the connection to the database.

type Note

type Note struct {
	ID         int64     `json:"id" db:"id"`
	RecipeID   int64     `json:"recipeId" db:"recipe_id"`
	Note       string    `json:"text" db:"note"`
	CreatedAt  time.Time `json:"createdAt" db:"created_at"`
	ModifiedAt time.Time `json:"modifiedAt" db:"modified_at"`
}

Note represents an individual comment (or note) on a recipe

type NoteModel

type NoteModel struct {
	*Model
}

NoteModel provides functionality to edit and retrieve notes attached to recipes.

func (*NoteModel) Create

func (m *NoteModel) Create(note *Note) error

Create stores the note in the database as a new record using a dedicated transation that is committed if there are not errors.

func (*NoteModel) CreateTx

func (m *NoteModel) CreateTx(note *Note, tx *sqlx.Tx) error

CreateTx stores the note in the database as a new record using the specified transaction.

func (*NoteModel) Delete

func (m *NoteModel) Delete(id int64) error

Delete removes the specified note from the database using a dedicated transation that is committed if there are not errors.

func (*NoteModel) DeleteAll

func (m *NoteModel) DeleteAll(recipeID int64) error

DeleteAll removes all notes for the specified recipe from the database using a dedicated transation that is committed if there are not errors.

func (*NoteModel) DeleteAllTx

func (m *NoteModel) DeleteAllTx(recipeID int64, tx *sqlx.Tx) error

DeleteAllTx removes all notes for the specified recipe from the database using the specified transaction.

func (*NoteModel) DeleteTx

func (m *NoteModel) DeleteTx(id int64, tx *sqlx.Tx) error

DeleteTx removes the specified note from the database using the specified transaction.

func (*NoteModel) List

func (m *NoteModel) List(recipeID int64) (*Notes, error)

List retrieves all notes associated with the recipe with the specified id.

func (*NoteModel) Update

func (m *NoteModel) Update(note *Note) error

Update stores the note in the database by updating the existing record with the specified id using a dedicated transation that is committed if there are not errors.

func (*NoteModel) UpdateTx

func (m *NoteModel) UpdateTx(note *Note, tx *sqlx.Tx) error

UpdateTx stores the note in the database by updating the existing record with the specified id using the specified transaction.

type Notes

type Notes []Note

Notes represents a collection of Note objects

type Recipe

type Recipe struct {
	ID            int64       `json:"id" db:"id"`
	Name          string      `json:"name" db:"name"`
	ServingSize   string      `json:"servingSize" db:"serving_size"`
	NutritionInfo string      `json:"nutritionInfo" db:"nutrition_info"`
	Ingredients   string      `json:"ingredients" db:"ingredients"`
	Directions    string      `json:"directions" db:"directions"`
	SourceURL     string      `json:"sourceUrl" db:"source_url"`
	AvgRating     float64     `json:"averageRating" db:"avg_rating"`
	MainImage     RecipeImage `json:"mainImage"`
	Tags          []string    `json:"tags"`
}

Recipe is the primary model class for recipe storage and retrieval

type RecipeImage

type RecipeImage struct {
	ID           int64     `json:"id" db:"id"`
	RecipeID     int64     `json:"recipeId" db:"recipe_id"`
	Name         string    `json:"name" db:"name"`
	URL          string    `json:"url" db:"url"`
	ThumbnailURL string    `json:"thumbnailUrl" db:"thumbnail_url"`
	CreatedAt    time.Time `json:"createdAt" db:"created_at"`
	ModifiedAt   time.Time `json:"modifiedAt" db:"modified_at"`
}

RecipeImage represents the data associated with an image attached to a recipe

type RecipeImageModel

type RecipeImageModel struct {
	*Model
	// contains filtered or unexported fields
}

RecipeImageModel provides functionality to edit and retrieve images attached to recipes

func NewRecipeImageModel

func NewRecipeImageModel(model *Model) *RecipeImageModel

NewRecipeImageModel constructs a RecipeImageModel

func (*RecipeImageModel) Create

func (m *RecipeImageModel) Create(imageInfo *RecipeImage, imageData []byte) error

Create saves the image using the backing upload.Driver and creates an associated record in the database using a dedicated transation that is committed if there are not errors.

func (*RecipeImageModel) CreateTx

func (m *RecipeImageModel) CreateTx(imageInfo *RecipeImage, imageData []byte, tx *sqlx.Tx) error

CreateTx saves the image using the backing upload.Driver and creates an associated record in the database using the specified transaction.

func (*RecipeImageModel) Delete

func (m *RecipeImageModel) Delete(id int64) error

Delete removes the specified image from the backing store and database using a dedicated transation that is committed if there are not errors.

func (*RecipeImageModel) DeleteAll

func (m *RecipeImageModel) DeleteAll(recipeID int64) error

DeleteAll removes all images for the specified recipe from the database using a dedicated transation that is committed if there are not errors.

func (*RecipeImageModel) DeleteAllTx

func (m *RecipeImageModel) DeleteAllTx(recipeID int64, tx *sqlx.Tx) error

DeleteAllTx removes all images for the specified recipe from the database using the specified transaction.

func (*RecipeImageModel) DeleteTx

func (m *RecipeImageModel) DeleteTx(id int64, tx *sqlx.Tx) error

DeleteTx removes the specified image from the backing store and database using the specified transaction.

func (*RecipeImageModel) List

func (m *RecipeImageModel) List(recipeID int64) (*RecipeImages, error)

List returns a RecipeImages slice that contains data for all images attached to the specified recipe.

func (*RecipeImageModel) ReadMainImage

func (m *RecipeImageModel) ReadMainImage(recipeID int64) (*RecipeImage, error)

ReadMainImage retrieves the information about the main image for the specified recipe image from the database. If no main image exists, a ErrNotFound error is returned.

func (*RecipeImageModel) ReadTx

func (m *RecipeImageModel) ReadTx(id int64, tx *sqlx.Tx) (*RecipeImage, error)

ReadTx retrieves the information about the image from the database, if found, using the specified transaction. If no image exists with the specified ID, a ErrNotFound error is returned.

func (*RecipeImageModel) UpdateMainImage

func (m *RecipeImageModel) UpdateMainImage(image *RecipeImage) error

UpdateMainImage sets the id of the main image for the specified recipe using a dedicated transation that is committed if there are not errors.

func (*RecipeImageModel) UpdateMainImageTx

func (m *RecipeImageModel) UpdateMainImageTx(image *RecipeImage, tx *sqlx.Tx) error

UpdateMainImageTx sets the id of the main image for the specified recipe using the specified transaction.

type RecipeImages

type RecipeImages []RecipeImage

RecipeImages represents a collection of RecipeImage objects

type RecipeModel

type RecipeModel struct {
	*Model
}

RecipeModel provides functionality to edit and retrieve recipes.

func (*RecipeModel) Create

func (m *RecipeModel) Create(recipe *Recipe) error

Create stores the recipe in the database as a new record using a dedicated transation that is committed if there are not errors.

func (*RecipeModel) CreateTx

func (m *RecipeModel) CreateTx(recipe *Recipe, tx *sqlx.Tx) error

CreateTx stores the recipe in the database as a new record using the specified transaction.

func (*RecipeModel) Delete

func (m *RecipeModel) Delete(id int64) error

Delete removes the specified recipe from the database using a dedicated transation that is committed if there are not errors. Note that this method does not delete any attachments that we associated with the deleted recipe.

func (*RecipeModel) DeleteTx

func (m *RecipeModel) DeleteTx(id int64, tx *sqlx.Tx) error

DeleteTx removes the specified recipe from the database using the specified transaction. Note that this method does not delete any attachments that we associated with the deleted recipe.

func (*RecipeModel) Read

func (m *RecipeModel) Read(id int64) (*Recipe, error)

Read retrieves the information about the recipe from the database, if found. If no recipe exists with the specified ID, a NoRecordFound error is returned.

func (*RecipeModel) SetRating

func (m *RecipeModel) SetRating(id int64, rating float64) error

SetRating adds or updates the rating of the specified recipe.

func (*RecipeModel) Update

func (m *RecipeModel) Update(recipe *Recipe) error

Update stores the specified recipe in the database by updating the existing record with the specified id using a dedicated transation that is committed if there are not errors.

func (*RecipeModel) UpdateTx

func (m *RecipeModel) UpdateTx(recipe *Recipe, tx *sqlx.Tx) error

UpdateTx stores the specified recipe in the database by updating the existing record with the sepcified id using the specified transaction.

type Recipes

type Recipes []Recipe

Recipes represents a collection of Recipe objects

type RecipesFilter

type RecipesFilter struct {
	Query   string   `json:"query"`
	Tags    []string `json:"tags"`
	SortBy  string   `json:"sortBy"`
	SortDir string   `json:"sortDir"`
	Page    int64    `json:"page"`
	Count   int64    `json:"count"`
}

RecipesFilter is the primary model class for recipe search

type SearchModel

type SearchModel struct {
	*Model
}

SearchModel provides functionality to search recipes.

func (*SearchModel) FindRecipes

func (m *SearchModel) FindRecipes(filter RecipesFilter) (*Recipes, int64, error)

FindRecipes retrieves all recipes matching the specified search filter and within the range specified.

func (*SearchModel) FindTags

func (m *SearchModel) FindTags(filter TagsFilter) (*[]string, error)

FindTags retrieves all tags matching the specified search filter and within the range specified.

type TagModel

type TagModel struct {
	*Model
}

TagModel provides functionality to edit and retrieve tags attached to recipes.

func (*TagModel) Create

func (m *TagModel) Create(recipeID int64, tag string) error

Create stores the tag in the database as a new record using a dedicated transation that is committed if there are not errors.

func (*TagModel) CreateTx

func (m *TagModel) CreateTx(recipeID int64, tag string, tx *sqlx.Tx) error

CreateTx stores the tag in the database as a new record using the specified transaction.

func (*TagModel) DeleteAll

func (m *TagModel) DeleteAll(recipeID int64) error

DeleteAll removes all tags for the specified recipe from the database using a dedicated transation that is committed if there are not errors.

func (*TagModel) DeleteAllTx

func (m *TagModel) DeleteAllTx(recipeID int64, tx *sqlx.Tx) error

DeleteAllTx removes all tags for the specified recipe from the database using the specified transaction.

func (*TagModel) List

func (m *TagModel) List(recipeID int64) (*[]string, error)

List retrieves all tags associated with the recipe with the specified id.

type TagsFilter

type TagsFilter struct {
	SortBy  string `json:"sortBy"`
	SortDir string `json:"sortDir"`
	Count   int64  `json:"count"`
}

TagsFilter is the primary model class for tag search

type User

type User struct {
	ID           int64  `db:"id"`
	Username     string `db:"username"`
	PasswordHash string `db:"password_hash"`
}

User represents an individual user

type UserModel

type UserModel struct {
	*Model
}

UserModel provides functionality to edit and authenticate users.

func (*UserModel) Authenticate

func (m *UserModel) Authenticate(username, password string) (*User, error)

Authenticate verifies the username and password combination match an existing user

func (*UserModel) Read

func (m *UserModel) Read(id int64) (*User, error)

Jump to

Keyboard shortcuts

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