Lots of progress on account management

This commit is contained in:
Torkel Ödegaard
2014-09-21 15:01:59 +02:00
parent 19a35673fa
commit cd9306df45
12 changed files with 317 additions and 110 deletions
+30 -3
View File
@@ -8,10 +8,17 @@ import (
type CollaboratorLink struct {
AccountId int
Role string
Email string
ModifiedOn time.Time
CreatedOn time.Time
}
type OtherAccount struct {
Id int `gorethink:"id"`
Name string
Role string
}
type Account struct {
Id int `gorethink:"id"`
Version int
@@ -27,15 +34,16 @@ type Account struct {
LastLoginOn time.Time
}
func (account *Account) AddCollaborator(accountId int) error {
func (account *Account) AddCollaborator(newCollaborator *Account) error {
for _, collaborator := range account.Collaborators {
if collaborator.AccountId == accountId {
if collaborator.AccountId == newCollaborator.Id {
return errors.New("Collaborator already exists")
}
}
account.Collaborators = append(account.Collaborators, CollaboratorLink{
AccountId: accountId,
AccountId: newCollaborator.Id,
Email: newCollaborator.Email,
Role: "admin",
CreatedOn: time.Now(),
ModifiedOn: time.Now(),
@@ -43,3 +51,22 @@ func (account *Account) AddCollaborator(accountId int) error {
return nil
}
func (account *Account) RemoveCollaborator(accountId int) {
list := account.Collaborators
for i, collaborator := range list {
if collaborator.AccountId == accountId {
account.Collaborators = append(list[:i], list[i+1:]...)
break
}
}
}
func (account *Account) HasCollaborator(accountId int) bool {
for _, collaborator := range account.Collaborators {
if collaborator.AccountId == accountId {
return true
}
}
return false
}