storage

package
v0.0.0-...-fd11f28 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package storage ...

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidTodoID = errors.New("invalid todo ID")

ErrInvalidTodoID is returned when the ID sent for fetching/updating/etc a Todo is not a valid Todo in our datastore.

Functions

This section is empty.

Types

type Db

type Db struct {
	*sql.DB
}

Db provides a set of methods for interacting with our database.

func NewDB

func NewDB() (*Db, error)

NewDB creates a connection with our postgres database and returns it, otherwise an error.

func (*Db) CreateTodo

func (d *Db) CreateTodo(content string) (int, error)

CreateTodo handles inserting a new Todo in the database with the caller's provided content.

func (*Db) DeleteTodo

func (d *Db) DeleteTodo(id string) error

DeleteTodo deletes a Todo by its ID.

func (*Db) GetTodo

func (d *Db) GetTodo(id string) (Todo, error)

GetTodo fetches a Todo by the caller's provided ID.

func (*Db) ListTodos

func (d *Db) ListTodos() ([]Todo, error)

ListTodos fetches all the available Todos.

func (*Db) UpdateTodo

func (d *Db) UpdateTodo(id, content, completedAt string) error

UpdateTodo finds a Todo by it's ID and updates its contents. Postgres trigger functions handle assigning a new timestamp to updated_at.

type Todo

type Todo struct {
	ID          int    `json:"id,omitempty"`
	Content     string `json:"content,omitempty" gator:"nonzero"`
	CreatedAt   string `json:"created_at,omitempty"`
	UpdatedAt   string `json:"updated_at,omitempty"`
	CompletedAt string `json:"completed_at,omitempty"`
}

Todo represents a Todo in in our datastore.

type TodoRepository

type TodoRepository interface {
	GetTodo(id string) (Todo, error)
	CreateTodo(content string) (int, error)
	UpdateTodo(id, content, completedAt string) error
	ListTodos() ([]Todo, error)
	DeleteTodo(id string) error
}

TodoRepository describes the interface for interacting with our datastore. This can viewed like a plug in adapter, making testing and/or switching datastores much more trivial.

Jump to

Keyboard shortcuts

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