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
04bbdbad
Commit
04bbdbad
authored
Jan 15, 2015
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Worked on account update, moved collaborats to its own api url and files
parent
804bff55
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
178 additions
and
82 deletions
+178
-82
grafana
+1
-1
pkg/api/account.go
+5
-34
pkg/api/api.go
+2
-0
pkg/api/collaborators.go
+64
-0
pkg/models/account.go
+9
-7
pkg/models/collaborator.go
+29
-8
pkg/services/sqlstore/accounts.go
+17
-32
pkg/services/sqlstore/collaborators.go
+51
-0
No files found.
grafana
@
9d1dacb8
Subproject commit
5b93e09714dbee6c1c181daf0182704334d8c6be
Subproject commit
9d1dacb8d417cac2cc66797e5dae6deba36c3080
pkg/api/account.go
View file @
04bbdbad
...
...
@@ -18,51 +18,22 @@ func GetAccount(c *middleware.Context) {
c
.
JSON
(
200
,
query
.
Result
)
}
func
AddCollaborator
(
c
*
middleware
.
Context
)
{
var
cmd
m
.
AddCollaboratorCommand
func
UpdateAccount
(
c
*
middleware
.
Context
)
{
cmd
:=
m
.
UpdateAccountCommand
{}
if
!
c
.
JsonBody
(
&
cmd
)
{
c
.
JsonApiErr
(
400
,
"Invalid request"
,
nil
)
return
}
userQuery
:=
m
.
GetAccountByLoginQuery
{
Login
:
cmd
.
Email
}
err
:=
bus
.
Dispatch
(
&
userQuery
)
if
err
!=
nil
{
c
.
JsonApiErr
(
404
,
"Collaborator not found"
,
nil
)
return
}
accountToAdd
:=
userQuery
.
Result
if
accountToAdd
.
Id
==
c
.
UserAccount
.
Id
{
c
.
JsonApiErr
(
400
,
"Cannot add yourself as collaborator"
,
nil
)
return
}
cmd
.
AccountId
=
c
.
UserAccount
.
Id
cmd
.
CollaboratorId
=
accountToAdd
.
Id
cmd
.
Role
=
m
.
ROLE_READ_WRITE
err
=
bus
.
Dispatch
(
&
cmd
)
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Could not add collaborator"
,
err
)
return
}
c
.
JsonOK
(
"Collaborator added"
)
}
func
RemoveCollaborator
(
c
*
middleware
.
Context
)
{
collaboratorId
:=
c
.
ParamsInt64
(
":id"
)
cmd
:=
m
.
RemoveCollaboratorCommand
{
AccountId
:
c
.
UserAccount
.
Id
,
CollaboratorId
:
collaboratorId
}
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to remove collaborator"
,
err
)
c
.
JsonApiErr
(
400
,
"Failed to update account"
,
nil
)
return
}
c
.
JsonOK
(
"
Collaborator remov
ed"
)
c
.
JsonOK
(
"
Account updat
ed"
)
}
func
GetOtherAccounts
(
c
*
middleware
.
Context
)
{
...
...
pkg/api/api.go
View file @
04bbdbad
...
...
@@ -34,7 +34,9 @@ func Register(m *macaron.Macaron) {
// account
m
.
Group
(
"/account"
,
func
()
{
m
.
Get
(
"/"
,
GetAccount
)
m
.
Post
(
"/"
,
UpdateAccount
)
m
.
Put
(
"/collaborators"
,
AddCollaborator
)
m
.
Get
(
"/collaborators"
,
GetCollaborators
)
m
.
Delete
(
"/collaborators/:id"
,
RemoveCollaborator
)
m
.
Post
(
"/using/:id"
,
SetUsingAccount
)
m
.
Get
(
"/others"
,
GetOtherAccounts
)
...
...
pkg/api/collaborators.go
0 → 100644
View file @
04bbdbad
package
api
import
(
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware"
m
"github.com/torkelo/grafana-pro/pkg/models"
)
func
AddCollaborator
(
c
*
middleware
.
Context
)
{
var
cmd
m
.
AddCollaboratorCommand
if
!
c
.
JsonBody
(
&
cmd
)
{
c
.
JsonApiErr
(
400
,
"Invalid request"
,
nil
)
return
}
userQuery
:=
m
.
GetAccountByLoginQuery
{
Login
:
cmd
.
Email
}
err
:=
bus
.
Dispatch
(
&
userQuery
)
if
err
!=
nil
{
c
.
JsonApiErr
(
404
,
"Collaborator not found"
,
nil
)
return
}
accountToAdd
:=
userQuery
.
Result
if
accountToAdd
.
Id
==
c
.
UserAccount
.
Id
{
c
.
JsonApiErr
(
400
,
"Cannot add yourself as collaborator"
,
nil
)
return
}
cmd
.
AccountId
=
c
.
UserAccount
.
Id
cmd
.
CollaboratorId
=
accountToAdd
.
Id
cmd
.
Role
=
m
.
ROLE_READ_WRITE
err
=
bus
.
Dispatch
(
&
cmd
)
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Could not add collaborator"
,
err
)
return
}
c
.
JsonOK
(
"Collaborator added"
)
}
func
GetCollaborators
(
c
*
middleware
.
Context
)
{
query
:=
m
.
GetCollaboratorsQuery
{
AccountId
:
c
.
UserAccount
.
Id
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to get collaborators"
,
err
)
return
}
c
.
JSON
(
200
,
query
.
Result
)
}
func
RemoveCollaborator
(
c
*
middleware
.
Context
)
{
collaboratorId
:=
c
.
ParamsInt64
(
":id"
)
cmd
:=
m
.
RemoveCollaboratorCommand
{
AccountId
:
c
.
UserAccount
.
Id
,
CollaboratorId
:
collaboratorId
}
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to remove collaborator"
,
err
)
}
c
.
JsonOK
(
"Collaborator removed"
)
}
pkg/models/account.go
View file @
04bbdbad
...
...
@@ -42,6 +42,14 @@ type CreateAccountCommand struct {
Result
Account
`json:"-"`
}
type
UpdateAccountCommand
struct
{
Email
string
`json:"email" binding:"required"`
Login
string
`json:"login"`
Name
string
`json:"name"`
AccountId
int64
`json:"-"`
}
type
SetUsingAccountCommand
struct
{
AccountId
int64
UsingAccountId
int64
...
...
@@ -88,12 +96,6 @@ type OtherAccountDTO struct {
IsUsing
bool
`json:"isUsing"`
}
type
CollaboratorDTO
struct
{
CollaboratorId
int64
`json:"id"`
Email
string
`json:"email"`
Role
string
`json:"role"`
}
type
AccountSearchHitDTO
struct
{
Id
int64
`json:"id"`
Name
string
`json:"name"`
...
...
@@ -105,5 +107,5 @@ type AccountSearchHitDTO struct {
type
AccountDTO
struct
{
Email
string
`json:"email"`
Name
string
`json:"name"`
Collaborators
[]
*
CollaboratorDTO
`json:"collaborators
"`
Login
string
`json:"login
"`
}
pkg/models/collaborator.go
View file @
04bbdbad
...
...
@@ -21,6 +21,19 @@ type Collaborator struct {
Updated
time
.
Time
}
func
NewCollaborator
(
accountId
int64
,
collaboratorId
int64
,
role
RoleType
)
*
Collaborator
{
return
&
Collaborator
{
AccountId
:
accountId
,
CollaboratorId
:
collaboratorId
,
Role
:
role
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
}
// ---------------------
// COMMANDS
type
RemoveCollaboratorCommand
struct
{
CollaboratorId
int64
AccountId
int64
...
...
@@ -33,12 +46,20 @@ type AddCollaboratorCommand struct {
Role
RoleType
`json:"-"`
}
func
NewCollaborator
(
accountId
int64
,
collaboratorId
int64
,
role
RoleType
)
*
Collaborator
{
return
&
Collaborator
{
AccountId
:
accountId
,
CollaboratorId
:
collaboratorId
,
Role
:
role
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
// ----------------------
// QUERIES
type
GetCollaboratorsQuery
struct
{
AccountId
int64
Result
[]
*
CollaboratorDTO
}
// ----------------------
// Projections and DTOs
type
CollaboratorDTO
struct
{
CollaboratorId
int64
`json:"id"`
Email
string
`json:"email"`
Login
string
`json:"login"`
Role
string
`json:"role"`
}
pkg/services/sqlstore/accounts.go
View file @
04bbdbad
...
...
@@ -18,9 +18,8 @@ func init() {
bus
.
AddHandler
(
"sql"
,
GetAccountById
)
bus
.
AddHandler
(
"sql"
,
GetAccountByLogin
)
bus
.
AddHandler
(
"sql"
,
GetAccountByToken
)
bus
.
AddHandler
(
"sql"
,
AddCollaborator
)
bus
.
AddHandler
(
"sql"
,
RemoveCollaborator
)
bus
.
AddHandler
(
"sql"
,
SearchAccounts
)
bus
.
AddHandler
(
"sql"
,
UpdateAccount
)
}
func
CreateAccount
(
cmd
*
m
.
CreateAccountCommand
)
error
{
...
...
@@ -44,6 +43,21 @@ func CreateAccount(cmd *m.CreateAccountCommand) error {
})
}
func
UpdateAccount
(
cmd
*
m
.
UpdateAccountCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
account
:=
m
.
Account
{
Email
:
cmd
.
Email
,
Login
:
cmd
.
Login
,
Name
:
cmd
.
Name
,
Updated
:
time
.
Now
(),
}
_
,
err
:=
sess
.
Id
(
cmd
.
AccountId
)
.
Update
(
&
account
)
return
err
})
}
func
SetUsingAccount
(
cmd
*
m
.
SetUsingAccountCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
account
:=
m
.
Account
{}
...
...
@@ -68,33 +82,12 @@ func GetAccountInfo(query *m.GetAccountInfoQuery) error {
query
.
Result
=
m
.
AccountDTO
{
Name
:
account
.
Name
,
Email
:
account
.
Email
,
Collaborators
:
make
([]
*
m
.
CollaboratorDTO
,
0
)
,
Login
:
account
.
Login
,
}
sess
:=
x
.
Table
(
"collaborator"
)
sess
.
Join
(
"INNER"
,
"account"
,
"account.id=collaborator.collaborator_id"
)
sess
.
Where
(
"collaborator.account_id=?"
,
query
.
Id
)
err
=
sess
.
Find
(
&
query
.
Result
.
Collaborators
)
return
err
}
func
AddCollaborator
(
cmd
*
m
.
AddCollaboratorCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
entity
:=
m
.
Collaborator
{
AccountId
:
cmd
.
AccountId
,
CollaboratorId
:
cmd
.
CollaboratorId
,
Role
:
cmd
.
Role
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
_
,
err
:=
sess
.
Insert
(
&
entity
)
return
err
})
}
func
GetAccountById
(
query
*
m
.
GetAccountByIdQuery
)
error
{
var
err
error
...
...
@@ -165,14 +158,6 @@ func GetAccountByLogin(query *m.GetAccountByLoginQuery) error {
return
nil
}
func
RemoveCollaborator
(
cmd
*
m
.
RemoveCollaboratorCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
var
rawSql
=
"DELETE FROM collaborator WHERE collaborator_id=? and account_id=?"
_
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
CollaboratorId
,
cmd
.
AccountId
)
return
err
})
}
func
GetOtherAccounts
(
query
*
m
.
GetOtherAccountsQuery
)
error
{
query
.
Result
=
make
([]
*
m
.
OtherAccountDTO
,
0
)
sess
:=
x
.
Table
(
"collaborator"
)
...
...
pkg/services/sqlstore/collaborators.go
0 → 100644
View file @
04bbdbad
package
sqlstore
import
(
"time"
"github.com/go-xorm/xorm"
"github.com/torkelo/grafana-pro/pkg/bus"
m
"github.com/torkelo/grafana-pro/pkg/models"
)
func
init
()
{
bus
.
AddHandler
(
"sql"
,
AddCollaborator
)
bus
.
AddHandler
(
"sql"
,
RemoveCollaborator
)
bus
.
AddHandler
(
"sql"
,
GetCollaborators
)
}
func
AddCollaborator
(
cmd
*
m
.
AddCollaboratorCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
entity
:=
m
.
Collaborator
{
AccountId
:
cmd
.
AccountId
,
CollaboratorId
:
cmd
.
CollaboratorId
,
Role
:
cmd
.
Role
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
_
,
err
:=
sess
.
Insert
(
&
entity
)
return
err
})
}
func
GetCollaborators
(
query
*
m
.
GetCollaboratorsQuery
)
error
{
query
.
Result
=
make
([]
*
m
.
CollaboratorDTO
,
0
)
sess
:=
x
.
Table
(
"collaborator"
)
sess
.
Join
(
"INNER"
,
"account"
,
"collaborator.collaborator_id=account.id"
)
sess
.
Where
(
"collaborator.account_id=?"
,
query
.
AccountId
)
sess
.
Cols
(
"collaborator.collaborator_id"
,
"collaborator.role"
,
"account.email"
,
"account.login"
)
err
:=
sess
.
Find
(
&
query
.
Result
)
return
err
}
func
RemoveCollaborator
(
cmd
*
m
.
RemoveCollaboratorCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
var
rawSql
=
"DELETE FROM collaborator WHERE collaborator_id=? and account_id=?"
_
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
CollaboratorId
,
cmd
.
AccountId
)
return
err
})
}
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