~evanj/cms

ec3e2007ca0aa953d128deb0f0f5fc040e7f0e84 — Evan J 10 months ago dd8fd32
Fix(Org): Rework how org is pulled.
4 files changed, 32 insertions(+), 53 deletions(-)

M internal/c/space/space.go
M internal/s/db/org.go
M internal/s/db/space.go
M internal/s/db/user.go
M internal/c/space/space.go => internal/c/space/space.go +0 -4
@@ 107,7 107,6 @@ func (s *Space) create(w http.ResponseWriter, r *http.Request) {
	}

	url := fmt.Sprintf("/space/%s", space.ID())
	s.log.Println("successfully created new space for user", user.Name(), "redirecting to", url)
	s.Redirect(w, r, url)
}



@@ 135,7 134,6 @@ func (s *Space) copy(w http.ResponseWriter, r *http.Request) {
	}

	url := fmt.Sprintf("/space/%s", spaceNext.ID())
	s.log.Println("successfully created new space for user", user.Name(), "redirecting to", url)
	s.Redirect(w, r, url)
}



@@ 163,7 161,6 @@ func (s *Space) update(w http.ResponseWriter, r *http.Request) {
	}

	url := fmt.Sprintf("/space/%s", next.ID())
	s.log.Println("successfully updated space for user", user.Name(), "redirecting to", url)
	s.Redirect(w, r, url)
}



@@ 188,7 185,6 @@ func (s *Space) delete(w http.ResponseWriter, r *http.Request) {
	}

	url := "/"
	s.log.Println("successfully deleted space for user", user.Name(), "redirecting to", url)
	s.Redirect(w, r, url)
}


M internal/s/db/org.go => internal/s/db/org.go +0 -8
@@ 75,14 75,6 @@ func (db *DB) orgNew(t *sql.Tx) (org.Org, error) {
	return org, nil
}

func (db *DB) OrgGet(u user.User, orgID string) (org.Org, error) {
	var org Org
	if err := db.QueryRow(queryOrgByUserAndID, u.ID(), orgID).Scan(&org.OrgID, &org.OrgBillingTierName, &org.OrgPaymentCustomer); err != nil {
		return nil, err
	}
	return org, nil
}

