woodpecker/pkg/store/builtin/star.go

48 lines
1.1 KiB
Go
Raw Normal View History

package builtin
import (
2015-06-14 00:49:21 +00:00
"database/sql"
"github.com/drone/drone/pkg/types"
)
type Starstore struct {
2015-06-14 00:49:21 +00:00
*sql.DB
}
2015-06-14 00:49:21 +00:00
func NewStarstore(db *sql.DB) *Starstore {
return &Starstore{db}
}
// Starred returns true if the user starred
// the given repository.
2015-06-14 00:49:21 +00:00
func (db *Starstore) Starred(user *types.User, repo *types.Repo) (bool, error) {
_, err := getStar(db, rebind(stmtStarSelectStarUserRepo), user.ID, repo.ID)
return (err == nil), err
}
// AddStar inserts a starred repo / user in the datastore.
2015-06-14 00:49:21 +00:00
func (db *Starstore) AddStar(user *types.User, repo *types.Repo) error {
var star = &Star{UserID: user.ID, RepoID: repo.ID}
return createStar(db, rebind(stmtStarInsert), star)
}
// DelStar removes starred repo / user from the datastore.
2015-06-14 00:49:21 +00:00
func (db *Starstore) DelStar(user *types.User, repo *types.Repo) error {
var _, err = db.Exec(rebind(stmtStartDeleteUserRepo), user.ID, repo.ID)
return err
}
2015-06-14 00:49:21 +00:00
type Star struct {
ID int64
UserID int64 `sql:"unique:ux_star_user_repo"`
RepoID int64 `sql:"unique:ux_star_user_repo"`
}
// SQL statement to delete a star by ID.
2015-06-14 00:49:21 +00:00
const stmtStartDeleteUserRepo = `
DELETE FROM stars
2015-06-14 00:49:21 +00:00
WHERE star_user_id=?
AND star_repo_id=?
`