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
d5a59ac6
Commit
d5a59ac6
authored
Dec 19, 2014
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More migration to command/query and sql tests, looking good
parent
ccba9554
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
73 additions
and
63 deletions
+73
-63
pkg/api/api_account.go
+42
-46
pkg/models/account.go
+17
-11
pkg/stores/sqlstore/accounts.go
+6
-5
pkg/stores/sqlstore/accounts_test.go
+8
-0
pkg/stores/sqlstore/sqlstore.go
+0
-1
No files found.
pkg/api/api_account.go
View file @
d5a59ac6
package
api
package
api
import
(
import
(
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware"
"github.com/torkelo/grafana-pro/pkg/middleware"
m
"github.com/torkelo/grafana-pro/pkg/models"
m
"github.com/torkelo/grafana-pro/pkg/models"
...
@@ -53,63 +52,60 @@ func AddCollaborator(c *middleware.Context) {
...
@@ -53,63 +52,60 @@ func AddCollaborator(c *middleware.Context) {
}
}
func
GetOtherAccounts
(
c
*
middleware
.
Context
)
{
func
GetOtherAccounts
(
c
*
middleware
.
Context
)
{
query
:=
m
.
GetOtherAccountsQuery
{
AccountId
:
c
.
UserAccount
.
Id
}
err
:=
bus
.
Dispatch
(
&
query
)
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
}
}
var
result
[]
*
dtos
.
OtherAccount
result
:=
append
(
query
.
Result
,
&
m
.
OtherAccountDTO
{
result
=
append
(
result
,
&
dtos
.
OtherAccount
{
Id
:
c
.
UserAccount
.
Id
,
Id
:
c
.
UserAccount
.
Id
,
Role
:
"owner"
,
Role
:
"owner"
,
Email
:
c
.
UserAccount
.
Email
,
IsUsing
:
c
.
UserAccount
.
Id
==
c
.
UserAccount
.
UsingAccountId
,
Name
:
c
.
UserAccount
.
Email
,
})
})
for
_
,
other
:=
range
otherAccounts
{
for
_
,
ac
:=
range
result
{
result
=
append
(
result
,
&
dtos
.
OtherAccount
{
if
ac
.
Id
==
c
.
UserAccount
.
UsingAccountId
{
Id
:
other
.
Id
,
ac
.
IsUsing
=
true
Role
:
other
.
Role
,
break
Name
:
other
.
Email
,
}
IsUsing
:
other
.
Id
==
c
.
UserAccount
.
UsingAccountId
,
})
}
}
c
.
JSON
(
200
,
result
)
c
.
JSON
(
200
,
result
)
}
}
func
SetUsingAccount
(
c
*
middleware
.
Context
)
{
func
SetUsingAccount
(
c
*
middleware
.
Context
)
{
usingAccountId
:=
c
.
ParamsInt64
(
":id"
)
//
usingAccountId := c.ParamsInt64(":id")
//
account
:=
c
.
UserAccount
//
account := c.UserAccount
otherAccounts
,
err
:=
m
.
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
}
//
}
//
// validate that the account id in the list
//
//
validate that the account id in the list
valid
:=
false
//
valid := false
for
_
,
other
:=
range
otherAccounts
{
//
for _, other := range otherAccounts {
if
other
.
Id
==
usingAccountId
{
//
if other.Id == usingAccountId {
valid
=
true
//
valid = true
}
//
}
}
//
}
//
if
!
valid
{
//
if !valid {
c
.
Status
(
401
)
//
c.Status(401)
return
//
return
}
//
}
//
account
.
UsingAccountId
=
usingAccountId
//
account.UsingAccountId = usingAccountId
err
=
m
.
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
}
//
}
//
c
.
Status
(
204
)
//
c.Status(204)
}
}
pkg/models/account.go
View file @
d5a59ac6
...
@@ -6,10 +6,9 @@ import (
...
@@ -6,10 +6,9 @@ import (
)
)
var
(
var
(
SaveAccount
func
(
account
*
Account
)
error
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
)
GetOtherAccountsFor
func
(
accountId
int64
)
([]
*
OtherAccount
,
error
)
)
)
// Typed errors
// Typed errors
...
@@ -17,13 +16,6 @@ var (
...
@@ -17,13 +16,6 @@ var (
ErrAccountNotFound
=
errors
.
New
(
"Account not found"
)
ErrAccountNotFound
=
errors
.
New
(
"Account not found"
)
)
)
// Projection from User -> other account given access to
type
OtherAccount
struct
{
Id
int64
Email
string
Role
string
}
type
Account
struct
{
type
Account
struct
{
Id
int64
Id
int64
Login
string
`xorm:"UNIQUE NOT NULL"`
Login
string
`xorm:"UNIQUE NOT NULL"`
...
@@ -41,6 +33,14 @@ type Account struct {
...
@@ -41,6 +33,14 @@ type Account struct {
Updated
time
.
Time
Updated
time
.
Time
}
}
// api projection
type
OtherAccountDTO
struct
{
Id
int64
`json:"id"`
Email
string
`json:"email"`
Role
string
`json:"role"`
IsUsing
bool
`json:"isUsing"`
}
// api projection model
// api projection model
type
CollaboratorDTO
struct
{
type
CollaboratorDTO
struct
{
AccountId
int64
`json:"accountId"`
AccountId
int64
`json:"accountId"`
...
@@ -60,3 +60,9 @@ type GetAccountInfoQuery struct {
...
@@ -60,3 +60,9 @@ type GetAccountInfoQuery struct {
Id
int64
Id
int64
Result
AccountDTO
Result
AccountDTO
}
}
// returns a view projection
type
GetOtherAccountsQuery
struct
{
AccountId
int64
Result
[]
*
OtherAccountDTO
}
pkg/stores/sqlstore/accounts.go
View file @
d5a59ac6
...
@@ -12,6 +12,7 @@ import (
...
@@ -12,6 +12,7 @@ import (
func
init
()
{
func
init
()
{
bus
.
AddHandler
(
"sql"
,
GetAccountInfo
)
bus
.
AddHandler
(
"sql"
,
GetAccountInfo
)
bus
.
AddHandler
(
"sql"
,
AddCollaborator
)
bus
.
AddHandler
(
"sql"
,
AddCollaborator
)
bus
.
AddHandler
(
"sql"
,
GetOtherAccounts
)
}
}
func
GetAccountInfo
(
query
*
m
.
GetAccountInfoQuery
)
error
{
func
GetAccountInfo
(
query
*
m
.
GetAccountInfoQuery
)
error
{
...
@@ -114,12 +115,12 @@ func GetAccountByLogin(emailOrLogin string) (*m.Account, error) {
...
@@ -114,12 +115,12 @@ func GetAccountByLogin(emailOrLogin string) (*m.Account, error) {
return
account
,
nil
return
account
,
nil
}
}
func
GetOtherAccounts
For
(
accountId
int64
)
([]
*
m
.
OtherAccount
,
error
)
{
func
GetOtherAccounts
(
query
*
m
.
GetOtherAccountsQuery
)
error
{
collaborators
:=
make
([]
*
m
.
OtherAccount
,
0
)
query
.
Result
=
make
([]
*
m
.
OtherAccountDTO
,
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=?"
,
a
ccountId
)
sess
.
Where
(
"account_id=?"
,
query
.
A
ccountId
)
sess
.
Cols
(
"collaborator.id"
,
"collaborator.role"
,
"account.email"
)
sess
.
Cols
(
"collaborator.id"
,
"collaborator.role"
,
"account.email"
)
err
:=
sess
.
Find
(
&
collaborators
)
err
:=
sess
.
Find
(
&
query
.
Result
)
return
collaborators
,
err
return
err
}
}
pkg/stores/sqlstore/accounts_test.go
View file @
d5a59ac6
...
@@ -58,6 +58,14 @@ func TestAccountDataAccess(t *testing.T) {
...
@@ -58,6 +58,14 @@ func TestAccountDataAccess(t *testing.T) {
So
(
query
.
Result
.
Collaborators
[
0
]
.
Role
,
ShouldEqual
,
m
.
ROLE_READ_WRITE
)
So
(
query
.
Result
.
Collaborators
[
0
]
.
Role
,
ShouldEqual
,
m
.
ROLE_READ_WRITE
)
So
(
query
.
Result
.
Collaborators
[
0
]
.
Email
,
ShouldEqual
,
"ac2@test.com"
)
So
(
query
.
Result
.
Collaborators
[
0
]
.
Email
,
ShouldEqual
,
"ac2@test.com"
)
})
})
Convey
(
"Can get other accounts"
,
func
()
{
query
:=
m
.
GetOtherAccountsQuery
{
AccountId
:
ac2
.
Id
}
err
:=
GetOtherAccounts
(
&
query
)
So
(
err
,
ShouldBeNil
)
So
(
query
.
Result
[
0
]
.
Email
,
ShouldEqual
,
"ac1@test.com"
)
})
})
})
})
})
})
})
...
...
pkg/stores/sqlstore/sqlstore.go
View file @
d5a59ac6
...
@@ -38,7 +38,6 @@ func Init() {
...
@@ -38,7 +38,6 @@ func Init() {
m
.
SaveAccount
=
SaveAccount
m
.
SaveAccount
=
SaveAccount
m
.
GetAccount
=
GetAccount
m
.
GetAccount
=
GetAccount
m
.
GetAccountByLogin
=
GetAccountByLogin
m
.
GetAccountByLogin
=
GetAccountByLogin
m
.
GetOtherAccountsFor
=
GetOtherAccountsFor
m
.
GetDashboard
=
GetDashboard
m
.
GetDashboard
=
GetDashboard
m
.
SaveDashboard
=
SaveDashboard
m
.
SaveDashboard
=
SaveDashboard
m
.
SearchQuery
=
SearchQuery
m
.
SearchQuery
=
SearchQuery
...
...
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