Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
607b0c0c
Commit
607b0c0c
authored
Dec 19, 2014
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More refactoring and aligning code to the command query model
parent
d5a59ac6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
54 additions
and
68 deletions
+54
-68
pkg/api/api_login_oauth.go
+8
-5
pkg/api/api_register.go
+9
-21
pkg/models/account.go
+10
-1
pkg/stores/sqlstore/accounts.go
+18
-26
pkg/stores/sqlstore/accounts_test.go
+9
-14
pkg/stores/sqlstore/sqlstore.go
+0
-1
No files found.
pkg/api/api_login_oauth.go
View file @
607b0c0c
...
@@ -4,9 +4,10 @@ import (
...
@@ -4,9 +4,10 @@ import (
"errors"
"errors"
"fmt"
"fmt"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/log"
"github.com/torkelo/grafana-pro/pkg/log"
"github.com/torkelo/grafana-pro/pkg/middleware"
"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/setting"
"github.com/torkelo/grafana-pro/pkg/social"
"github.com/torkelo/grafana-pro/pkg/social"
)
)
...
@@ -48,21 +49,23 @@ func OAuthLogin(ctx *middleware.Context) {
...
@@ -48,21 +49,23 @@ func OAuthLogin(ctx *middleware.Context) {
log
.
Info
(
"login.OAuthLogin(social login): %s"
,
userInfo
)
log
.
Info
(
"login.OAuthLogin(social login): %s"
,
userInfo
)
account
,
err
:=
m
odels
.
GetAccountByLogin
(
userInfo
.
Email
)
account
,
err
:=
m
.
GetAccountByLogin
(
userInfo
.
Email
)
// create account if missing
// create account if missing
if
err
==
m
odels
.
ErrAccountNotFound
{
if
err
==
m
.
ErrAccountNotFound
{
account
=
&
models
.
Account
{
cmd
:=
&
m
.
CreateAccountCommand
{
Login
:
userInfo
.
Email
,
Login
:
userInfo
.
Email
,
Email
:
userInfo
.
Email
,
Email
:
userInfo
.
Email
,
Name
:
userInfo
.
Name
,
Name
:
userInfo
.
Name
,
Company
:
userInfo
.
Company
,
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
)
ctx
.
Handle
(
500
,
"Failed to create account"
,
err
)
return
return
}
}
account
=
&
cmd
.
Result
}
else
if
err
!=
nil
{
}
else
if
err
!=
nil
{
ctx
.
Handle
(
500
,
"Unexpected error"
,
err
)
ctx
.
Handle
(
500
,
"Unexpected error"
,
err
)
}
}
...
...
pkg/api/api_register.go
View file @
607b0c0c
package
api
package
api
import
(
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/middleware"
"github.com/torkelo/grafana-pro/pkg/models"
m
"github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/utils"
)
)
type
registerAccountJsonModel
struct
{
Email
string
`json:"email" binding:"required"`
Password
string
`json:"password" binding:"required"`
Password2
bool
`json:"remember2"`
}
func
CreateAccount
(
c
*
middleware
.
Context
)
{
func
CreateAccount
(
c
*
middleware
.
Context
)
{
var
registerModel
registerAccountJsonModel
var
cmd
m
.
CreateAccountCommand
if
!
c
.
JsonBody
(
&
registerModel
)
{
if
!
c
.
JsonBody
(
&
cmd
)
{
c
.
J
SON
(
400
,
utils
.
DynMap
{
"status"
:
"bad request"
}
)
c
.
J
sonApiErr
(
400
,
"Validation error"
,
nil
)
return
return
}
}
account
:=
models
.
Account
{
cmd
.
Login
=
cmd
.
Email
Login
:
registerModel
.
Email
,
err
:=
bus
.
Dispatch
(
&
cmd
)
Email
:
registerModel
.
Email
,
Password
:
registerModel
.
Password
,
}
err
:=
models
.
SaveAccount
(
&
account
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Error
(
2
,
"Failed to create user account, email: %v, error: %v"
,
registerModel
.
Email
,
err
)
c
.
JsonApiErr
(
500
,
"failed to create account"
,
err
)
c
.
JSON
(
500
,
utils
.
DynMap
{
"status"
:
"failed to create account"
})
return
return
}
}
c
.
J
SON
(
200
,
utils
.
DynMap
{
"status"
:
"ok"
}
)
c
.
J
sonOK
(
"Account created"
)
}
}
pkg/models/account.go
View file @
607b0c0c
...
@@ -6,7 +6,6 @@ import (
...
@@ -6,7 +6,6 @@ import (
)
)
var
(
var
(
SaveAccount
func
(
account
*
Account
)
error
GetAccountByLogin
func
(
emailOrName
string
)
(
*
Account
,
error
)
GetAccountByLogin
func
(
emailOrName
string
)
(
*
Account
,
error
)
GetAccount
func
(
accountId
int64
)
(
*
Account
,
error
)
GetAccount
func
(
accountId
int64
)
(
*
Account
,
error
)
)
)
...
@@ -55,6 +54,16 @@ type AccountDTO struct {
...
@@ -55,6 +54,16 @@ type AccountDTO struct {
Collaborators
[]
*
CollaboratorDTO
`json:"collaborators"`
Collaborators
[]
*
CollaboratorDTO
`json:"collaborators"`
}
}
type
CreateAccountCommand
struct
{
Email
string
`json:"email" binding:"required"`
Login
string
`json:"login"`
Password
string
`json:"password" binding:"required"`
Name
string
`json:"name"`
Company
string
`json:"company"`
Result
Account
`json:"-"`
}
// returns a view projection
// returns a view projection
type
GetAccountInfoQuery
struct
{
type
GetAccountInfoQuery
struct
{
Id
int64
Id
int64
...
...
pkg/stores/sqlstore/accounts.go
View file @
607b0c0c
...
@@ -13,6 +13,24 @@ func init() {
...
@@ -13,6 +13,24 @@ func init() {
bus
.
AddHandler
(
"sql"
,
GetAccountInfo
)
bus
.
AddHandler
(
"sql"
,
GetAccountInfo
)
bus
.
AddHandler
(
"sql"
,
AddCollaborator
)
bus
.
AddHandler
(
"sql"
,
AddCollaborator
)
bus
.
AddHandler
(
"sql"
,
GetOtherAccounts
)
bus
.
AddHandler
(
"sql"
,
GetOtherAccounts
)
bus
.
AddHandler
(
"sql"
,
CreateAccount
)
}
func
CreateAccount
(
cmd
*
m
.
CreateAccountCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
account
:=
m
.
Account
{
Email
:
cmd
.
Email
,
Login
:
cmd
.
Login
,
Password
:
cmd
.
Password
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
_
,
err
:=
sess
.
Insert
(
&
account
)
cmd
.
Result
=
account
return
err
})
}
}
func
GetAccountInfo
(
query
*
m
.
GetAccountInfoQuery
)
error
{
func
GetAccountInfo
(
query
*
m
.
GetAccountInfoQuery
)
error
{
...
@@ -55,32 +73,6 @@ func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
...
@@ -55,32 +73,6 @@ func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
})
})
}
}
func
SaveAccount
(
account
*
m
.
Account
)
error
{
var
err
error
sess
:=
x
.
NewSession
()
defer
sess
.
Close
()
if
err
=
sess
.
Begin
();
err
!=
nil
{
return
err
}
if
account
.
Id
==
0
{
_
,
err
=
sess
.
Insert
(
account
)
}
else
{
_
,
err
=
sess
.
Id
(
account
.
Id
)
.
Update
(
account
)
}
if
err
!=
nil
{
sess
.
Rollback
()
return
err
}
else
if
err
=
sess
.
Commit
();
err
!=
nil
{
return
err
}
return
nil
}
func
GetAccount
(
id
int64
)
(
*
m
.
Account
,
error
)
{
func
GetAccount
(
id
int64
)
(
*
m
.
Account
,
error
)
{
var
err
error
var
err
error
...
...
pkg/stores/sqlstore/accounts_test.go
View file @
607b0c0c
...
@@ -14,27 +14,22 @@ func TestAccountDataAccess(t *testing.T) {
...
@@ -14,27 +14,22 @@ func TestAccountDataAccess(t *testing.T) {
InitTestDB
(
t
)
InitTestDB
(
t
)
Convey
(
"Given two saved accounts"
,
func
()
{
Convey
(
"Given two saved accounts"
,
func
()
{
ac1
:=
m
.
Account
{
ac1cmd
:=
m
.
CreateAccountCommand
{
Login
:
"ac1"
,
Email
:
"ac1@test.com"
}
Login
:
"ac1"
,
ac2cmd
:=
m
.
CreateAccountCommand
{
Login
:
"ac2"
,
Email
:
"ac2@test.com"
}
Email
:
"ac1@test.com"
,
Name
:
"ac1_name"
,
err
:=
CreateAccount
(
&
ac1cmd
)
}
err
=
CreateAccount
(
&
ac2cmd
)
ac2
:=
m
.
Account
{
Login
:
"ac2"
,
Email
:
"ac2@test.com"
,
Name
:
"ac2_name"
,
}
err
:=
SaveAccount
(
&
ac1
)
err
=
SaveAccount
(
&
ac2
)
So
(
err
,
ShouldBeNil
)
So
(
err
,
ShouldBeNil
)
ac1
:=
ac1cmd
.
Result
ac2
:=
ac2cmd
.
Result
Convey
(
"Should be able to read account info projection"
,
func
()
{
Convey
(
"Should be able to read account info projection"
,
func
()
{
query
:=
m
.
GetAccountInfoQuery
{
Id
:
ac1
.
Id
}
query
:=
m
.
GetAccountInfoQuery
{
Id
:
ac1
.
Id
}
err
=
GetAccountInfo
(
&
query
)
err
=
GetAccountInfo
(
&
query
)
So
(
err
,
ShouldBeNil
)
So
(
err
,
ShouldBeNil
)
So
(
query
.
Result
.
Name
,
ShouldEqual
,
"ac1_name
"
)
So
(
query
.
Result
.
Email
,
ShouldEqual
,
"ac1@test.com
"
)
})
})
Convey
(
"Can add collaborator"
,
func
()
{
Convey
(
"Can add collaborator"
,
func
()
{
...
...
pkg/stores/sqlstore/sqlstore.go
View file @
607b0c0c
...
@@ -35,7 +35,6 @@ func init() {
...
@@ -35,7 +35,6 @@ func init() {
}
}
func
Init
()
{
func
Init
()
{
m
.
SaveAccount
=
SaveAccount
m
.
GetAccount
=
GetAccount
m
.
GetAccount
=
GetAccount
m
.
GetAccountByLogin
=
GetAccountByLogin
m
.
GetAccountByLogin
=
GetAccountByLogin
m
.
GetDashboard
=
GetDashboard
m
.
GetDashboard
=
GetDashboard
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment