More refactoring and aligning code to the command query model
This commit is contained in:
@@ -4,9 +4,10 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||
"github.com/torkelo/grafana-pro/pkg/log"
|
||||
"github.com/torkelo/grafana-pro/pkg/middleware"
|
||||
"github.com/torkelo/grafana-pro/pkg/models"
|
||||
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||
"github.com/torkelo/grafana-pro/pkg/setting"
|
||||
"github.com/torkelo/grafana-pro/pkg/social"
|
||||
)
|
||||
@@ -48,21 +49,23 @@ func OAuthLogin(ctx *middleware.Context) {
|
||||
|
||||
log.Info("login.OAuthLogin(social login): %s", userInfo)
|
||||
|
||||
account, err := models.GetAccountByLogin(userInfo.Email)
|
||||
account, err := m.GetAccountByLogin(userInfo.Email)
|
||||
|
||||
// create account if missing
|
||||
if err == models.ErrAccountNotFound {
|
||||
account = &models.Account{
|
||||
if err == m.ErrAccountNotFound {
|
||||
cmd := &m.CreateAccountCommand{
|
||||
Login: userInfo.Email,
|
||||
Email: userInfo.Email,
|
||||
Name: userInfo.Name,
|
||||
Company: userInfo.Company,
|
||||
}
|
||||
|
||||
if err = models.SaveAccount(account); err != nil {
|
||||
if err = bus.Dispatch(&cmd); err != nil {
|
||||
ctx.Handle(500, "Failed to create account", err)
|
||||
return
|
||||
}
|
||||
|
||||
account = &cmd.Result
|
||||
} else if err != nil {
|
||||
ctx.Handle(500, "Unexpected error", err)
|
||||
}
|
||||
|
||||
+9
-21
@@ -1,38 +1,26 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/torkelo/grafana-pro/pkg/log"
|
||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||
"github.com/torkelo/grafana-pro/pkg/middleware"
|
||||
"github.com/torkelo/grafana-pro/pkg/models"
|
||||
"github.com/torkelo/grafana-pro/pkg/utils"
|
||||
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||
)
|
||||
|
||||
type registerAccountJsonModel struct {
|
||||
Email string `json:"email" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
Password2 bool `json:"remember2"`
|
||||
}
|
||||
|
||||
func CreateAccount(c *middleware.Context) {
|
||||
var registerModel registerAccountJsonModel
|
||||
var cmd m.CreateAccountCommand
|
||||
|
||||
if !c.JsonBody(®isterModel) {
|
||||
c.JSON(400, utils.DynMap{"status": "bad request"})
|
||||
if !c.JsonBody(&cmd) {
|
||||
c.JsonApiErr(400, "Validation error", nil)
|
||||
return
|
||||
}
|
||||
|
||||
account := models.Account{
|
||||
Login: registerModel.Email,
|
||||
Email: registerModel.Email,
|
||||
Password: registerModel.Password,
|
||||
}
|
||||
cmd.Login = cmd.Email
|
||||
err := bus.Dispatch(&cmd)
|
||||
|
||||
err := models.SaveAccount(&account)
|
||||
if err != nil {
|
||||
log.Error(2, "Failed to create user account, email: %v, error: %v", registerModel.Email, err)
|
||||
c.JSON(500, utils.DynMap{"status": "failed to create account"})
|
||||
c.JsonApiErr(500, "failed to create account", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, utils.DynMap{"status": "ok"})
|
||||
c.JsonOK("Account created")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user