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
import
(
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware"
m
"github.com/torkelo/grafana-pro/pkg/models"
...
...
@@ -53,63 +52,60 @@ func AddCollaborator(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
{
c
.
JSON
(
500
,
utils
.
DynMap
{
"message"
:
err
.
Error
()})
return
}
var
result
[]
*
dtos
.
OtherAccount
result
=
append
(
result
,
&
dtos
.
OtherAccount
{
Id
:
c
.
UserAccount
.
Id
,
Role
:
"owner"
,
IsUsing
:
c
.
UserAccount
.
Id
==
c
.
UserAccount
.
UsingAccountId
,
Name
:
c
.
UserAccount
.
Email
,
result
:=
append
(
query
.
Result
,
&
m
.
OtherAccountDTO
{
Id
:
c
.
UserAccount
.
Id
,
Role
:
"owner"
,
Email
:
c
.
UserAccount
.
Email
,
})
for
_
,
other
:=
range
otherAccounts
{
result
=
append
(
result
,
&
dtos
.
OtherAccount
{
Id
:
other
.
Id
,
Role
:
other
.
Role
,
Name
:
other
.
Email
,
IsUsing
:
other
.
Id
==
c
.
UserAccount
.
UsingAccountId
,
})
for
_
,
ac
:=
range
result
{
if
ac
.
Id
==
c
.
UserAccount
.
UsingAccountId
{
ac
.
IsUsing
=
true
break
}
}
c
.
JSON
(
200
,
result
)
}
func
SetUsingAccount
(
c
*
middleware
.
Context
)
{
usingAccountId
:=
c
.
ParamsInt64
(
":id"
)
account
:=
c
.
UserAccount
otherAccounts
,
err
:=
m
.
GetOtherAccountsFor
(
c
.
UserAccount
.
Id
)
if
err
!=
nil
{
c
.
JSON
(
500
,
utils
.
DynMap
{
"message"
:
err
.
Error
()})
return
}
// validate that the account id in the list
valid
:=
false
for
_
,
other
:=
range
otherAccounts
{
if
other
.
Id
==
usingAccountId
{
valid
=
true
}
}
if
!
valid
{
c
.
Status
(
401
)
return
}
account
.
UsingAccountId
=
usingAccountId
err
=
m
.
SaveAccount
(
account
)
if
err
!=
nil
{
c
.
JSON
(
500
,
utils
.
DynMap
{
"message"
:
err
.
Error
()})
return
}
c
.
Status
(
204
)
//
usingAccountId := c.ParamsInt64(":id")
//
//
account := c.UserAccount
//
otherAccounts, err := m.GetOtherAccountsFor(c.UserAccount.Id)
//
//
if err != nil {
//
c.JSON(500, utils.DynMap{"message": err.Error()})
//
return
//
}
//
//
//
validate that the account id in the list
//
valid := false
//
for _, other := range otherAccounts {
//
if other.Id == usingAccountId {
//
valid = true
//
}
//
}
//
//
if !valid {
//
c.Status(401)
//
return
//
}
//
//
account.UsingAccountId = usingAccountId
//
err = m.SaveAccount(account)
//
if err != nil {
//
c.JSON(500, utils.DynMap{"message": err.Error()})
//
return
//
}
//
//
c.Status(204)
}
pkg/models/account.go
View file @
d5a59ac6
...
...
@@ -6,10 +6,9 @@ import (
)
var
(
SaveAccount
func
(
account
*
Account
)
error
GetAccountByLogin
func
(
emailOrName
string
)
(
*
Account
,
error
)
GetAccount
func
(
accountId
int64
)
(
*
Account
,
error
)
GetOtherAccountsFor
func
(
accountId
int64
)
([]
*
OtherAccount
,
error
)
SaveAccount
func
(
account
*
Account
)
error
GetAccountByLogin
func
(
emailOrName
string
)
(
*
Account
,
error
)
GetAccount
func
(
accountId
int64
)
(
*
Account
,
error
)
)
// Typed errors
...
...
@@ -17,13 +16,6 @@ var (
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
{
Id
int64
Login
string
`xorm:"UNIQUE NOT NULL"`
...
...
@@ -41,6 +33,14 @@ type Account struct {
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
type
CollaboratorDTO
struct
{
AccountId
int64
`json:"accountId"`
...
...
@@ -60,3 +60,9 @@ type GetAccountInfoQuery struct {
Id
int64
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 (
func
init
()
{
bus
.
AddHandler
(
"sql"
,
GetAccountInfo
)
bus
.
AddHandler
(
"sql"
,
AddCollaborator
)
bus
.
AddHandler
(
"sql"
,
GetOtherAccounts
)
}
func
GetAccountInfo
(
query
*
m
.
GetAccountInfoQuery
)
error
{
...
...
@@ -114,12 +115,12 @@ func GetAccountByLogin(emailOrLogin string) (*m.Account, error) {
return
account
,
nil
}
func
GetOtherAccounts
For
(
accountId
int64
)
([]
*
m
.
OtherAccount
,
error
)
{
collaborators
:=
make
([]
*
m
.
OtherAccount
,
0
)
func
GetOtherAccounts
(
query
*
m
.
GetOtherAccountsQuery
)
error
{
query
.
Result
=
make
([]
*
m
.
OtherAccountDTO
,
0
)
sess
:=
x
.
Table
(
"collaborator"
)
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"
)
err
:=
sess
.
Find
(
&
collaborators
)
return
collaborators
,
err
err
:=
sess
.
Find
(
&
query
.
Result
)
return
err
}
pkg/stores/sqlstore/accounts_test.go
View file @
d5a59ac6
...
...
@@ -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
]
.
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() {
m
.
SaveAccount
=
SaveAccount
m
.
GetAccount
=
GetAccount
m
.
GetAccountByLogin
=
GetAccountByLogin
m
.
GetOtherAccountsFor
=
GetOtherAccountsFor
m
.
GetDashboard
=
GetDashboard
m
.
SaveDashboard
=
SaveDashboard
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