woodpecker/server/database/user.go

113 lines
2.5 KiB
Go
Raw Normal View History

package database
2014-06-04 21:25:38 +00:00
import (
"database/sql"
"time"
"github.com/drone/drone/shared/model"
2014-06-04 21:25:38 +00:00
"github.com/russross/meddler"
)
type UserManager interface {
// Find finds the User by ID.
Find(id int64) (*model.User, error)
2014-06-04 21:25:38 +00:00
// FindLogin finds the User by remote login.
FindLogin(remote, login string) (*model.User, error)
2014-06-04 21:25:38 +00:00
// FindToken finds the User by token.
FindToken(token string) (*model.User, error)
2014-06-04 21:25:38 +00:00
// List finds all registered users of the system.
List() ([]*model.User, error)
2014-06-04 21:25:38 +00:00
// Insert persists the User to the datastore.
Insert(user *model.User) error
2014-06-04 21:25:38 +00:00
// Update persists changes to the User to the datastore.
Update(user *model.User) error
2014-06-04 21:25:38 +00:00
// Delete removes the User from the datastore.
Delete(user *model.User) error
2014-06-04 21:25:38 +00:00
}
// userManager manages a list of users in a SQL database.
type userManager struct {
*sql.DB
}
// SQL query to retrieve a User by remote login.
const findUserLoginQuery = `
2014-06-04 21:25:38 +00:00
SELECT *
FROM users
WHERE user_remote=?
AND user_login=?
LIMIT 1
`
// SQL query to retrieve a User by remote login.
const findUserTokenQuery = `
2014-06-04 21:25:38 +00:00
SELECT *
FROM users
WHERE user_token=?
LIMIT 1
`
// SQL query to retrieve a list of all users.
const listUserQuery = `
2014-06-04 21:25:38 +00:00
SELECT *
FROM users
ORDER BY user_name ASC
`
// SQL statement to delete a User by ID.
const deleteUserStmt = `
2014-06-04 21:25:38 +00:00
DELETE FROM users WHERE user_id=?
`
// NewUserManager initiales a new UserManager intended to
2014-06-04 21:25:38 +00:00
// manage and persist commits.
func NewUserManager(db *sql.DB) UserManager {
2014-06-04 21:25:38 +00:00
return &userManager{db}
}
func (db *userManager) Find(id int64) (*model.User, error) {
dst := model.User{}
2014-06-04 21:25:38 +00:00
err := meddler.Load(db, "users", &dst, id)
return &dst, err
}
func (db *userManager) FindLogin(remote, login string) (*model.User, error) {
dst := model.User{}
err := meddler.QueryRow(db, &dst, findUserLoginQuery, remote, login)
2014-06-04 21:25:38 +00:00
return &dst, err
}
func (db *userManager) FindToken(token string) (*model.User, error) {
dst := model.User{}
err := meddler.QueryRow(db, &dst, findUserTokenQuery, token)
2014-06-04 21:25:38 +00:00
return &dst, err
}
func (db *userManager) List() ([]*model.User, error) {
var dst []*model.User
err := meddler.QueryAll(db, &dst, listUserQuery)
2014-06-04 21:25:38 +00:00
return dst, err
}
func (db *userManager) Insert(user *model.User) error {
2014-06-04 21:25:38 +00:00
user.Created = time.Now().Unix()
user.Updated = time.Now().Unix()
return meddler.Insert(db, "users", user)
}
func (db *userManager) Update(user *model.User) error {
2014-06-04 21:25:38 +00:00
user.Updated = time.Now().Unix()
return meddler.Update(db, "users", user)
}
func (db *userManager) Delete(user *model.User) error {
_, err := db.Exec(deleteUserStmt, user.ID)
2014-06-04 21:25:38 +00:00
return err
}