mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Move updateorg out of sqlstore (#54111)
* Chore: move updateorg out of sqlstore * fix api test
This commit is contained in:
@@ -71,6 +71,11 @@ type UserOrgDTO struct {
|
||||
Role RoleType `json:"role"`
|
||||
}
|
||||
|
||||
type UpdateOrgCommand struct {
|
||||
Name string
|
||||
OrgId int64
|
||||
}
|
||||
|
||||
func (r RoleType) IsValid() bool {
|
||||
return r == RoleViewer || r == RoleAdmin || r == RoleEditor
|
||||
}
|
||||
|
||||
@@ -9,4 +9,5 @@ type Service interface {
|
||||
InsertOrgUser(context.Context, *OrgUser) (int64, error)
|
||||
DeleteUserFromAll(context.Context, int64) error
|
||||
GetUserOrgList(context.Context, *GetUserOrgListQuery) ([]*UserOrgDTO, error)
|
||||
UpdateOrg(ctx context.Context, cmd *UpdateOrgCommand) error
|
||||
}
|
||||
|
||||
@@ -104,3 +104,7 @@ func (s *Service) GetUserOrgList(ctx context.Context, query *org.GetUserOrgListQ
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *Service) UpdateOrg(ctx context.Context, cmd *org.UpdateOrgCommand) error {
|
||||
return s.store.Update(ctx, cmd)
|
||||
}
|
||||
|
||||
@@ -79,3 +79,7 @@ func (f *FakeOrgStore) InsertOrgUser(ctx context.Context, org *org.OrgUser) (int
|
||||
func (f *FakeOrgStore) DeleteUserFromAll(ctx context.Context, userID int64) error {
|
||||
return f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakeOrgStore) Update(ctx context.Context, cmd *org.UpdateOrgCommand) error {
|
||||
return f.ExpectedError
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@ package orgimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/events"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
||||
@@ -17,6 +19,7 @@ type store interface {
|
||||
Insert(context.Context, *org.Org) (int64, error)
|
||||
InsertOrgUser(context.Context, *org.OrgUser) (int64, error)
|
||||
DeleteUserFromAll(context.Context, int64) error
|
||||
Update(ctx context.Context, cmd *org.UpdateOrgCommand) error
|
||||
}
|
||||
|
||||
type sqlStore struct {
|
||||
@@ -91,3 +94,52 @@ func (ss *sqlStore) DeleteUserFromAll(ctx context.Context, userID int64) error {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (ss *sqlStore) Update(ctx context.Context, cmd *org.UpdateOrgCommand) error {
|
||||
return ss.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
if isNameTaken, err := isOrgNameTaken(cmd.Name, cmd.OrgId, sess); err != nil {
|
||||
return err
|
||||
} else if isNameTaken {
|
||||
return models.ErrOrgNameTaken
|
||||
}
|
||||
|
||||
org := models.Org{
|
||||
Name: cmd.Name,
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
affectedRows, err := sess.ID(cmd.OrgId).Update(&org)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if affectedRows == 0 {
|
||||
return models.ErrOrgNotFound
|
||||
}
|
||||
|
||||
sess.PublishAfterCommit(&events.OrgUpdated{
|
||||
Timestamp: org.Updated,
|
||||
Id: org.Id,
|
||||
Name: org.Name,
|
||||
})
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func isOrgNameTaken(name string, existingId int64, sess *sqlstore.DBSession) (bool, error) {
|
||||
// check if org name is taken
|
||||
var org models.Org
|
||||
exists, err := sess.Where("name=?", name).Get(&org)
|
||||
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
if exists && existingId != org.Id {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
@@ -35,3 +35,7 @@ func (f *FakeOrgService) DeleteUserFromAll(ctx context.Context, userID int64) er
|
||||
func (f *FakeOrgService) GetUserOrgList(ctx context.Context, query *org.GetUserOrgListQuery) ([]*org.UserOrgDTO, error) {
|
||||
return f.ExpectedUserOrgDTO, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakeOrgService) UpdateOrg(ctx context.Context, cmd *org.UpdateOrgCommand) error {
|
||||
return f.ExpectedError
|
||||
}
|
||||
|
||||
@@ -100,10 +100,6 @@ func (m *SQLStoreMock) CreateOrg(ctx context.Context, cmd *models.CreateOrgComma
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) UpdateOrg(ctx context.Context, cmd *models.UpdateOrgCommand) error {
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) UpdateOrgAddress(ctx context.Context, cmd *models.UpdateOrgAddressCommand) error {
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
@@ -159,39 +159,6 @@ func (ss *SQLStore) CreateOrg(ctx context.Context, cmd *models.CreateOrgCommand)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ss *SQLStore) UpdateOrg(ctx context.Context, cmd *models.UpdateOrgCommand) error {
|
||||
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
if isNameTaken, err := isOrgNameTaken(cmd.Name, cmd.OrgId, sess); err != nil {
|
||||
return err
|
||||
} else if isNameTaken {
|
||||
return models.ErrOrgNameTaken
|
||||
}
|
||||
|
||||
org := models.Org{
|
||||
Name: cmd.Name,
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
affectedRows, err := sess.ID(cmd.OrgId).Update(&org)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if affectedRows == 0 {
|
||||
return models.ErrOrgNotFound
|
||||
}
|
||||
|
||||
sess.publishAfterCommit(&events.OrgUpdated{
|
||||
Timestamp: org.Updated,
|
||||
Id: org.Id,
|
||||
Name: org.Name,
|
||||
})
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (ss *SQLStore) UpdateOrgAddress(ctx context.Context, cmd *models.UpdateOrgAddressCommand) error {
|
||||
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
org := models.Org{
|
||||
|
||||
@@ -22,7 +22,6 @@ type Store interface {
|
||||
GetOrgByName(name string) (*models.Org, error)
|
||||
CreateOrg(ctx context.Context, cmd *models.CreateOrgCommand) error
|
||||
CreateOrgWithMember(name string, userID int64) (models.Org, error)
|
||||
UpdateOrg(ctx context.Context, cmd *models.UpdateOrgCommand) error
|
||||
UpdateOrgAddress(ctx context.Context, cmd *models.UpdateOrgAddressCommand) error
|
||||
DeleteOrg(ctx context.Context, cmd *models.DeleteOrgCommand) error
|
||||
GetOrgById(context.Context, *models.GetOrgByIdQuery) error
|
||||
|
||||
Reference in New Issue
Block a user