2025-04-18 17:17:23 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"admin/apps/game/domain/entity"
|
|
|
|
"admin/apps/game/model"
|
2025-04-24 20:39:31 +08:00
|
|
|
"admin/internal/consts"
|
2025-04-18 17:17:23 +08:00
|
|
|
"admin/internal/errcode"
|
2025-04-24 20:39:31 +08:00
|
|
|
"errors"
|
2025-04-18 17:17:23 +08:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IProjectRepo interface {
|
2025-04-24 20:39:31 +08:00
|
|
|
EnsureProjectsDBData() ([]*model.Project, error)
|
|
|
|
List() ([]*entity.Project, error)
|
|
|
|
GetByProjectId(id string) (*entity.Project, bool, error)
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectRepo(db *gorm.DB) IProjectRepo {
|
2025-04-24 20:39:31 +08:00
|
|
|
model.GetProjectServersHandler = func(projectId string) ([]*model.Server, error) {
|
|
|
|
servers := make([]*model.Server, 0)
|
|
|
|
err := db.Where("project_id=?", projectId).Find(&servers).Error
|
|
|
|
return servers, err
|
|
|
|
}
|
|
|
|
return &projectRepoImpl{db: db}
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type projectRepoImpl struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
2025-04-24 20:39:31 +08:00
|
|
|
func (impl *projectRepoImpl) EnsureProjectsDBData() ([]*model.Project, error) {
|
|
|
|
var curProjects []*model.Project
|
|
|
|
err := impl.db.Find(&curProjects).Error
|
2025-04-18 17:17:23 +08:00
|
|
|
if err != nil {
|
2025-04-24 20:39:31 +08:00
|
|
|
return nil, errcode.New(errcode.DBError, "find all projects error:%v", err)
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
2025-04-24 20:39:31 +08:00
|
|
|
for k, v := range consts.RegisteredProjects {
|
|
|
|
find := false
|
|
|
|
for _, cur := range curProjects {
|
|
|
|
if k == cur.ProjectId {
|
|
|
|
find = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !find {
|
|
|
|
po := &model.Project{ProjectId: k, Name: v.Name}
|
|
|
|
err := impl.db.Create(po).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, errcode.New(errcode.DBError, "create project:%v error:%v", k, err)
|
|
|
|
}
|
|
|
|
curProjects = append(curProjects, po)
|
|
|
|
}
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
2025-04-24 20:39:31 +08:00
|
|
|
return curProjects, nil
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
|
|
|
|
2025-04-24 20:39:31 +08:00
|
|
|
func (impl *projectRepoImpl) List() ([]*entity.Project, error) {
|
|
|
|
list := make([]*model.Project, 0)
|
|
|
|
err := impl.db.Find(&list).Error
|
2025-04-18 17:17:23 +08:00
|
|
|
if err != nil {
|
2025-04-24 20:39:31 +08:00
|
|
|
return nil, errcode.New(errcode.DBError, "list project error:%v", err)
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
2025-04-24 20:39:31 +08:00
|
|
|
|
|
|
|
list1 := make([]*entity.Project, 0, len(list))
|
|
|
|
for _, v := range list {
|
|
|
|
list1 = append(list1, (&entity.CommonResource{}).FromPo(v).ToProjectEntity())
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
2025-04-24 20:39:31 +08:00
|
|
|
return list1, nil
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
|
|
|
|
2025-04-24 20:39:31 +08:00
|
|
|
func (impl *projectRepoImpl) GetByProjectId(id string) (*entity.Project, bool, error) {
|
|
|
|
po := &model.Project{}
|
|
|
|
err := impl.db.Where("project_id=?", id).Find(po).Error
|
2025-04-18 17:17:23 +08:00
|
|
|
if err != nil {
|
2025-04-24 20:39:31 +08:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return (&entity.CommonResource{}).FromPo(po).ToProjectEntity(), false, nil
|
|
|
|
}
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
2025-04-24 20:39:31 +08:00
|
|
|
|
|
|
|
return (&entity.CommonResource{}).FromPo(po).ToProjectEntity(), true, nil
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|