Commit 40ff57d8 by Torkel Ödegaard

Account stuff

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