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
36c46112
Commit
36c46112
authored
Dec 19, 2014
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring data access to command query model, and adding tests for sql code
parent
e5811e29
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
107 additions
and
34 deletions
+107
-34
pkg/api/api_account.go
+11
-21
pkg/models/account.go
+20
-0
pkg/stores/sqlstore/accounts.go
+44
-13
pkg/stores/sqlstore/accounts_test.go
+32
-0
No files found.
pkg/api/api_account.go
View file @
36c46112
...
@@ -2,32 +2,22 @@ package api
...
@@ -2,32 +2,22 @@ package api
import
(
import
(
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"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"
"github.com/torkelo/grafana-pro/pkg/utils"
)
)
func
GetAccount
(
c
*
middleware
.
Context
)
{
func
GetAccount
(
c
*
middleware
.
Context
)
{
model
:=
dtos
.
AccountInfo
{
query
:=
m
.
GetAccountInfoQuery
{
Id
:
c
.
UserAccount
.
Id
}
Name
:
c
.
UserAccount
.
Name
,
err
:=
bus
.
Dispatch
(
&
query
)
Email
:
c
.
UserAccount
.
Email
,
}
collaborators
,
err
:=
models
.
GetCollaboratorsForAccount
(
c
.
UserAccount
.
Id
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to fetch collaboratos"
,
err
)
c
.
JsonApiErr
(
500
,
"Failed to fetch collaboratos"
,
err
)
return
return
}
}
for
_
,
collaborator
:=
range
collaborators
{
c
.
JSON
(
200
,
query
.
Result
)
model
.
Collaborators
=
append
(
model
.
Collaborators
,
&
dtos
.
Collaborator
{
AccountId
:
collaborator
.
AccountId
,
Role
:
collaborator
.
Role
,
Email
:
collaborator
.
Email
,
})
}
c
.
JSON
(
200
,
model
)
}
}
func
AddCollaborator
(
c
*
middleware
.
Context
)
{
func
AddCollaborator
(
c
*
middleware
.
Context
)
{
...
@@ -38,7 +28,7 @@ func AddCollaborator(c *middleware.Context) {
...
@@ -38,7 +28,7 @@ func AddCollaborator(c *middleware.Context) {
return
return
}
}
accountToAdd
,
err
:=
m
odels
.
GetAccountByLogin
(
model
.
Email
)
accountToAdd
,
err
:=
m
.
GetAccountByLogin
(
model
.
Email
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JsonApiErr
(
404
,
"Collaborator not found"
,
nil
)
c
.
JsonApiErr
(
404
,
"Collaborator not found"
,
nil
)
return
return
...
@@ -49,9 +39,9 @@ func AddCollaborator(c *middleware.Context) {
...
@@ -49,9 +39,9 @@ func AddCollaborator(c *middleware.Context) {
return
return
}
}
var
collaborator
=
m
odels
.
NewCollaborator
(
accountToAdd
.
Id
,
c
.
UserAccount
.
Id
,
models
.
ROLE_READ_WRITE
)
var
collaborator
=
m
.
NewCollaborator
(
accountToAdd
.
Id
,
c
.
UserAccount
.
Id
,
m
.
ROLE_READ_WRITE
)
err
=
m
odels
.
AddCollaborator
(
collaborator
)
err
=
m
.
AddCollaborator
(
collaborator
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Could not add collaborator"
,
err
)
c
.
JsonApiErr
(
500
,
"Could not add collaborator"
,
err
)
return
return
...
@@ -62,7 +52,7 @@ func AddCollaborator(c *middleware.Context) {
...
@@ -62,7 +52,7 @@ func AddCollaborator(c *middleware.Context) {
func
GetOtherAccounts
(
c
*
middleware
.
Context
)
{
func
GetOtherAccounts
(
c
*
middleware
.
Context
)
{
otherAccounts
,
err
:=
m
odels
.
GetOtherAccountsFor
(
c
.
UserAccount
.
Id
)
otherAccounts
,
err
:=
m
.
GetOtherAccountsFor
(
c
.
UserAccount
.
Id
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JSON
(
500
,
utils
.
DynMap
{
"message"
:
err
.
Error
()})
c
.
JSON
(
500
,
utils
.
DynMap
{
"message"
:
err
.
Error
()})
return
return
...
@@ -92,7 +82,7 @@ func SetUsingAccount(c *middleware.Context) {
...
@@ -92,7 +82,7 @@ func SetUsingAccount(c *middleware.Context) {
usingAccountId
:=
c
.
ParamsInt64
(
":id"
)
usingAccountId
:=
c
.
ParamsInt64
(
":id"
)
account
:=
c
.
UserAccount
account
:=
c
.
UserAccount
otherAccounts
,
err
:=
m
odels
.
GetOtherAccountsFor
(
c
.
UserAccount
.
Id
)
otherAccounts
,
err
:=
m
.
GetOtherAccountsFor
(
c
.
UserAccount
.
Id
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JSON
(
500
,
utils
.
DynMap
{
"message"
:
err
.
Error
()})
c
.
JSON
(
500
,
utils
.
DynMap
{
"message"
:
err
.
Error
()})
...
@@ -113,7 +103,7 @@ func SetUsingAccount(c *middleware.Context) {
...
@@ -113,7 +103,7 @@ func SetUsingAccount(c *middleware.Context) {
}
}
account
.
UsingAccountId
=
usingAccountId
account
.
UsingAccountId
=
usingAccountId
err
=
m
odels
.
SaveAccount
(
account
)
err
=
m
.
SaveAccount
(
account
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JSON
(
500
,
utils
.
DynMap
{
"message"
:
err
.
Error
()})
c
.
JSON
(
500
,
utils
.
DynMap
{
"message"
:
err
.
Error
()})
return
return
...
...
pkg/models/account.go
View file @
36c46112
...
@@ -42,3 +42,23 @@ type Account struct {
...
@@ -42,3 +42,23 @@ type Account struct {
Created
time
.
Time
Created
time
.
Time
Updated
time
.
Time
Updated
time
.
Time
}
}
// api projection model
type
CollaboratorDTO
struct
{
AccountId
int64
`json:"accountId"`
Email
string
`json:"email"`
Role
string
`json:"role"`
}
// api view projection
type
AccountDTO
struct
{
Email
string
`json:"email"`
Name
string
`json:"name"`
Collaborators
[]
*
CollaboratorDTO
`json:"collaborators"`
}
// returns a view projection
type
GetAccountInfoQuery
struct
{
Id
int64
Result
AccountDTO
}
pkg/stores/sqlstore/
sqlstore_
accounts.go
→
pkg/stores/sqlstore/accounts.go
View file @
36c46112
package
sqlstore
package
sqlstore
import
"github.com/torkelo/grafana-pro/pkg/models"
import
(
"github.com/torkelo/grafana-pro/pkg/bus"
m
"github.com/torkelo/grafana-pro/pkg/models"
)
func
SaveAccount
(
account
*
models
.
Account
)
error
{
func
init
()
{
bus
.
AddHandler
(
"sql"
,
GetAccountInfo
)
}
func
GetAccountInfo
(
query
*
m
.
GetAccountInfoQuery
)
error
{
var
account
m
.
Account
has
,
err
:=
x
.
Id
(
query
.
Id
)
.
Get
(
&
account
)
if
err
!=
nil
{
return
err
}
else
if
has
==
false
{
return
m
.
ErrAccountNotFound
}
query
.
Result
=
m
.
AccountDTO
{
Name
:
account
.
Name
,
Email
:
account
.
Email
,
Collaborators
:
make
([]
*
m
.
CollaboratorDTO
,
0
),
}
sess
:=
x
.
Table
(
"collaborator"
)
sess
.
Join
(
"INNER"
,
"account"
,
"account.id=collaborator.account_Id"
)
sess
.
Where
(
"collaborator.for_account_id=?"
,
query
.
Id
)
err
=
sess
.
Find
(
&
query
.
Result
.
Collaborators
)
return
err
}
func
SaveAccount
(
account
*
m
.
Account
)
error
{
var
err
error
var
err
error
sess
:=
x
.
NewSession
()
sess
:=
x
.
NewSession
()
...
@@ -28,16 +59,16 @@ func SaveAccount(account *models.Account) error {
...
@@ -28,16 +59,16 @@ func SaveAccount(account *models.Account) error {
return
nil
return
nil
}
}
func
GetAccount
(
id
int64
)
(
*
m
odels
.
Account
,
error
)
{
func
GetAccount
(
id
int64
)
(
*
m
.
Account
,
error
)
{
var
err
error
var
err
error
var
account
m
odels
.
Account
var
account
m
.
Account
has
,
err
:=
x
.
Id
(
id
)
.
Get
(
&
account
)
has
,
err
:=
x
.
Id
(
id
)
.
Get
(
&
account
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
else
if
has
==
false
{
}
else
if
has
==
false
{
return
nil
,
m
odels
.
ErrAccountNotFound
return
nil
,
m
.
ErrAccountNotFound
}
}
if
account
.
UsingAccountId
==
0
{
if
account
.
UsingAccountId
==
0
{
...
@@ -47,23 +78,23 @@ func GetAccount(id int64) (*models.Account, error) {
...
@@ -47,23 +78,23 @@ func GetAccount(id int64) (*models.Account, error) {
return
&
account
,
nil
return
&
account
,
nil
}
}
func
GetAccountByLogin
(
emailOrLogin
string
)
(
*
m
odels
.
Account
,
error
)
{
func
GetAccountByLogin
(
emailOrLogin
string
)
(
*
m
.
Account
,
error
)
{
var
err
error
var
err
error
account
:=
&
m
odels
.
Account
{
Login
:
emailOrLogin
}
account
:=
&
m
.
Account
{
Login
:
emailOrLogin
}
has
,
err
:=
x
.
Get
(
account
)
has
,
err
:=
x
.
Get
(
account
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
else
if
has
==
false
{
}
else
if
has
==
false
{
return
nil
,
m
odels
.
ErrAccountNotFound
return
nil
,
m
.
ErrAccountNotFound
}
}
return
account
,
nil
return
account
,
nil
}
}
func
GetCollaboratorsForAccount
(
accountId
int64
)
([]
*
m
odels
.
CollaboratorInfo
,
error
)
{
func
GetCollaboratorsForAccount
(
accountId
int64
)
([]
*
m
.
CollaboratorInfo
,
error
)
{
collaborators
:=
make
([]
*
m
odels
.
CollaboratorInfo
,
0
)
collaborators
:=
make
([]
*
m
.
CollaboratorInfo
,
0
)
sess
:=
x
.
Table
(
"collaborator"
)
sess
:=
x
.
Table
(
"collaborator"
)
sess
.
Join
(
"INNER"
,
"account"
,
"account.id=collaborator.account_Id"
)
sess
.
Join
(
"INNER"
,
"account"
,
"account.id=collaborator.account_Id"
)
...
@@ -73,7 +104,7 @@ func GetCollaboratorsForAccount(accountId int64) ([]*models.CollaboratorInfo, er
...
@@ -73,7 +104,7 @@ func GetCollaboratorsForAccount(accountId int64) ([]*models.CollaboratorInfo, er
return
collaborators
,
err
return
collaborators
,
err
}
}
func
AddCollaborator
(
collaborator
*
m
odels
.
Collaborator
)
error
{
func
AddCollaborator
(
collaborator
*
m
.
Collaborator
)
error
{
var
err
error
var
err
error
sess
:=
x
.
NewSession
()
sess
:=
x
.
NewSession
()
...
@@ -93,8 +124,8 @@ func AddCollaborator(collaborator *models.Collaborator) error {
...
@@ -93,8 +124,8 @@ func AddCollaborator(collaborator *models.Collaborator) error {
return
nil
return
nil
}
}
func
GetOtherAccountsFor
(
accountId
int64
)
([]
*
m
odels
.
OtherAccount
,
error
)
{
func
GetOtherAccountsFor
(
accountId
int64
)
([]
*
m
.
OtherAccount
,
error
)
{
collaborators
:=
make
([]
*
m
odels
.
OtherAccount
,
0
)
collaborators
:=
make
([]
*
m
.
OtherAccount
,
0
)
sess
:=
x
.
Table
(
"collaborator"
)
sess
:=
x
.
Table
(
"collaborator"
)
sess
.
Join
(
"INNER"
,
"account"
,
"collaborator.for_account_id=account.id"
)
sess
.
Join
(
"INNER"
,
"account"
,
"collaborator.for_account_id=account.id"
)
sess
.
Where
(
"account_id=?"
,
accountId
)
sess
.
Where
(
"account_id=?"
,
accountId
)
...
...
pkg/stores/sqlstore/accounts_test.go
0 → 100644
View file @
36c46112
package
sqlstore
import
(
"testing"
.
"github.com/smartystreets/goconvey/convey"
m
"github.com/torkelo/grafana-pro/pkg/models"
)
func
TestAccountDataAccess
(
t
*
testing
.
T
)
{
Convey
(
"Testing Account DB Access"
,
t
,
func
()
{
InitTestDB
(
t
)
Convey
(
"Can save account"
,
func
()
{
account
:=
m
.
Account
{
Login
:
"login"
,
Email
:
"login@test.com"
,
Name
:
"name"
,
}
err
:=
SaveAccount
(
&
account
)
query
:=
m
.
GetAccountInfoQuery
{
Id
:
account
.
Id
}
err
=
GetAccountInfo
(
&
query
)
So
(
err
,
ShouldBeNil
)
So
(
query
.
Result
.
Name
,
ShouldEqual
,
"name"
)
})
})
}
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