models

package
v0.0.0-...-1402125 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2019 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	*sql.DB
}

DB is our database type By attaching the Datastore interface's methods, our DB struct will implement the Datastore interface.

func NewDB

func NewDB(dataSourceName string) (*DB, error)

NewDB creates a new DB instance

func (*DB) AllPosts

func (db *DB) AllPosts(prevDate string, limit int) ([]*Post, error)

AllPosts takes a previous date and limit and returns all posts in reverse chronological order or an error.

func (*DB) CreatePost

func (db *DB) CreatePost(Post *Post) error

CreatePost creates a new post in the DB and returns an error. CreatePost expects Post will come in with id uuid.UUID, title string, body string, created time.Time, uid uuid.UUID

func (*DB) CreateUser

func (db *DB) CreateUser(user *User) error

CreateUser creates a new user and returns nil or an error CreateUser expects user will come in with name string, email string, pwd []byte

func (*DB) DeletePost

func (db *DB) DeletePost(Post *Post) error

DeletePost deletes one specific Post from DB and returns an error. DeletePost expects Post will come in with id uuid.UUID

func (*DB) DeleteUser

func (db *DB) DeleteUser(user *User) error

DeleteUser deletes one specific user from DB, along with associated posts, and returns nil or an error. DeleteUser expects user will come in with id uuid.UUID

func (*DB) EmailCheck

func (db *DB) EmailCheck(email string) (bool, error)

EmailCheck checks if an email is already in use when a new user signs up.

func (*DB) NameCheck

func (db *DB) NameCheck(name string) (bool, error)

NameCheck checks if a name is already in use when a new user signs up

func (*DB) OnePost

func (db *DB) OnePost(id uuid.UUID) (*Post, error)

OnePost returns one specific post or an error

func (*DB) SearchUsers

func (db *DB) SearchUsers(query, prevDate string, limit int) ([]*User, error)

SearchUsers takes a search query and limit and returns all posts in reverse chronological order or an error.

func (*DB) UpdatePost

func (db *DB) UpdatePost(Post *Post) error

UpdatePost updates a specific Post in DB and returns an error. UpdatePost expects Post will come in with id uuid.UUID, title string, body string, updated time.Time

func (*DB) UpdateUserPhoto

func (db *DB) UpdateUserPhoto(user *User) error

UpdateUserPhoto updates a user's profile photo and returns nil or an error. UpdateUserPhoto expects user will come in with avatar string, updated time.Time

type Datastore

type Datastore interface {

	//Sample User methods
	SearchUsers(query, prevDate string, limit int) ([]*User, error)
	CreateUser(user *User) error
	EmailCheck(email string) (bool, error)
	NameCheck(name string) (bool, error)
	UpdateUserPhoto(user *User) error
	DeleteUser(user *User) error

	//Sample Post methods
	AllPosts(prevDate string, limit int) ([]*Post, error)
	OnePost(id uuid.UUID) (*Post, error)
	CreatePost(Post *Post) error
	UpdatePost(Post *Post) error
	DeletePost(Post *Post) error
}

Datastore is an interface to work with the Postgres database. The Server struct in API/app/server.go includes this Datastore interface for handlers to access via dependency injection. Using an interface allows us to easily create mock databases for testing purposes.

type Post

type Post struct {
	ID      uuid.UUID `json:"id"`
	Title   string    `json:"title"`
	Body    string    `json:"body"`
	Created time.Time `json:"created"`
	Updated time.Time `json:"updated"`
	Author  User      `json:"author"`
}

Post type defined

type User

type User struct {
	ID       uuid.UUID `json:"id"`
	Name     string    `json:"name"`
	Email    string    `json:"email,omitempty"`
	Password string    `json:"password,omitempty"`
	Avatar   string    `json:"avatar"`
	Created  time.Time `json:"created,omitempty"`
	Updated  time.Time `json:"updated,omitempty"`
}

User type defined

Jump to

Keyboard shortcuts

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