1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-08-07 22:45:24 +02:00

[WIP] new schema implementation

This commit is contained in:
Harvey Kandola 2018-09-18 20:55:40 +01:00
parent 9c2594b1b4
commit 28342fcf5e
27 changed files with 1413 additions and 1158 deletions

View file

@ -33,7 +33,7 @@ func (s Scope) Add(ctx domain.RequestContext, account account.Account) (err erro
account.Created = time.Now().UTC()
account.Revised = time.Now().UTC()
_, err = ctx.Transaction.Exec("INSERT INTO account (refid, orgid, userid, `admin`, editor, users, analytics, active, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
_, err = ctx.Transaction.Exec("INSERT INTO dmz_user_account (c_refid, c_orgid, c_userid, c_admin, c_editor, c_users, c_analytics, c_active, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
account.RefID, account.OrgID, account.UserID, account.Admin, account.Editor, account.Users, account.Analytics, account.Active, account.Created, account.Revised)
if err != nil {
@ -46,10 +46,13 @@ func (s Scope) Add(ctx domain.RequestContext, account account.Account) (err erro
// GetUserAccount returns the database account record corresponding to the given userID, using the client's current organizaion.
func (s Scope) GetUserAccount(ctx domain.RequestContext, userID string) (account account.Account, err error) {
err = s.Runtime.Db.Get(&account, `
SELECT a.id, a.refid, a.orgid, a.userid, a.editor, a.admin, a.users, a.analytics, a.active, a.created, a.revised,
b.company, b.title, b.message, b.domain
FROM account a, organization b
WHERE b.refid=a.orgid AND a.orgid=? AND a.userid=?`, ctx.OrgID, userID)
SELECT a.id, a.c_refid AS refid, a.c_orgid AS orgid, a.c_userid AS userid,
a.c_editor AS editor, a.c_admin AS admin, a.c_users AS users, a.c_analytics AS analytics,
a.c_active AS active, a.c_created AS created, a.c_revised AS revised,
b.c_company AS company, b.c_title AS title, b.c_message AS message, b.c_domain as domain
FROM dmz_user_account a, dmz_org b
WHERE b.c_refid=a.c_orgid AND a.c_orgid=? AND a.c_userid=?`,
ctx.OrgID, userID)
if err != sql.ErrNoRows && err != nil {
err = errors.Wrap(err, fmt.Sprintf("execute select for account by user %s", userID))
@ -61,10 +64,14 @@ func (s Scope) GetUserAccount(ctx domain.RequestContext, userID string) (account
// GetUserAccounts returns a slice of database account records, for all organizations that the userID is a member of, in organization title order.
func (s Scope) GetUserAccounts(ctx domain.RequestContext, userID string) (t []account.Account, err error) {
err = s.Runtime.Db.Select(&t, `
SELECT a.id, a.refid, a.orgid, a.userid, a.editor, a.admin, a.users, a.analytics, a.active, a.created, a.revised,
b.company, b.title, b.message, b.domain
FROM account a, organization b
WHERE a.userid=? AND a.orgid=b.refid AND a.active=1 ORDER BY b.title`, userID)
SELECT a.id, a.c_refid AS refid, a.c_orgid AS orgid, a.c_userid AS userid,
a.c_editor AS editor, a.c_admin AS admin, a.c_users AS users, a.c_analytics AS analytics,
a.c_active AS active, a.c_created AS created, a.c_revised AS revised,
b.c_company AS company, b.c_title AS title, b.c_message AS message, b.c_domain as domain
FROM dmz_user_account a, dmz_org b
WHERE a.c_userid=? AND a.c_orgid=b.c_refid AND a.c_active=1
ORDER BY b.c_title`,
userID)
if err != sql.ErrNoRows && err != nil {
err = errors.Wrap(err, fmt.Sprintf("Unable to execute select account for user %s", userID))
@ -75,11 +82,14 @@ func (s Scope) GetUserAccounts(ctx domain.RequestContext, userID string) (t []ac
// GetAccountsByOrg returns a slice of database account records, for all users in the client's organization.
func (s Scope) GetAccountsByOrg(ctx domain.RequestContext) (t []account.Account, err error) {
err = s.Runtime.Db.Select(&t,
`SELECT a.id, a.refid, a.orgid, a.userid, a.editor, a.admin, a.users, a.analytics, a.active, a.created, a.revised,
b.company, b.title, b.message, b.domain
FROM account a, organization b
WHERE a.orgid=b.refid AND a.orgid=? AND a.active=1`, ctx.OrgID)
err = s.Runtime.Db.Select(&t, `
SELECT a.id, a.c_refid AS refid, a.c_orgid AS orgid, a.c_userid AS userid,
a.c_editor AS editor, a.c_admin AS admin, a.c_users AS users, a.c_analytics AS analytics,
a.c_active AS active, a.c_created AS created, a.c_revised AS revised,
b.c_company AS company, b.c_title AS title, b.c_message AS message, b.c_domain as domain
FROM dmz_user_account a, dmz_org b
WHERE a.c_orgid=b.c_refid AND a.c_orgid=? AND a.c_active=1`,
ctx.OrgID)
if err != sql.ErrNoRows && err != nil {
err = errors.Wrap(err, fmt.Sprintf("execute select account for org %s", ctx.OrgID))
@ -90,10 +100,8 @@ func (s Scope) GetAccountsByOrg(ctx domain.RequestContext) (t []account.Account,
// CountOrgAccounts returns the numnber of active user accounts for specified organization.
func (s Scope) CountOrgAccounts(ctx domain.RequestContext) (c int) {
row := s.Runtime.Db.QueryRow("SELECT count(*) FROM account WHERE orgid=? AND active=1", ctx.OrgID)
row := s.Runtime.Db.QueryRow("SELECT count(*) FROM dmz_user_account WHERE c_orgid=? AND c_active=1", ctx.OrgID)
err := row.Scan(&c)
if err == sql.ErrNoRows {
return 0
}
@ -109,7 +117,10 @@ func (s Scope) CountOrgAccounts(ctx domain.RequestContext) (c int) {
func (s Scope) UpdateAccount(ctx domain.RequestContext, account account.Account) (err error) {
account.Revised = time.Now().UTC()
_, err = ctx.Transaction.NamedExec("UPDATE account SET userid=:userid, `admin`=:admin, editor=:editor, users=:users, analytics=:analytics, active=:active, revised=:revised WHERE orgid=:orgid AND refid=:refid", &account)
_, err = ctx.Transaction.NamedExec(`
UPDATE dmz_user_account SET
c_userid=:userid, c_admin=:admin, c_editor=:editor, c_users=:users, c_analytics=:analytics,
c_active=:active, c_revised=:revised WHERE c_orgid=:orgid AND c_refid=:refid`, &account)
if err != sql.ErrNoRows && err != nil {
err = errors.Wrap(err, fmt.Sprintf("execute update for account %s", account.RefID))
@ -120,7 +131,7 @@ func (s Scope) UpdateAccount(ctx domain.RequestContext, account account.Account)
// HasOrgAccount returns if the given orgID has valid userID.
func (s Scope) HasOrgAccount(ctx domain.RequestContext, orgID, userID string) bool {
row := s.Runtime.Db.QueryRow("SELECT count(*) FROM account WHERE orgid=? and userid=?", orgID, userID)
row := s.Runtime.Db.QueryRow("SELECT count(*) FROM dmz_user_account WHERE c_orgid=? and c_userid=?", orgID, userID)
var count int
err := row.Scan(&count)
@ -128,7 +139,6 @@ func (s Scope) HasOrgAccount(ctx domain.RequestContext, orgID, userID string) bo
if err == sql.ErrNoRows {
return false
}
if err != nil && err != sql.ErrNoRows {
err = errors.Wrap(err, "HasOrgAccount")
return false
@ -144,5 +154,5 @@ func (s Scope) HasOrgAccount(ctx domain.RequestContext, orgID, userID string) bo
// DeleteAccount deletes the database record in the account table for user ID.
func (s Scope) DeleteAccount(ctx domain.RequestContext, ID string) (rows int64, err error) {
b := mysql.BaseQuery{}
return b.DeleteConstrained(ctx.Transaction, "account", ctx.OrgID, ID)
return b.DeleteConstrained(ctx.Transaction, "dmz_user_account", ctx.OrgID, ID)
}