1
0
Fork 0

Update user model

This commit is contained in:
Gregory Eremin 2015-03-20 21:19:18 +07:00
parent 923c9b63e3
commit 60877e5393
1 changed files with 25 additions and 26 deletions

View File

@ -1,40 +1,39 @@
package db
import "time"
import (
"time"
)
type User struct {
ID uint64 `json:"id"`
Login string `json:"login"`
Name string `json:"name"`
AvatarURL string `json:"avatar_url" db:"avatar_url"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
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 = ?`
const saveUserQuery = `
func (u *User) Save() {
defer measure("SaveUser", time.Now())
mustExecN(`
insert into users (id, login, name, avatar_url, created_at, updated_at)
values (:id, :login, :name, :avatar_url, now(), now())
on duplicate key update
login = values(login),
name = values(name),
avatar_url = values(avatar_url),
updated_at = now()`
func (u *User) Save() {
defer measure("SaveUser", time.Now())
mustExecN(saveUserQuery, u)
updated_at = now()
`, u)
}
func OrgUsers(login string) (users []*User) {
defer measure("OrgUsers", time.Now())
mustSelect(&users, orgUsersQuery, login)
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)
return
}