favdb

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: MIT Imports: 12 Imported by: 1

README

favdb: User Favorite DB handle in go (memory, github issue, postgesDB...)

GitHub license GoDocGo

fav is a package to help you setup a single entry fo MemoryDB, Github Issue, PostgresSQL DB with user favorite.

The database must be simple and ease to use.

User1 -> Fav1, Fav2 ...
User2 -> Fav3

Install

go get github.com/kkdai/favdb

Usage

refer example/github-issuefor more detail. GITHUB_URL is a string combine with "USE/REPO/GITHU_TOKEN".

var DB favdb.UserFavData

func main() {
    // PostgresSQL connect string
    url := os.Getenv("DATABASE_URL")
    // `GITHUB_URL` is a string combine with "USE/REPO/GITHU_TOKEN".
    gitUrl := os.Getenv("GITHUB_URL")
    if url != "" {
        // Use PostgresSQL as DB.
        DB = favdb.NewPGSql(url)
    } else if gitUrl != "" {
        // Use Github Issue as DB.
        DB = favdb.NewGithubDB(gitUrl)
    } else {
        //Use memory as DB
        DB = favdb.NewMemDB()
    }

    addBookmarkArticle("title1", "Fav1")
    addBookmarkArticle("title1", "Fav2")
}

func addBookmarkArticle(user, fav string) {
    newFavoriteArticle := fav
    newUser := favdb.UserFavorite{
        UserId:    user,
        Favorites: []string{newFavoriteArticle},
    }
    if record, err := DB.Get(user); err != nil {
        //User data is not created, create a new one
        DB.Add(newUser)
    } else {
        //Record found, update it
        oldRecords := record.Favorites
        if exist, idx := favdb.InArray(newFavoriteArticle, oldRecords); exist == true {
            oldRecords = favdb.RemoveStringItem(oldRecords, idx)
        } else {
            oldRecords = append(oldRecords, newFavoriteArticle)
        }
        record.Favorites = oldRecords
        DB.Update(record)
    }
}

If you want to run it directly, just run

Github Issue
    go install github.com/kkdai/photomgr/example/github_issue

Contribute

Please open up an issue on GitHub before you put a lot efforts on pull request. The code submitting to PR must be filtered with gofmt

License

This package is licensed under MIT license. See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InArray added in v0.2.0

func InArray(val interface{}, array interface{}) (exists bool, index int)

InArray: Check if string item is in array

func NewDB

func NewDB() *firestore.Client

func RemoveStringItem added in v0.2.0

func RemoveStringItem(slice []string, s int) []string

RemoveStringItem: Remove string item from slice

func String

func String(v string) *string

func TruncateString added in v0.2.0

func TruncateString(s string, maxLength int) string

Types

type AllArticles

type AllArticles []ArticleDocument

ArticleDocument for sorting.

func (AllArticles) Len

func (a AllArticles) Len() int

func (AllArticles) Less

func (a AllArticles) Less(i, j int) bool

func (AllArticles) Swap

func (a AllArticles) Swap(i, j int)

type ArticleDocument

type ArticleDocument struct {
	ArticleID    string        `json:"article_id"`
	ArticleTitle string        `json:"article_title"`
	Author       string        `json:"author"`
	Board        string        `json:"board"`
	Content      string        `json:"content"`
	Date         string        `json:"date"`
	IP           string        `json:"ip"`
	MessageCount MessageCount  `bson:"message_count"`
	Messages     []interface{} `json:"messages"`
	Timestamp    int           `json:"timestamp"`
	URL          string        `json:"url"`
	ImageLinks   []string      `json:"image_links"`
}

func (*ArticleDocument) ToString

func (d *ArticleDocument) ToString() (info string)

type GithubDB

type GithubDB struct {
	Name   string
	Repo   string
	Token  string
	Client *github.Client
}

func NewGithubDB

func NewGithubDB(dbStr string) *GithubDB

func (*GithubDB) Add

func (u *GithubDB) Add(user UserFavorite)

func (*GithubDB) Get

func (u *GithubDB) Get(uid string) (result *UserFavorite, err error)

func (*GithubDB) ShowAll

func (u *GithubDB) ShowAll() (result []UserFavorite, err error)

ShowAll: Print all result.

func (*GithubDB) Update

func (u *GithubDB) Update(user *UserFavorite) (err error)

type MemDB

type MemDB struct {
	Db MemStorage
}

func NewMemDB

func NewMemDB() *MemDB

func (*MemDB) Add

func (u *MemDB) Add(user UserFavorite)

func (*MemDB) Get

func (u *MemDB) Get(uid string) (result *UserFavorite, err error)

func (*MemDB) ShowAll

func (u *MemDB) ShowAll() (result []UserFavorite, err error)

ShowAll: Print all result.

func (*MemDB) Update

func (u *MemDB) Update(user *UserFavorite) (err error)

type MemStorage

type MemStorage map[string]UserFavorite

type MessageCount

type MessageCount struct {
	All     int `json:"all"`
	Boo     int `json:"boo"`
	Count   int `json:"count"`
	Neutral int `json:"neutral"`
	Push    int `json:"push"`
}

type Model

type Model struct {
	Db  UserFavData
	Log *log.Logger
}

type PGSql

type PGSql struct {
	Db *pg.DB
	// contains filtered or unexported fields
}

func NewPGSql

func NewPGSql(url string) *PGSql

func (*PGSql) Add

func (u *PGSql) Add(user UserFavorite)

func (*PGSql) Get

func (u *PGSql) Get(uid string) (result *UserFavorite, err error)

func (*PGSql) ShowAll

func (u *PGSql) ShowAll() (result []UserFavorite, err error)

ShowAll: Print all result.

func (*PGSql) Update

func (u *PGSql) Update(user *UserFavorite) (err error)

type UserFavData

type UserFavData interface {
	Add(user UserFavorite)
	Get(uid string) (result *UserFavorite, err error)
	ShowAll() (result []UserFavorite, err error)
	Update(user *UserFavorite) (err error)
}

type UserFavorite

type UserFavorite struct {
	Id        int64    `bson:"_id"`
	UserId    string   `json:"user_id" bson:"user_id"`
	Favorites []string `json:"favorites" bson:"favorites"`
}

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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