1
0
Fork 0
empact/db/user.go

39 lines
857 B
Go
Raw Normal View History

2015-03-05 15:04:44 +00:00
package db
2015-03-20 14:19:18 +00:00
import (
"time"
)
2015-03-06 13:35:13 +00:00
2015-03-06 10:00:04 +00:00
type User struct {
2015-03-20 15:22:49 +00:00
ID int `json:"id"`
2015-03-20 14:19:18 +00:00
Login string `json:"login"`
Name string `json:"name"`
AvatarURL string `json:"avatar_url" db:"avatar_url"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
2015-03-06 10:00:04 +00:00
}
2015-03-05 15:04:44 +00:00
func (u *User) Save() {
2015-03-06 13:35:13 +00:00
defer measure("SaveUser", time.Now())
2015-03-20 14:19:18 +00:00
mustExecN(`
insert into users (id, login, name, avatar_url, updated_at)
values (:id, :login, :name, :avatar_url, now())
2015-03-20 14:19:18 +00:00
on duplicate key update
login = values(login),
name = values(name),
avatar_url = values(avatar_url),
updated_at = now()
`, u)
2015-03-05 15:04:44 +00:00
}
2015-03-14 17:34:37 +00:00
func OrgUsers(login string) (users []*User) {
defer measure("OrgUsers", time.Now())
2015-03-20 14:19:18 +00:00
mustSelect(&users, `
select u.*
from org_members om
join users u on u.id = om.user_id
join orgs o on o.id = om.org_id
where o.login = ?
`, login)
2015-03-14 17:34:37 +00:00
return
}