package dao

import "github.com/pingcap/tispector/service/db_info"

type ClusterImpl struct {
}

var Cluster ClusterImpl

// AddCluster 添加集群信息
func (ClusterImpl) AddCluster(cluster *db_info.TispectorCluster) (int64, error) {
	err := GetDB().Create(cluster).Error
	return cluster.ID, err
}

// UpdateCluster 更新集群信息
func (ClusterImpl) UpdateCluster(cluster *db_info.TispectorCluster) error {
	return GetDB().Model(cluster).Where("id = ?", cluster.ID).Update(cluster).Error
}

// DelCluster 删除某个集群
func (ClusterImpl) DelCluster(id int64) error {
	return GetDB().Where("id = ?", id).Delete(&db_info.TispectorCluster{}).Error
}

// GetClusterByName 通过集群名字来获取该集群信息
func (ClusterImpl) GetClusterByName(name string) (*db_info.TispectorCluster, error) {
	var cluster db_info.TispectorCluster
	return &cluster, GetDB().First(&cluster, "name = ?", name).Error
}

// ListCluster 列举所有集群
func (ClusterImpl) ListCluster() ([]db_info.TispectorCluster, error) {
	var clusters []db_info.TispectorCluster
	return clusters, GetDB().Find(&clusters).Error
}
