db

package
v0.0.0-...-870a995 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2018 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPostID = "INVALID_POST_ID"
	ErrAlreadyLiked  = "ALREADY_LIKED"
)
View Source
var (
	// ErrInvalidData is sent when a value in request is invalid
	ErrInvalidData = "INVALID_DATA"

	// ErrNotRegistered is sent when a deviceid is not registered
	ErrNotRegistered = "NOT_REGISTERED"
)

Functions

This section is empty.

Types

type Comment

type Comment struct {
	ID        int    `db:"commentid" json:"commentid"`
	Text      string `db:"comment" json:"comment"`
	Timestamp int64  `db:"timestamp" json:"timestamp"`
	DeviceID  string `db:"deviceid" json:"-"`
}

type DB

type DB struct {
	Pq    *sql.DB
	Redis *redis.Client
}

func (*DB) FetchNPosts

func (d *DB) FetchNPosts(ctx context.Context, n int) ([]*Post, error)

FetchNPosts takes an integer and returns the most recent N posts

func (*DB) FetchPost

func (d *DB) FetchPost(ctx context.Context, postid string) (*Post, error)

func (*DB) FetchPostsFromID

func (d *DB) FetchPostsFromID(ctx context.Context, id, limit int, prop string) ([]*Post, error)

FetchPostsFromID fetches a number of posts before or after the specified id

func (*DB) LikePost

func (d *DB) LikePost(ctx context.Context, postid string, deviceid string) error

LikePost adds a new entry in likes table containing details like deviceid and postid. When complete details of a post are requested, We'll have to count all the entries containing specified postid.

func (*DB) RegisterDeviceID

func (d *DB) RegisterDeviceID(ctx context.Context, deviceid string, hash string, t time.Duration) error

RegisterDeviceID takes a device id and a hash and saves it in database

func (*DB) Report

func (d *DB) Report(ctx context.Context, postid, deviceid, reason string) error

Report puts information like postid and device id in reports table

func (*DB) SubmitPost

func (d *DB) SubmitPost(ctx context.Context, p *Post) error

SubmitPost takes a Post, puts it into the database and returns the postid

func (*DB) VerifyDeviceID

func (d *DB) VerifyDeviceID(ctx context.Context, deviceid string) (string, error)

VerifyDeviceID takes a Device ID and checks if it registered via checking it's existence in Redis.

type IDB

type IDB interface {
	FetchNPosts(ctx context.Context, n int) ([]*Post, error)
	FetchPostsFromID(ctx context.Context, id, limit int, prop string) ([]*Post, error)
	LikePost(ctx context.Context, postid, deviceid string) error
	Report(ctx context.Context, postid, deviceid, reason string) error
	SubmitPost(ctx context.Context, p *Post) error

	// Authentication related endpoints
	VerifyDeviceID(ctx context.Context, deviceid string) (string, error)
	RegisterDeviceID(ctx context.Context, deviceid, hash string, t time.Duration) error
}

IDB interface defines all the database operations used by the application.

func Init

func Init() (IDB, error)

type Like

type Like struct {
	DeviceID string `db:"deviceid" json:"-"`
}

type Post

type Post struct {
	ID        int    `db:"postid" json:"postid"`
	Text      string `db:"post" json:"post"`
	Timestamp int64  `db:"timestamp" json:"timestamp"`
	DeviceID  string `db:"deviceid" json:"-"`
	IPAddr    string `db:"ipAddr" json:"-"`
	PostMeta
}

type PostMeta

type PostMeta struct {
	Likeable      bool       `json:"likeable"`
	Editable      bool       `json:"editable"`
	CommentsCount int        `db:"comments" json:"comments_count"`
	LikesCount    int        `db:"likes" json:"likes_count"`
	Comments      []*Comment `json:"comments"`
}

Jump to

Keyboard shortcuts

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