Browse Source

get other accounts works

Torkel Ödegaard 11 years ago
parent
commit
fcdcd63dc7

+ 3 - 3
pkg/models/account.go

@@ -29,9 +29,9 @@ type CollaboratorLink struct {
 }
 
 type OtherAccount struct {
-	Id   int `gorethink:"id"`
-	Name string
-	Role string
+	Id    int64
+	Email string
+	Role  string
 }
 
 type Account struct {

+ 2 - 2
pkg/models/collaborator.go

@@ -27,11 +27,11 @@ type CollaboratorInfo struct {
 	Email     string
 }
 
-func NewCollaborator(accountId int64, forAccountId int64) *Collaborator {
+func NewCollaborator(accountId int64, forAccountId int64, role RoleType) *Collaborator {
 	return &Collaborator{
 		AccountId:    accountId,
 		ForAccountId: forAccountId,
-		Role:         ROLE_READ,
+		Role:         role,
 		Created:      time.Now(),
 		Updated:      time.Now(),
 	}

+ 29 - 2
pkg/routes/api/api_account.go

@@ -49,8 +49,7 @@ func AddCollaborator(c *middleware.Context) {
 		return
 	}
 
-	var collaborator = models.NewCollaborator(accountToAdd.Id, c.UserAccount.Id)
-	collaborator.Role = models.ROLE_READ_WRITE
+	var collaborator = models.NewCollaborator(accountToAdd.Id, c.UserAccount.Id, models.ROLE_READ_WRITE)
 
 	err = models.AddCollaborator(collaborator)
 	if err != nil {
@@ -60,3 +59,31 @@ func AddCollaborator(c *middleware.Context) {
 
 	c.Status(204)
 }
+
+func GetOtherAccounts(c *middleware.Context) {
+
+	otherAccounts, err := models.GetOtherAccountsFor(c.UserAccount.Id)
+	if err != nil {
+		c.JSON(500, utils.DynMap{"message": err.Error()})
+		return
+	}
+
+	var result []*dtos.OtherAccount
+	result = append(result, &dtos.OtherAccount{
+		Id:      c.UserAccount.Id,
+		Role:    "owner",
+		IsUsing: c.UserAccount.Id == c.UserAccount.UsingAccountId,
+		Name:    c.UserAccount.Email,
+	})
+
+	for _, other := range otherAccounts {
+		result = append(result, &dtos.OtherAccount{
+			Id:      other.Id,
+			Role:    other.Role,
+			Name:    other.Email,
+			IsUsing: other.Id == c.UserAccount.UsingAccountId,
+		})
+	}
+
+	c.JSON(200, result)
+}

+ 7 - 0
pkg/routes/dtos/models.go

@@ -25,6 +25,13 @@ type AccountInfo struct {
 	Collaborators []*Collaborator `json:"collaborators"`
 }
 
+type OtherAccount struct {
+	Id      int64  `json:"id"`
+	Name    string `json:"name"`
+	Role    string `json:"role"`
+	IsUsing bool   `json:"isUsing"`
+}
+
 type Collaborator struct {
 	AccountId int64  `json:"accountId"`
 	Email     string `json:"email"`

+ 1 - 0
pkg/routes/index.go

@@ -24,6 +24,7 @@ func Register(m *macaron.Macaron) {
 	m.Get("/account/", auth, Index)
 	m.Get("/api/account/", auth, api.GetAccount)
 	m.Post("/api/account/collaborators/add", auth, api.AddCollaborator)
+	m.Get("/api/account/others", auth, api.GetOtherAccounts)
 
 	// user register
 	m.Get("/register/*_", Index)

+ 1 - 0
pkg/stores/sqlstore/sqlstore.go

@@ -32,6 +32,7 @@ func Init() {
 	models.CreateAccount = CreateAccount
 	models.GetAccount = GetAccount
 	models.GetAccountByLogin = GetAccountByLogin
+	models.GetOtherAccountsFor = GetOtherAccountsFor
 	models.GetDashboard = GetDashboard
 	models.SaveDashboard = SaveDashboard
 	models.SearchQuery = SearchQuery

+ 12 - 0
pkg/stores/sqlstore/sqlstore_accounts.go

@@ -60,9 +60,12 @@ func GetAccountByLogin(emailOrLogin string) (*models.Account, error) {
 
 func GetCollaboratorsForAccount(accountId int64) ([]*models.CollaboratorInfo, error) {
 	collaborators := make([]*models.CollaboratorInfo, 0)
+
 	sess := x.Table("Collaborator")
 	sess.Join("INNER", "Account", "Account.id=Collaborator.account_Id")
+	sess.Where("Collaborator.for_account_id=?", accountId)
 	err := sess.Find(&collaborators)
+
 	return collaborators, err
 }
 
@@ -85,3 +88,12 @@ func AddCollaborator(collaborator *models.Collaborator) error {
 
 	return nil
 }
+
+func GetOtherAccountsFor(accountId int64) ([]*models.OtherAccount, error) {
+	collaborators := make([]*models.OtherAccount, 0)
+	sess := x.Table("Collaborator")
+	sess.Join("INNER", "Account", "Account.id=Collaborator.account_Id")
+	sess.Where("Collaborator.account_id=?", accountId)
+	err := sess.Find(&collaborators)
+	return collaborators, err
+}