Commit 04bbdbad by Torkel Ödegaard

Worked on account update, moved collaborats to its own api url and files

parent 804bff55
Subproject commit 5b93e09714dbee6c1c181daf0182704334d8c6be
Subproject commit 9d1dacb8d417cac2cc66797e5dae6deba36c3080
......@@ -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 removed")
c.JsonOK("Account updated")
}
func GetOtherAccounts(c *middleware.Context) {
......
......@@ -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)
......
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")
}
......@@ -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"`
}
......@@ -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"`
}
......@@ -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")
......
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
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment