models

package
v0.0.0-...-78466f4 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 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 {
	*sqlx.DB
}

DB holds the database access method (allows us to mock the database)

func CreateDB

func CreateDB(database string, connectionOptions string) (*DB, error)

CreateDB initializes the database connection and returns a new DB struct pointer databaseName is expected to be a PostgreSQL URL

func (*DB) AllImages

func (db *DB) AllImages() (*[]Image, error)

AllImages returns all images in the database

func (*DB) DeleteImage

func (db *DB) DeleteImage(id uuid.UUID) (*Image, error)

DeleteImage takes an id of an image and if exists deletes from database returns error if not found

func (*DB) DeletePost

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

DeletePost deletes and returns the image from the database that matches the uuid

func (*DB) FindImage

func (db *DB) FindImage(id uuid.UUID) (*Image, error)

FindImage returns an image from the database for the given uuid

func (*DB) FindImagesByUser

func (db *DB) FindImagesByUser(user uuid.UUID) (*[]Image, error)

FindImagesByUser returns an slice of images from the database for a given user

func (*DB) FindPost

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

FindPost returns the post that matches the uuid

func (*DB) FindPostsByUser

func (db *DB) FindPostsByUser(user uuid.UUID) (*[]Post, error)

FindPostsByUser returns a slice of posts created by the given user

func (*DB) GetPosts

func (db *DB) GetPosts(start int, end int) (*[]Post, error)

GetPosts returns all posts in database

func (*DB) GetUserByEmail

func (db *DB) GetUserByEmail(email string) (*User, error)

GetUserByEmail ...

func (*DB) GetUserByID

func (db *DB) GetUserByID(user uuid.UUID) (*User, error)

GetUserByID ...

func (*DB) GetUserByUname

func (db *DB) GetUserByUname(uname string) (*User, error)

GetUserByUname ...

func (*DB) InsertImage

func (db *DB) InsertImage(user uuid.UUID, url string, medium string, small string, caption string) (*Image, error)

InsertImage attempts to insert an image into the database provided three sizes of the image (original, medium, and small) return an error if it cannot be added or user reference invalid

func (*DB) InsertPost

func (db *DB) InsertPost(user uuid.UUID, title string, slug string, subtitle string, short string, content string, digest string, published bool) (*Post, error)

InsertPost creates a post for the given user and returns the post

func (*DB) InsertUser

func (db *DB) InsertUser(uname string, digest []byte, role string, email string, gpg string) (*User, error)

InsertUser ...

func (*DB) PublishedPosts

func (db *DB) PublishedPosts(start int, end int) (*[]Post, error)

PublishedPosts returns all published posts in database

func (*DB) UnpublishedPosts

func (db *DB) UnpublishedPosts() (*[]Post, error)

UnpublishedPosts returns all unpublished posts in database

func (*DB) UpdatePost

func (db *DB) UpdatePost(id uuid.UUID, user uuid.UUID, title string, slug string, subtitle string, short string, content string, digest string, published bool) (*Post, error)

UpdatePost updates a post in the database and returns the updated image

type Datastore

type Datastore interface {
	// User Functions
	InsertUser(uname string, digest []byte, role string, email string, gpg string) (*User, error)
	GetUserByEmail(email string) (*User, error)
	GetUserByID(user uuid.UUID) (*User, error)
	GetUserByUname(uname string) (*User, error)
	// Post Functions
	PublishedPosts(start int, end int) (*[]Post, error)
	UnpublishedPosts() (*[]Post, error)
	GetPosts(start int, end int) (*[]Post, error)
	FindPost(id uuid.UUID) (*Post, error)
	FindPostsByUser(user uuid.UUID) (*[]Post, error)
	InsertPost(user uuid.UUID, title string, slug string, subtitle string, short string, content string, digest string, published bool) (*Post, error)
	UpdatePost(id uuid.UUID, user uuid.UUID, title string, slug string, subtitle string, short string, content string, digest string, published bool) (*Post, error)
	DeletePost(id uuid.UUID, user uuid.UUID) (*Post, error)
	// Image Functions
	AllImages() (*[]Image, error)
	FindImage(id uuid.UUID) (*Image, error)
	FindImagesByUser(user uuid.UUID) (*[]Image, error)
	InsertImage(user uuid.UUID, url string, medium string, small string, caption string) (*Image, error)
	DeleteImage(id uuid.UUID) (*Image, error)
}

Datastore interface contains all of our functions for the PostgreSQL database this allows us to mock the database during tests!

type Image

type Image struct {
	ID        uuid.UUID `db:"id" json:"id"`
	UserID    uuid.UUID `db:"user_id" json:"user_id"`
	URL       string    `db:"url" json:"url"`
	Medium    string    `db:"medium" json:"medium"`
	Small     string    `db:"small" json:"small"`
	Caption   string    `db:"caption" json:"caption"`
	UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
	CreatedAt time.Time `db:"created_at" json:"created_at"`
}

Image struct based on image table in database

type Post

type Post struct {
	ID          uuid.UUID `db:"id" json:"id"`
	UserID      uuid.UUID `db:"user_id" json:"user_id"`
	Title       string    `db:"title" json:"title"`
	Slug        string    `db:"slug" json:"slug"`
	SubTitle    string    `db:"sub_title" json:"sub_title"`
	Short       string    `db:"short" json:"short"`
	PostContent string    `db:"post_content" json:"post_content"`
	Digest      string    `db:"digest" json:"digest"`
	Published   bool      `db:"published" json:"published"`
	UpdatedAt   time.Time `db:"updated_at" json:"updated_at"`
	CreatedAt   time.Time `db:"created_at" json:"created_at"`
}

Post struct based on posts table in database

type Tag

type Tag struct {
	ID   uuid.UUID `db:"id" json:"id"`
	Name string    `db:"name" json:"name"`
	Slug string    `db:"slug" json:"slug"`
}

Tag struct based on tag table in database

type User

type User struct {
	ID           uuid.UUID `db:"id"`
	Uname        string    `db:"uname"`
	Digest       []byte    `db:"digest"`
	Role         string    `db:"role"`
	Email        string    `db:"email"`
	GpgKey       string    `db:"email"`
	LastOnlineAt time.Time `db:"updated_at"`
	CreatedAt    time.Time `db:"created_at"`
}

User struct based on users table in database

Jump to

Keyboard shortcuts

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