Commit 40ff57d8 by Torkel Ödegaard

Account stuff

parent cdabe503
...@@ -64,7 +64,7 @@ func (self *HttpServer) index(c *gin.Context) { ...@@ -64,7 +64,7 @@ func (self *HttpServer) index(c *gin.Context) {
viewModel := &IndexDto{} viewModel := &IndexDto{}
userAccount, _ := c.Get("userAccount") userAccount, _ := c.Get("userAccount")
if userAccount != nil { if userAccount != nil {
viewModel.User.Login = userAccount.(*models.UserAccount).Login viewModel.User.Login = userAccount.(*models.Account).Login
} }
c.HTML(200, "index.html", viewModel) c.HTML(200, "index.html", viewModel)
......
...@@ -20,7 +20,7 @@ func (self *HttpServer) addCollaborator(c *gin.Context, auth *authContext) { ...@@ -20,7 +20,7 @@ func (self *HttpServer) addCollaborator(c *gin.Context, auth *authContext) {
return return
} }
collaborator, err := self.store.GetUserAccountLogin(model.Email) collaborator, err := self.store.GetAccountByLogin(model.Email)
if err != nil { if err != nil {
c.JSON(404, gin.H{"status": "Collaborator not found"}) c.JSON(404, gin.H{"status": "Collaborator not found"})
return return
...@@ -39,7 +39,11 @@ func (self *HttpServer) addCollaborator(c *gin.Context, auth *authContext) { ...@@ -39,7 +39,11 @@ func (self *HttpServer) addCollaborator(c *gin.Context, auth *authContext) {
return return
} }
self.store.SaveUserAccount(userAccount) err = self.store.UpdateAccount(userAccount)
if err != nil {
c.JSON(500, gin.H{"status": err.Error()})
return
}
c.JSON(200, gin.H{"status": "Collaborator added"}) c.JSON(200, gin.H{"status": "Collaborator added"})
} }
...@@ -6,8 +6,8 @@ import ( ...@@ -6,8 +6,8 @@ import (
) )
type authContext struct { type authContext struct {
account *models.UserAccount account *models.Account
userAccount *models.UserAccount userAccount *models.Account
} }
func (auth *authContext) getAccountId() int { func (auth *authContext) getAccountId() int {
......
...@@ -24,7 +24,7 @@ func (self *HttpServer) loginPost(c *gin.Context) { ...@@ -24,7 +24,7 @@ func (self *HttpServer) loginPost(c *gin.Context) {
return return
} }
account, err := self.store.GetUserAccountLogin(loginModel.Email) account, err := self.store.GetAccountByLogin(loginModel.Email)
if err != nil { if err != nil {
c.JSON(400, gin.H{"status": "some error"}) c.JSON(400, gin.H{"status": "some error"})
} }
......
...@@ -27,14 +27,14 @@ func (self *HttpServer) registerUserPost(c *gin.Context) { ...@@ -27,14 +27,14 @@ func (self *HttpServer) registerUserPost(c *gin.Context) {
return return
} }
account := models.UserAccount{ account := models.Account{
UserName: registerModel.Email, UserName: registerModel.Email,
Login: registerModel.Email, Login: registerModel.Email,
Email: registerModel.Email, Email: registerModel.Email,
Password: registerModel.Password, Password: registerModel.Password,
} }
err := self.store.SaveUserAccount(&account) err := self.store.CreateAccount(&account)
if err != nil { if err != nil {
log.Error("Failed to create user account, email: %v, error: %v", registerModel.Email, err) log.Error("Failed to create user account, email: %v, error: %v", registerModel.Email, err)
c.JSON(500, gin.H{"status": "failed to create account"}) c.JSON(500, gin.H{"status": "failed to create account"})
......
...@@ -13,8 +13,8 @@ var routeHandlers = make([]routeHandlerRegisterFn, 0) ...@@ -13,8 +13,8 @@ var routeHandlers = make([]routeHandlerRegisterFn, 0)
func getRouteHandlerWrapper(handler routeHandlerFn) gin.HandlerFunc { func getRouteHandlerWrapper(handler routeHandlerFn) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
authContext := authContext{ authContext := authContext{
account: c.MustGet("usingAccount").(*models.UserAccount), account: c.MustGet("usingAccount").(*models.Account),
userAccount: c.MustGet("userAccount").(*models.UserAccount), userAccount: c.MustGet("userAccount").(*models.Account),
} }
handler(c, &authContext) handler(c, &authContext)
} }
......
...@@ -12,7 +12,7 @@ type CollaboratorLink struct { ...@@ -12,7 +12,7 @@ type CollaboratorLink struct {
CreatedOn time.Time CreatedOn time.Time
} }
type UserAccount struct { type Account struct {
Id int `gorethink:"id"` Id int `gorethink:"id"`
UserName string UserName string
Login string Login string
...@@ -25,7 +25,7 @@ type UserAccount struct { ...@@ -25,7 +25,7 @@ type UserAccount struct {
ModifiedOn time.Time ModifiedOn time.Time
} }
func (account *UserAccount) AddCollaborator(accountId int) error { func (account *Account) AddCollaborator(accountId int) error {
for _, collaborator := range account.Collaborators { for _, collaborator := range account.Collaborators {
if collaborator.AccountId == accountId { if collaborator.AccountId == accountId {
return errors.New("Collaborator already exists") return errors.New("Collaborator already exists")
......
...@@ -25,7 +25,7 @@ func (self *rethinkStore) getNextAccountId() (int, error) { ...@@ -25,7 +25,7 @@ func (self *rethinkStore) getNextAccountId() (int, error) {
return int(change.NewValue.(map[string]interface{})["NextAccountId"].(float64)), nil return int(change.NewValue.(map[string]interface{})["NextAccountId"].(float64)), nil
} }
func (self *rethinkStore) SaveUserAccount(account *models.UserAccount) error { func (self *rethinkStore) CreateAccount(account *models.Account) error {
accountId, err := self.getNextAccountId() accountId, err := self.getNextAccountId()
if err != nil { if err != nil {
return err return err
...@@ -46,14 +46,14 @@ func (self *rethinkStore) SaveUserAccount(account *models.UserAccount) error { ...@@ -46,14 +46,14 @@ func (self *rethinkStore) SaveUserAccount(account *models.UserAccount) error {
return nil return nil
} }
func (self *rethinkStore) GetUserAccountLogin(emailOrName string) (*models.UserAccount, error) { func (self *rethinkStore) GetAccountByLogin(emailOrName string) (*models.Account, error) {
resp, err := r.Table("accounts").GetAllByIndex("AccountLogin", []interface{}{emailOrName}).Run(self.session) resp, err := r.Table("accounts").GetAllByIndex("AccountLogin", []interface{}{emailOrName}).Run(self.session)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var account models.UserAccount var account models.Account
err = resp.One(&account) err = resp.One(&account)
if err != nil { if err != nil {
return nil, errors.New("Not found") return nil, errors.New("Not found")
...@@ -62,14 +62,14 @@ func (self *rethinkStore) GetUserAccountLogin(emailOrName string) (*models.UserA ...@@ -62,14 +62,14 @@ func (self *rethinkStore) GetUserAccountLogin(emailOrName string) (*models.UserA
return &account, nil return &account, nil
} }
func (self *rethinkStore) GetAccount(id int) (*models.UserAccount, error) { func (self *rethinkStore) GetAccount(id int) (*models.Account, error) {
resp, err := r.Table("accounts").Get(id).Run(self.session) resp, err := r.Table("accounts").Get(id).Run(self.session)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var account models.UserAccount var account models.Account
err = resp.One(&account) err = resp.One(&account)
if err != nil { if err != nil {
return nil, errors.New("Not found") return nil, errors.New("Not found")
...@@ -78,6 +78,19 @@ func (self *rethinkStore) GetAccount(id int) (*models.UserAccount, error) { ...@@ -78,6 +78,19 @@ func (self *rethinkStore) GetAccount(id int) (*models.UserAccount, error) {
return &account, nil return &account, nil
} }
func (self *rethinkStore) UpdateAccount(account *models.Account) error {
resp, err := r.Table("accounts").Update(account).RunWrite(self.session)
if err != nil {
return err
}
if resp.Replaced != 1 {
return errors.New("Could not fund account to uodate")
}
return nil
}
func (self *rethinkStore) getNextDashboardNumber(accountId int) (int, error) { func (self *rethinkStore) getNextDashboardNumber(accountId int) (int, error) {
resp, err := r.Table("accounts").Get(accountId).Update(map[string]interface{}{ resp, err := r.Table("accounts").Get(accountId).Update(map[string]interface{}{
"NextDashboardId": r.Row.Field("NextDashboardId").Add(1), "NextDashboardId": r.Row.Field("NextDashboardId").Add(1),
......
...@@ -35,8 +35,8 @@ func TestRethinkStore(t *testing.T) { ...@@ -35,8 +35,8 @@ func TestRethinkStore(t *testing.T) {
}) })
Convey("can create account", t, func() { Convey("can create account", t, func() {
account := &models.UserAccount{UserName: "torkelo", Email: "mupp", Login: "test@test.com"} account := &models.Account{UserName: "torkelo", Email: "mupp", Login: "test@test.com"}
err := store.SaveUserAccount(account) err := store.CreateAccount(account)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(account.Id, ShouldNotEqual, 0) So(account.Id, ShouldNotEqual, 0)
...@@ -46,8 +46,8 @@ func TestRethinkStore(t *testing.T) { ...@@ -46,8 +46,8 @@ func TestRethinkStore(t *testing.T) {
}) })
Convey("can get next dashboard id", t, func() { Convey("can get next dashboard id", t, func() {
account := &models.UserAccount{UserName: "torkelo", Email: "mupp"} account := &models.Account{UserName: "torkelo", Email: "mupp"}
err := store.SaveUserAccount(account) err := store.CreateAccount(account)
dashId, err := store.getNextDashboardNumber(account.Id) dashId, err := store.getNextDashboardNumber(account.Id)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(dashId, ShouldEqual, 1) So(dashId, ShouldEqual, 1)
......
...@@ -9,9 +9,10 @@ type Store interface { ...@@ -9,9 +9,10 @@ type Store interface {
SaveDashboard(dash *models.Dashboard) error SaveDashboard(dash *models.Dashboard) error
DeleteDashboard(slug string, accountId int) error DeleteDashboard(slug string, accountId int) error
Query(query string, acccountId int) ([]*models.SearchResult, error) Query(query string, acccountId int) ([]*models.SearchResult, error)
SaveUserAccount(acccount *models.UserAccount) error CreateAccount(acccount *models.Account) error
GetUserAccountLogin(emailOrName string) (*models.UserAccount, error) UpdateAccount(acccount *models.Account) error
GetAccount(id int) (*models.UserAccount, error) GetAccountByLogin(emailOrName string) (*models.Account, error)
GetAccount(id int) (*models.Account, error)
Close() Close()
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment