models

package
v0.0.0-...-5aaad3a Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2015 License: BSD-2-Clause Imports: 8 Imported by: 0

Documentation

Overview

This package contains the blog's models and holds the database connection globally but as an unexported value.

Index

Constants

View Source
const (
	SQL_POST_BY_ID = `` /* 197-byte string literal not displayed */

	SQL_POST_BY_SLUG = `` /* 195-byte string literal not displayed */

	SQL_POSTS_BY_USER = `
		SELECT idPost, title, slug, date, draft
		FROM posts
		WHERE idUser=?
		ORDER BY draft DESC, date DESC`

	SQL_POSTS_BY_TAG = `` /* 200-byte string literal not displayed */

	SQL_ALL_POSTS = `` /* 152-byte string literal not displayed */

	SQL_ALL_TAGS = `SELECT DISTINCT tag FROM post_tags INNER JOIN posts USING(idPost) WHERE posts.draft=false`

	SQL_INSERT_POST = `
		INSERT INTO posts (slug, title, abstract, body, idUser, draft)
		VALUES (?, ?, ?, ?, ?, ?)`

	SQL_INSERT_TAGS = `
		INSERT IGNORE INTO post_tags (idPost, tag)
		VALUES (?, ?)`

	SQL_REMOVE_TAGS = `DELETE from post_tags WHERE idPost=?`

	SQL_DELETE_POST = `DELETE from posts WHERE idPost=?`

	SQL_UPDATE_POST = `
		UPDATE posts SET slug=?, title=?, abstract=?, body=?, idUser=?, draft=?
		WHERE idPost=?`

	SQL_USER_BY_ID = `
		SELECT name, email 
		FROM users 
		WHERE idUser=?`

	SQL_USER_AUTH = `
		SELECT name, idUser
		FROM users 
		WHERE email=? AND password=?`
)

Variables

This section is empty.

Functions

func CloseDb

func CloseDb()

Closes database connection

func ConnectDb

func ConnectDb(address string)

Creates and tests database connection

func TagsAll

func TagsAll() ([]string, error)

Returns a list of unique tags

Types

type Post

type Post struct {
	Id          int
	Slug        string
	Title, Body string
	Abstract    string
	Author      User
	Date        time.Time
	Draft       bool
	Tags        []string
}

func Posts

func Posts(limit int) (posts []Post, err error)

Fetches number of posts from the database ordered by date

func PostsByTag

func PostsByTag(tag string) (posts []Post, err error)

Retrieves all posts that match a given tag and are not drafts, ordered by date

func PostsByUser

func PostsByUser(u *User) (posts []Post, err error)

Fetches all posts by user's ID

func (*Post) BodyHTML

func (p *Post) BodyHTML() template.HTML

func (*Post) Delete

func (p *Post) Delete() error

Deletes the post

func (*Post) Fetch

func (p *Post) Fetch() error

Fetches one post from the database based on ID or slug

func (*Post) FormattedDate

func (p *Post) FormattedDate() string

func (*Post) Save

func (p *Post) Save() error

Saves a post to the database. If it has a set ID it will try to update an already existing post, otherwise it will insert a new post and generate an ID for it. An unset ID is an ID of value 0.

func (*Post) TagsString

func (p *Post) TagsString() string

type User

type User struct {
	Id          int
	Name, Email string
	Password    string
}

Contains information about a user

func (*User) Fetch

func (u *User) Fetch() error

Fetches a user by ID and updates the structure

func (*User) LoginCorrect

func (u *User) LoginCorrect() bool

Verifies if a password & e-mail combination is correct for the user and fetches rest of data

Directories

Path Synopsis
Package testdb assists with creating and managing a database destined for testing.
Package testdb assists with creating and managing a database destined for testing.

Jump to

Keyboard shortcuts

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