From 43a1d1484dd5b21895d1f0c5c45b164858108867 Mon Sep 17 00:00:00 2001 From: idafurjes <36131195+idafurjes@users.noreply.github.com> Date: Tue, 20 Sep 2022 12:57:51 +0200 Subject: [PATCH] Add some org methods to org service interface (#55449) * Add some org methods to org service interface * Fix fake method name --- pkg/services/org/model.go | 8 +++ pkg/services/org/org.go | 5 ++ pkg/services/org/orgimpl/org.go | 111 +++++++++++++++++++++++++++++++ pkg/services/org/orgtest/fake.go | 20 ++++++ 4 files changed, 144 insertions(+) diff --git a/pkg/services/org/model.go b/pkg/services/org/model.go index 0e85ad5dd18..d2fae4766f9 100644 --- a/pkg/services/org/model.go +++ b/pkg/services/org/model.go @@ -89,6 +89,14 @@ type OrgDTO struct { Name string `json:"name"` } +type GetOrgByIdQuery struct { + ID int64 +} + +type GetOrgByNameQuery struct { + Name string +} + func (r RoleType) IsValid() bool { return r == RoleViewer || r == RoleAdmin || r == RoleEditor } diff --git a/pkg/services/org/org.go b/pkg/services/org/org.go index 6e07a11cc22..e447ea127ed 100644 --- a/pkg/services/org/org.go +++ b/pkg/services/org/org.go @@ -11,4 +11,9 @@ type Service interface { GetUserOrgList(context.Context, *GetUserOrgListQuery) ([]*UserOrgDTO, error) UpdateOrg(context.Context, *UpdateOrgCommand) error Search(context.Context, *SearchOrgsQuery) ([]*OrgDTO, error) + GetByID(context.Context, *GetOrgByIdQuery) (*Org, error) + GetByNameHandler(context.Context, *GetOrgByNameQuery) (*Org, error) + GetByName(string) (*Org, error) + CreateWithMember(name string, userID int64) (*Org, error) + Create(ctx context.Context, cmd *CreateOrgCommand) (*Org, error) } diff --git a/pkg/services/org/orgimpl/org.go b/pkg/services/org/orgimpl/org.go index 63ce5b637c8..1d503af0adf 100644 --- a/pkg/services/org/orgimpl/org.go +++ b/pkg/services/org/orgimpl/org.go @@ -132,3 +132,114 @@ func (s *Service) Search(ctx context.Context, query *org.SearchOrgsQuery) ([]*or } return res, nil } + +// TODO: remove wrapper around sqlstore +func (s *Service) GetByID(ctx context.Context, query *org.GetOrgByIdQuery) (*org.Org, error) { + q := &models.GetOrgByIdQuery{Id: query.ID} + err := s.sqlStore.GetOrgById(ctx, q) + if err != nil { + return nil, err + } + return &org.Org{ + ID: q.Result.Id, + Version: q.Result.Version, + Name: q.Result.Name, + Address1: q.Result.Address1, + Address2: q.Result.Address2, + City: q.Result.City, + ZipCode: q.Result.ZipCode, + State: q.Result.State, + Country: q.Result.Country, + Created: q.Result.Created, + Updated: q.Result.Updated, + }, nil +} + +// TODO: remove wrapper around sqlstore +func (s *Service) GetByNameHandler(ctx context.Context, query *org.GetOrgByNameQuery) (*org.Org, error) { + q := &models.GetOrgByNameQuery{Name: query.Name} + err := s.sqlStore.GetOrgByNameHandler(ctx, q) + if err != nil { + return nil, err + } + return &org.Org{ + ID: q.Result.Id, + Version: q.Result.Version, + Name: q.Result.Name, + Address1: q.Result.Address1, + Address2: q.Result.Address2, + City: q.Result.City, + ZipCode: q.Result.ZipCode, + State: q.Result.State, + Country: q.Result.Country, + Created: q.Result.Created, + Updated: q.Result.Updated, + }, nil +} + +// TODO: remove wrapper around sqlstore +func (s *Service) GetByName(name string) (*org.Org, error) { + orga, err := s.sqlStore.GetOrgByName(name) + if err != nil { + return nil, err + } + return &org.Org{ + ID: orga.Id, + Version: orga.Version, + Name: orga.Name, + Address1: orga.Address1, + Address2: orga.Address2, + City: orga.City, + ZipCode: orga.ZipCode, + State: orga.State, + Country: orga.Country, + Created: orga.Created, + Updated: orga.Updated, + }, nil +} + +// TODO: remove wrapper around sqlstore +func (s *Service) CreateWithMember(name string, userID int64) (*org.Org, error) { + orga, err := s.sqlStore.CreateOrgWithMember(name, userID) + if err != nil { + return nil, err + } + return &org.Org{ + ID: orga.Id, + Version: orga.Version, + Name: orga.Name, + Address1: orga.Address1, + Address2: orga.Address2, + City: orga.City, + ZipCode: orga.ZipCode, + State: orga.State, + Country: orga.Country, + Created: orga.Created, + Updated: orga.Updated, + }, nil +} + +// TODO: remove wrapper around sqlstore +func (s *Service) Create(ctx context.Context, cmd *org.CreateOrgCommand) (*org.Org, error) { + q := &models.CreateOrgCommand{ + Name: cmd.Name, + UserId: cmd.UserID, + } + err := s.sqlStore.CreateOrg(ctx, q) + if err != nil { + return nil, err + } + return &org.Org{ + ID: q.Result.Id, + Version: q.Result.Version, + Name: q.Result.Name, + Address1: q.Result.Address1, + Address2: q.Result.Address2, + City: q.Result.City, + ZipCode: q.Result.ZipCode, + State: q.Result.State, + Country: q.Result.Country, + Created: q.Result.Created, + Updated: q.Result.Updated, + }, nil +} diff --git a/pkg/services/org/orgtest/fake.go b/pkg/services/org/orgtest/fake.go index 93eb6083d7a..a9f9d9c8231 100644 --- a/pkg/services/org/orgtest/fake.go +++ b/pkg/services/org/orgtest/fake.go @@ -11,6 +11,7 @@ type FakeOrgService struct { ExpectedError error ExpectedUserOrgDTO []*org.UserOrgDTO ExpectedOrgs []*org.OrgDTO + ExpectedOrg *org.Org } func NewOrgServiceFake() *FakeOrgService { @@ -44,3 +45,22 @@ func (f *FakeOrgService) UpdateOrg(ctx context.Context, cmd *org.UpdateOrgComman func (f *FakeOrgService) Search(ctx context.Context, query *org.SearchOrgsQuery) ([]*org.OrgDTO, error) { return f.ExpectedOrgs, f.ExpectedError } + +func (f *FakeOrgService) GetByID(ctx context.Context, query *org.GetOrgByIdQuery) (*org.Org, error) { + return f.ExpectedOrg, f.ExpectedError +} + +func (f *FakeOrgService) GetByNameHandler(ctx context.Context, query *org.GetOrgByNameQuery) (*org.Org, error) { + return f.ExpectedOrg, f.ExpectedError +} + +func (f *FakeOrgService) GetByName(name string) (*org.Org, error) { + return f.ExpectedOrg, f.ExpectedError +} + +func (f *FakeOrgService) CreateWithMember(name string, userID int64) (*org.Org, error) { + return f.ExpectedOrg, f.ExpectedError +} +func (f *FakeOrgService) Create(ctx context.Context, cmd *org.CreateOrgCommand) (*org.Org, error) { + return f.ExpectedOrg, f.ExpectedError +}