func (db *DB) OrgUpdateTier(u user.User, o org.Org, t tier.Tier, paymentCustomerID string) error {
	tx, err := db.Begin()
	if err != nil {

M internal/s/db/space.go => internal/s/db/space.go +10 -17
@@ 19,13 19,7 @@ type Space struct {
	SpaceID   string
	SpaceName string
	SpaceDesc string
	// Set on fetch.
	SpaceOrg org.Org
}

type spaceOrg struct {
	OrgID       string
	OrgTierName string
	SpaceOrg  Org
}

var (


@@ 41,9 35,11 @@ var (
	`

	queryFindSpaceByUserAndID = `
		SELECT cms_space.ID, cms_space.NAME, cms_space.DESCRIPTION FROM cms_space
		SELECT cms_space.ID, cms_space.NAME, cms_space.DESCRIPTION, cms_org.ID, cms_billing.TIER_NAME, cms_billing.PAYMENT_CUSTOMER 
		FROM cms_space
		JOIN cms_org ON cms_org.ID=cms_space.ORG_ID
		JOIN cms_user ON cms_user.ORG_ID=cms_org.ID
		LEFT JOIN cms_billing ON cms_billing.ORG_ID=cms_org.ID
		WHERE cms_user.ID=? 
		AND cms_space.ID=?
	`


@@ 346,18 342,15 @@ func (db *DB) spaceUpdateContent(t *sql.Tx, next space.Space, ct contenttype.Con
}

func (db *DB) spaceGet(t *sql.Tx, user user.User, spaceID string) (space.Space, error) {
	space := Space{
		SpaceOrg: user.Org(),
		// SpaceOrg: spaceOrg{
		// 	OrgID:       user.Org().ID(),
		// 	OrgTierName: user.Org().Tier().Name,
		// },
	}
	err := t.QueryRow(queryFindSpaceByUserAndID, user.ID(), spaceID).Scan(&space.SpaceID, &space.SpaceName, &space.SpaceDesc)
	var s Space
	err := t.QueryRow(queryFindSpaceByUserAndID, user.ID(), spaceID).Scan(
		&s.SpaceID, &s.SpaceName, &s.SpaceDesc,
		&s.SpaceOrg.OrgID, &s.SpaceOrg.OrgBillingTierName, &s.SpaceOrg.OrgPaymentCustomer,
	)
	if err != nil {
		return nil, fmt.Errorf("failed to find space")
	}
	return &space, nil
	return &s, nil
}

func (db *DB) SpaceGet(user user.User, spaceID string) (space.Space, error) {

M internal/s/db/user.go => internal/s/db/user.go +22 -24
@@ 13,12 13,11 @@ type User struct {
	// Stored in DB.
	UserID    string
	UserName  string
	UserOrg   Org
	userHash  string
	userOrgID string
	userEmail sql.NullString
	// Set on read.
	userToken string
	userOrg   org.Org
}

// SQL QUERIES


@@ 27,13 26,19 @@ var (
	queryCreateNewUser = `INSERT INTO cms_user (NAME, HASH, ORG_ID) VALUES (?, ?, ?)`

	queryFindUserByID = `
		SELECT cms_user.ID, NAME, HASH, ORG_ID, EMAIL FROM cms_user 
		SELECT cms_user.ID, NAME, HASH, EMAIL, cms_org.ID, cms_billing.TIER_NAME, cms_billing.PAYMENT_CUSTOMER 
		FROM cms_user 
		JOIN cms_org ON cms_org.ID=cms_user.ORG_ID
		LEFT JOIN cms_billing ON cms_billing.ORG_ID=cms_org.ID
		LEFT JOIN cms_email ON cms_email.USER_ID=cms_user.ID 
		WHERE cms_user.ID = ?
	`

	queryFindUserByName = `
		SELECT cms_user.ID, NAME, HASH, ORG_ID, EMAIL FROM cms_user 
		SELECT cms_user.ID, NAME, HASH, EMAIL, cms_org.ID, cms_billing.TIER_NAME, cms_billing.PAYMENT_CUSTOMER 
		FROM cms_user 
		JOIN cms_org ON cms_org.ID=cms_user.ORG_ID
		LEFT JOIN cms_billing ON cms_billing.ORG_ID=cms_org.ID
		LEFT JOIN cms_email ON cms_email.USER_ID=cms_user.ID 
		WHERE NAME = ?
	`


@@ 80,8 85,10 @@ func (db *DB) userNew(t *sql.Tx, username, password, verifyPassword string) (use
	}

	var user User
	if err := t.QueryRow(queryFindUserByID, id).Scan(&user.UserID, &user.UserName, &user.userHash, &user.userOrgID, &user.userEmail); err != nil {
		fmt.Println(err)
	if err := t.QueryRow(queryFindUserByID, id).Scan(
		&user.UserID, &user.UserName, &user.userHash, &user.userEmail,
		&user.UserOrg.OrgID, &user.UserOrg.OrgBillingTierName, &user.UserOrg.OrgPaymentCustomer,
	); err != nil {
		return nil, fmt.Errorf("failed to find user created")
	}



@@ 91,13 98,15 @@ func (db *DB) userNew(t *sql.Tx, username, password, verifyPassword string) (use
	}

	user.userToken = tok
	user.userOrg = org
	return &user, nil
}

func (db *DB) UserGet(username, password string) (user.User, error) {
	var user User
	if err := db.QueryRow(queryFindUserByName, username).Scan(&user.UserID, &user.UserName, &user.userHash, &user.userOrgID, &user.userEmail); err != nil {
	if err := db.QueryRow(queryFindUserByName, username).Scan(
		&user.UserID, &user.UserName, &user.userHash, &user.userEmail,
		&user.UserOrg.OrgID, &user.UserOrg.OrgBillingTierName, &user.UserOrg.OrgPaymentCustomer,
	); err != nil {
		return nil, fmt.Errorf("failed to find user '%s'", username)
	}



@@ 111,13 120,6 @@ func (db *DB) UserGet(username, password string) (user.User, error) {
	}

	user.userToken = tok

	org, err := db.OrgGet(&user, user.userOrgID)
	if err != nil {
		return nil, err
	}
	user.userOrg = org

	return &user, nil
}



@@ 133,7 135,10 @@ func (db *DB) UserGetFromToken(token string) (user.User, error) {
	}

	var user User
	if err := db.QueryRow(queryFindUserByID, id).Scan(&user.UserID, &user.UserName, &user.userHash, &user.userOrgID, &user.userEmail); err != nil {
	if err := db.QueryRow(queryFindUserByID, id).Scan(
		&user.UserID, &user.UserName, &user.userHash, &user.userEmail,
		&user.UserOrg.OrgID, &user.UserOrg.OrgBillingTierName, &user.UserOrg.OrgPaymentCustomer,
	); err != nil {

		fmt.Println(err)
		return nil, fmt.Errorf("failed to find user")


@@ 145,13 150,6 @@ func (db *DB) UserGetFromToken(token string) (user.User, error) {
	}

	user.userToken = tok

	org, err := db.OrgGet(&user, user.userOrgID)
	if err != nil {
		return nil, err
	}
	user.userOrg = org

	return &user, nil
}



@@ 166,6 164,6 @@ func (db *DB) UserSetEmail(u user.User, email string) (user.User, error) {
func (u *User) ID() string     { return u.UserID }
func (u *User) Name() string   { return u.UserName }
func (u *User) Token() string  { return u.userToken }
func (u *User) Org() org.Org   { return u.userOrg }
func (u *User) Org() org.Org   { return u.UserOrg }
func (u *User) HasEmail() bool { return u.userEmail.Valid }
func (u *User) Email() string  { return u.userEmail.String }