Skip to content

Commit

Permalink
feature. add clusters value to cloudSetting VO
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkfree committed Mar 27, 2023
1 parent fe8ab21 commit 844aae5
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 16 deletions.
6 changes: 3 additions & 3 deletions internal/repository/cloud-setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func (r *CloudSettingRepository) Get(cloudSettingId uuid.UUID) (out domain.Cloud
func (r *CloudSettingRepository) Fetch(organizationId string) (out []domain.CloudSetting, err error) {
var cloudSettings []CloudSetting
res := r.db.Find(&cloudSettings, "organization_id = ?", organizationId)
if res.RowsAffected == 0 {
return nil, httpErrors.NewNotFoundError(fmt.Errorf("No data found"))
}
if res.Error != nil {
return nil, res.Error
}
if res.RowsAffected == 0 {
return nil, httpErrors.NewNotFoundError(fmt.Errorf("No data found"))
}

for _, cloudSetting := range cloudSettings {
out = append(out, r.reflect(cloudSetting))
Expand Down
35 changes: 30 additions & 5 deletions internal/repository/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type IClusterRepository interface {
WithTrx(*gorm.DB) IClusterRepository
Fetch() (res []domain.Cluster, err error)
FetchByOrganizationId(organizationId string) (res []domain.Cluster, err error)
FetchByCloudSettingId(cloudSettingId uuid.UUID) (res []domain.Cluster, err error)
Get(id string) (domain.Cluster, error)
Create(organizationId string, templateId string, name string, conf *domain.ClusterConf, creator uuid.UUID, description string) (clusterId string, err error)
Delete(id string) error
Expand Down Expand Up @@ -54,6 +55,8 @@ type Cluster struct {
WorkflowId string
Status domain.ClusterStatus
StatusDesc string
CloudSettingId uuid.UUID
CloudSetting CloudSetting `gorm:"foreignKey:CloudSettingId"`
}

func (c *Cluster) BeforeCreate(tx *gorm.DB) (err error) {
Expand Down Expand Up @@ -86,25 +89,47 @@ func (r *ClusterRepository) Fetch() (out []domain.Cluster, err error) {
return out, nil
}

func (r *ClusterRepository) FetchByOrganizationId(organizationId string) (resClusters []domain.Cluster, err error) {
// [TODO] Need refactoring about filters and pagination
func (r *ClusterRepository) FetchByOrganizationId(organizationId string) (out []domain.Cluster, err error) {
var clusters []Cluster

res := r.db.Find(&clusters, "organization_id = ?", organizationId)

if res.Error != nil {
return nil, fmt.Errorf("Error while finding clusters with organizationId: %s", organizationId)
return nil, res.Error
}

if res.RowsAffected == 0 {
return resClusters, nil
return out, nil
}

for _, cluster := range clusters {
outCluster := r.reflect(cluster)
resClusters = append(resClusters, outCluster)
out = append(out, outCluster)
}

return
}

func (r *ClusterRepository) FetchByCloudSettingId(cloudSettingId uuid.UUID) (out []domain.Cluster, err error) {
var clusters []Cluster

res := r.db.Find(&clusters, "cloud_setting_id = ?", cloudSettingId)

if res.Error != nil {
return nil, res.Error
}

if res.RowsAffected == 0 {
return out, nil
}

for _, cluster := range clusters {
outCluster := r.reflect(cluster)
out = append(out, outCluster)
}

return resClusters, nil
return
}

func (r *ClusterRepository) Get(id string) (domain.Cluster, error) {
Expand Down
4 changes: 4 additions & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
package repository

import "gorm.io/gorm"

type FilterFunc func(user *gorm.DB) *gorm.DB
2 changes: 1 addition & 1 deletion internal/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repository

import (
"fmt"

"github.com/google/uuid"
"github.com/pkg/errors"

Expand Down Expand Up @@ -30,7 +31,6 @@ type IUserRepository interface {
AssignRoleWithUuid(uuid uuid.UUID, roleName string) error
}

type FilterFunc func(user *gorm.DB) *gorm.DB
type UserRepository struct {
db *gorm.DB
}
Expand Down
27 changes: 20 additions & 7 deletions internal/usecase/cloud-setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,9 @@ func (u *CloudSettingUsecase) Get(cloudSettingId uuid.UUID) (res domain.CloudSet
return domain.CloudSetting{}, err
}

/*
res, err = u.clusterRepo.Get(cloudSettingId)
if err != nil {
return domain.CloudSetting{}, err
}
*/
res.Clusters = u.getClusterCnt(cloudSettingId)

return res, nil
return
}

func (u *CloudSettingUsecase) Fetch(organizationId string) (res []domain.CloudSetting, err error) {
Expand All @@ -106,3 +101,21 @@ func (u *CloudSettingUsecase) Delete(cloudSettingId uuid.UUID) (err error) {

return nil
}

func (u *CloudSettingUsecase) getClusterCnt(cloudSettingId uuid.UUID) (cnt int) {
cnt = 0

clusters, err := u.clusterRepo.FetchByCloudSettingId(cloudSettingId)
if err != nil {
log.Error("Failed to get clusters by cloudSettingId. err : ", err)
return cnt
}

for _, cluster := range clusters {
if cluster.Status != "DELETED" {
cnt++
}
}

return cnt
}
12 changes: 12 additions & 0 deletions internal/usecase/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type IClusterUsecase interface {
WithTrx(*gorm.DB) IClusterUsecase
Fetch(organizationId string) ([]domain.Cluster, error)
FetchByCloudSettingId(cloudSettingId uuid.UUID) (out []domain.Cluster, err error)
Create(organizationId string, templateId string, name string, conf domain.ClusterConf, creatorId string, description string) (clusterId string, err error)
Get(clusterId string) (out domain.Cluster, err error)
Delete(clusterId string) (err error)
Expand Down Expand Up @@ -81,6 +82,17 @@ func (u *ClusterUsecase) Fetch(organizationId string) (out []domain.Cluster, err
return out, nil
}

func (u *ClusterUsecase) FetchByCloudSettingId(cloudSettingId uuid.UUID) (out []domain.Cluster, err error) {
if cloudSettingId == uuid.Nil {
return nil, fmt.Errorf("Invalid cloudSettingId")
}

if err != nil {
return nil, err
}
return out, nil
}

func (u *ClusterUsecase) Create(organizationId string, templateId string, name string, conf domain.ClusterConf, creatorId string, description string) (clusterId string, err error) {
creator := uuid.Nil
if creatorId != "" {
Expand Down

0 comments on commit 844aae5

Please sign in to comment.