From fcdcd63dc7ccd47f2318fcf9aecdc32b004e7012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 24 Nov 2014 08:04:41 +0100 Subject: [PATCH] get other accounts works --- pkg/models/account.go | 6 ++--- pkg/models/collaborator.go | 4 +-- pkg/routes/api/api_account.go | 31 ++++++++++++++++++++++-- pkg/routes/dtos/models.go | 7 ++++++ pkg/routes/index.go | 1 + pkg/stores/sqlstore/sqlstore.go | 1 + pkg/stores/sqlstore/sqlstore_accounts.go | 12 +++++++++ 7 files changed, 55 insertions(+), 7 deletions(-) diff --git a/pkg/models/account.go b/pkg/models/account.go index 4c7d0f840f3..6c8d87d422c 100644 --- a/pkg/models/account.go +++ b/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 { diff --git a/pkg/models/collaborator.go b/pkg/models/collaborator.go index d7f57df7fc4..c02dd0f8b17 100644 --- a/pkg/models/collaborator.go +++ b/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(), } diff --git a/pkg/routes/api/api_account.go b/pkg/routes/api/api_account.go index 1f47d96e9d0..c46fa42042c 100644 --- a/pkg/routes/api/api_account.go +++ b/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) +} diff --git a/pkg/routes/dtos/models.go b/pkg/routes/dtos/models.go index 99d40d3f585..c60d5614db3 100644 --- a/pkg/routes/dtos/models.go +++ b/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"` diff --git a/pkg/routes/index.go b/pkg/routes/index.go index 44ed96f75ac..95530223583 100644 --- a/pkg/routes/index.go +++ b/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) diff --git a/pkg/stores/sqlstore/sqlstore.go b/pkg/stores/sqlstore/sqlstore.go index f9a8244a380..e18db0f88c5 100644 --- a/pkg/stores/sqlstore/sqlstore.go +++ b/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 diff --git a/pkg/stores/sqlstore/sqlstore_accounts.go b/pkg/stores/sqlstore/sqlstore_accounts.go index 2332e6752b9..d7320455ce2 100644 --- a/pkg/stores/sqlstore/sqlstore_accounts.go +++ b/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 +}