1
0
Fork 0
empact/db/user.go

41 lines
849 B
Go
Raw Normal View History

2015-03-05 15:04:44 +00:00
package db
2015-03-06 13:35:13 +00:00
import "time"
2015-03-06 10:00:04 +00:00
type User struct {
2015-03-19 12:49:12 +00:00
ID uint64 `json:"id"`
2015-03-06 10:00:04 +00:00
Login string `json:"login"`
Name string `json:"name"`
2015-03-06 13:23:01 +00:00
AvatarURL string `json:"avatar_url" db:"avatar_url"`
2015-03-06 10:00:04 +00:00
}
2015-03-05 15:04:44 +00:00
2015-03-14 17:34:37 +00:00
const orgUsersQuery = `
select
u.*
from members m
join teams t on
m.team_id = t.id
join users u on
m.user = u.login
where m.org = ?`
2015-03-06 10:00:04 +00:00
const saveUserQuery = `
2015-03-19 12:49:12 +00:00
insert into users (id, login, name, avatar_url, created_at, updated_at)
values (:id, :login, :name, :avatar_url, now(), now())
2015-03-06 10:00:04 +00:00
on duplicate key update
2015-03-19 12:49:12 +00:00
login = values(login),
name = values(name),
avatar_url = values(avatar_url),
updated_at = now()`
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-06 11:29:52 +00:00
mustExecN(saveUserQuery, 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())
mustSelect(&users, orgUsersQuery, login)
return
}