Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pref: Adjust node editing logic #7217

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core/app/api/v2/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,25 @@ func (b *BaseApi) Upgrade(c *gin.Context) {
}
helper.SuccessWithData(c, nil)
}

// @Tags System Setting
// @Summary Upgrade
// @Description 系统回滚
// @Accept json
// @Param request body dto.OperateByID true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /core/settings/rollback [post]
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"upgrade_logs","output_column":"old_version","output_value":"version"}],"formatZH":"回滚系统 => [version]","formatEN":"rollback system => [version]"}
func (b *BaseApi) Rollback(c *gin.Context) {
var req dto.OperateByID
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}

if err := upgradeService.Rollback(req); err != nil {
helper.InternalServer(c, err)
return
}
helper.SuccessWithData(c, nil)
}
12 changes: 6 additions & 6 deletions core/app/repo/app_launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (
type LauncherRepo struct{}

type ILauncherRepo interface {
Get(opts ...DBOption) (model.AppLauncher, error)
List(opts ...DBOption) ([]model.AppLauncher, error)
Get(opts ...global.DBOption) (model.AppLauncher, error)
List(opts ...global.DBOption) ([]model.AppLauncher, error)
Create(launcher *model.AppLauncher) error
Delete(opts ...DBOption) error
Delete(opts ...global.DBOption) error
}

func NewILauncherRepo() ILauncherRepo {
return &LauncherRepo{}
}

func (u *LauncherRepo) Get(opts ...DBOption) (model.AppLauncher, error) {
func (u *LauncherRepo) Get(opts ...global.DBOption) (model.AppLauncher, error) {
var launcher model.AppLauncher
db := global.DB
for _, opt := range opts {
Expand All @@ -27,7 +27,7 @@ func (u *LauncherRepo) Get(opts ...DBOption) (model.AppLauncher, error) {
err := db.First(&launcher).Error
return launcher, err
}
func (u *LauncherRepo) List(opts ...DBOption) ([]model.AppLauncher, error) {
func (u *LauncherRepo) List(opts ...global.DBOption) ([]model.AppLauncher, error) {
var ops []model.AppLauncher
db := global.DB.Model(&model.AppLauncher{})
for _, opt := range opts {
Expand All @@ -41,7 +41,7 @@ func (u *LauncherRepo) Create(launcher *model.AppLauncher) error {
return global.DB.Create(launcher).Error
}

func (u *LauncherRepo) Delete(opts ...DBOption) error {
func (u *LauncherRepo) Delete(opts ...global.DBOption) error {
db := global.DB
for _, opt := range opts {
db = opt(db)
Expand Down
16 changes: 8 additions & 8 deletions core/app/repo/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (
type BackupRepo struct{}

type IBackupRepo interface {
Get(opts ...DBOption) (model.BackupAccount, error)
List(opts ...DBOption) ([]model.BackupAccount, error)
Page(limit, offset int, opts ...DBOption) (int64, []model.BackupAccount, error)
Get(opts ...global.DBOption) (model.BackupAccount, error)
List(opts ...global.DBOption) ([]model.BackupAccount, error)
Page(limit, offset int, opts ...global.DBOption) (int64, []model.BackupAccount, error)
Create(backup *model.BackupAccount) error
Save(backup *model.BackupAccount) error
Delete(opts ...DBOption) error
Delete(opts ...global.DBOption) error
}

func NewIBackupRepo() IBackupRepo {
return &BackupRepo{}
}

func (u *BackupRepo) Get(opts ...DBOption) (model.BackupAccount, error) {
func (u *BackupRepo) Get(opts ...global.DBOption) (model.BackupAccount, error) {
var backup model.BackupAccount
db := global.DB
for _, opt := range opts {
Expand All @@ -30,7 +30,7 @@ func (u *BackupRepo) Get(opts ...DBOption) (model.BackupAccount, error) {
return backup, err
}

func (u *BackupRepo) Page(page, size int, opts ...DBOption) (int64, []model.BackupAccount, error) {
func (u *BackupRepo) Page(page, size int, opts ...global.DBOption) (int64, []model.BackupAccount, error) {
var ops []model.BackupAccount
db := global.DB.Model(&model.BackupAccount{})
for _, opt := range opts {
Expand All @@ -42,7 +42,7 @@ func (u *BackupRepo) Page(page, size int, opts ...DBOption) (int64, []model.Back
return count, ops, err
}

func (u *BackupRepo) List(opts ...DBOption) ([]model.BackupAccount, error) {
func (u *BackupRepo) List(opts ...global.DBOption) ([]model.BackupAccount, error) {
var ops []model.BackupAccount
db := global.DB.Model(&model.BackupAccount{})
for _, opt := range opts {
Expand All @@ -60,7 +60,7 @@ func (u *BackupRepo) Save(backup *model.BackupAccount) error {
return global.DB.Save(backup).Error
}

func (u *BackupRepo) Delete(opts ...DBOption) error {
func (u *BackupRepo) Delete(opts ...global.DBOption) error {
db := global.DB
for _, opt := range opts {
db = opt(db)
Expand Down
28 changes: 20 additions & 8 deletions core/app/repo/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@ package repo
import (
"github.com/1Panel-dev/1Panel/core/app/model"
"github.com/1Panel-dev/1Panel/core/global"
"gorm.io/gorm"
)

type CommandRepo struct{}

type ICommandRepo interface {
List(opts ...DBOption) ([]model.Command, error)
Page(limit, offset int, opts ...DBOption) (int64, []model.Command, error)
List(opts ...global.DBOption) ([]model.Command, error)
Page(limit, offset int, opts ...global.DBOption) (int64, []model.Command, error)
Create(command *model.Command) error
Update(id uint, vars map[string]interface{}) error
UpdateGroup(group, newGroup uint) error
Delete(opts ...DBOption) error
Get(opts ...DBOption) (model.Command, error)
Delete(opts ...global.DBOption) error
Get(opts ...global.DBOption) (model.Command, error)

WithByInfo(info string) global.DBOption
}

func NewICommandRepo() ICommandRepo {
return &CommandRepo{}
}

func (u *CommandRepo) Get(opts ...DBOption) (model.Command, error) {
func (u *CommandRepo) Get(opts ...global.DBOption) (model.Command, error) {
var command model.Command
db := global.DB
for _, opt := range opts {
Expand All @@ -31,7 +34,7 @@ func (u *CommandRepo) Get(opts ...DBOption) (model.Command, error) {
return command, err
}

func (u *CommandRepo) Page(page, size int, opts ...DBOption) (int64, []model.Command, error) {
func (u *CommandRepo) Page(page, size int, opts ...global.DBOption) (int64, []model.Command, error) {
var users []model.Command
db := global.DB.Model(&model.Command{})
for _, opt := range opts {
Expand All @@ -43,7 +46,7 @@ func (u *CommandRepo) Page(page, size int, opts ...DBOption) (int64, []model.Com
return count, users, err
}

func (u *CommandRepo) List(opts ...DBOption) ([]model.Command, error) {
func (u *CommandRepo) List(opts ...global.DBOption) ([]model.Command, error) {
var commands []model.Command
db := global.DB.Model(&model.Command{})
for _, opt := range opts {
Expand All @@ -64,10 +67,19 @@ func (h *CommandRepo) UpdateGroup(group, newGroup uint) error {
return global.DB.Model(&model.Command{}).Where("group_id = ?", group).Updates(map[string]interface{}{"group_id": newGroup}).Error
}

func (u *CommandRepo) Delete(opts ...DBOption) error {
func (u *CommandRepo) Delete(opts ...global.DBOption) error {
db := global.DB
for _, opt := range opts {
db = opt(db)
}
return db.Delete(&model.Command{}).Error
}

func (c *CommandRepo) WithByInfo(info string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
if len(info) == 0 {
return g
}
return g.Where("name like ? or command like ?", "%"+info+"%", "%"+info+"%")
}
}
65 changes: 33 additions & 32 deletions core/app/repo/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ import (
"fmt"

"github.com/1Panel-dev/1Panel/core/constant"
"github.com/1Panel-dev/1Panel/core/global"
"gorm.io/gorm"
)

type DBOption func(*gorm.DB) *gorm.DB

type ICommonRepo interface {
WithByID(id uint) DBOption
WithByIDs(ids []uint) DBOption
WithByName(name string) DBOption
WithLikeName(name string) DBOption
WithByType(ty string) DBOption
WithByKey(key string) DBOption
WithOrderBy(orderStr string) DBOption
WithByStatus(status string) DBOption
WithByID(id uint) global.DBOption
WithByGroupID(id uint) global.DBOption

WithByName(name string) global.DBOption
WithByType(ty string) global.DBOption
WithByKey(key string) global.DBOption
WithOrderBy(orderStr string) global.DBOption
WithByStatus(status string) global.DBOption
WithByGroupBelong(group string) global.DBOption

WithOrderRuleBy(orderBy, order string) DBOption
WithByIDs(ids []uint) global.DBOption

WithOrderRuleBy(orderBy, order string) global.DBOption
}

type CommonRepo struct{}
Expand All @@ -28,57 +30,56 @@ func NewICommonRepo() ICommonRepo {
return &CommonRepo{}
}

func (c *CommonRepo) WithByID(id uint) DBOption {
func (c *CommonRepo) WithByID(id uint) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id = ?", id)
}
}
func (c *CommonRepo) WithByIDs(ids []uint) DBOption {
func (c *CommonRepo) WithByGroupID(id uint) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id in (?)", ids)
return g.Where("group_id = ?", id)
}
}
func (c *CommonRepo) WithByName(name string) DBOption {

func (c *CommonRepo) WithByIDs(ids []uint) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
if len(name) == 0 {
return g
}
return g.Where("`name` = ?", name)
return g.Where("id in (?)", ids)
}
}
func (c *CommonRepo) WithLikeName(name string) DBOption {
func (c *CommonRepo) WithByName(name string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
if len(name) == 0 {
return g
}
return g.Where("name like ? or command like ?", "%"+name+"%", "%"+name+"%")
return g.Where("`name` = ?", name)
}
}
func (c *CommonRepo) WithByType(ty string) DBOption {

func (c *CommonRepo) WithByType(ty string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
if len(ty) == 0 {
return g
}
return g.Where("`type` = ?", ty)
}
}
func (c *CommonRepo) WithByKey(key string) DBOption {
func (c *CommonRepo) WithByKey(key string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("key = ?", key)
}
}
func (c *CommonRepo) WithByStatus(status string) DBOption {
func (c *CommonRepo) WithByStatus(status string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("status = ?", status)
}
}
func (c *CommonRepo) WithOrderBy(orderStr string) DBOption {
func (c *CommonRepo) WithByGroupBelong(group string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("group_belong = ?", group)
}
}

func (c *CommonRepo) WithOrderBy(orderStr string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Order(orderStr)
}
}

func (c *CommonRepo) WithOrderRuleBy(orderBy, order string) DBOption {
func (c *CommonRepo) WithOrderRuleBy(orderBy, order string) global.DBOption {
switch order {
case constant.OrderDesc:
order = "desc"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code is incomplete and contains some errors. Here are my observations:

There seems to be an inconsistent usage of global.DBOption in multiple places, which needs to be fixed.

As for specific issues or optimizations, it's important to review each implementation based on real-world requirements.

However, based solely on the provided comments, I can make general notes that could help improve efficiency:

  • The goroutine syntax should not used due to its deprecated nature.
  • There isn't enough context available about how you're using goroutines inside this code snippet, but if used appropriately, they may introduce significant overheads in certain scenarios.
  • Consider using Gorm functions more effectively instead of creating custom ones. For example, use where with SQL queries directly rather than manually building them using where clauses from database objects.

If there was supposed to be further information here such as additional interfaces, structures, method signatures or examples of data types, let me know!

Expand Down
37 changes: 8 additions & 29 deletions core/app/repo/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,27 @@ import (
type GroupRepo struct{}

type IGroupRepo interface {
Get(opts ...DBOption) (model.Group, error)
GetList(opts ...DBOption) ([]model.Group, error)
Get(opts ...global.DBOption) (model.Group, error)
GetList(opts ...global.DBOption) ([]model.Group, error)
Create(group *model.Group) error
Update(id uint, vars map[string]interface{}) error
Delete(opts ...DBOption) error
Delete(opts ...global.DBOption) error

WithByID(id uint) DBOption
WithByGroupID(id uint) DBOption
WithByGroupType(ty string) DBOption
WithByDefault(isDefalut bool) DBOption
WithByDefault(isDefalut bool) global.DBOption
CancelDefault(groupType string) error
}

func NewIGroupRepo() IGroupRepo {
return &GroupRepo{}
}

func (c *GroupRepo) WithByID(id uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id = ?", id)
}
}

func (c *GroupRepo) WithByGroupID(id uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("group_id = ?", id)
}
}

func (c *GroupRepo) WithByGroupType(ty string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("`type` = ?", ty)
}
}

func (c *GroupRepo) WithByDefault(isDefalut bool) DBOption {
func (c *GroupRepo) WithByDefault(isDefalut bool) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("is_default = ?", isDefalut)
}
}

func (u *GroupRepo) Get(opts ...DBOption) (model.Group, error) {
func (u *GroupRepo) Get(opts ...global.DBOption) (model.Group, error) {
var group model.Group
db := global.DB
for _, opt := range opts {
Expand All @@ -60,7 +39,7 @@ func (u *GroupRepo) Get(opts ...DBOption) (model.Group, error) {
return group, err
}

func (u *GroupRepo) GetList(opts ...DBOption) ([]model.Group, error) {
func (u *GroupRepo) GetList(opts ...global.DBOption) ([]model.Group, error) {
var groups []model.Group
db := global.DB.Model(&model.Group{})
for _, opt := range opts {
Expand All @@ -78,7 +57,7 @@ func (u *GroupRepo) Update(id uint, vars map[string]interface{}) error {
return global.DB.Model(&model.Group{}).Where("id = ?", id).Updates(vars).Error
}

func (u *GroupRepo) Delete(opts ...DBOption) error {
func (u *GroupRepo) Delete(opts ...global.DBOption) error {
db := global.DB
for _, opt := range opts {
db = opt(db)
Expand Down
Loading