Update user model
This commit is contained in:
parent
923c9b63e3
commit
60877e5393
43
db/user.go
43
db/user.go
|
@ -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 = `
|
||||
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)
|
||||
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()
|
||||
`, 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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue