feat(api): add reset sort endpoints for server and node

This commit is contained in:
Chang lue Tsen
2025-09-01 11:41:34 -04:00
parent e910d0e345
commit 367ef9d2e7
12 changed files with 288 additions and 89 deletions
+12
View File
@@ -5,6 +5,7 @@ import (
"time"
"github.com/pkg/errors"
"gorm.io/gorm"
)
type Server struct {
@@ -25,6 +26,17 @@ func (*Server) TableName() string {
return "servers"
}
func (m *Server) BeforeCreate(tx *gorm.DB) error {
if m.Sort == 0 {
var maxSort int
if err := tx.Model(&Server{}).Select("COALESCE(MAX(sort), 0)").Scan(&maxSort).Error; err != nil {
return err
}
m.Sort = maxSort + 1
}
return nil
}
// MarshalProtocols Marshal server protocols to json
func (m *Server) MarshalProtocols(list []Protocol) error {
var validate = make(map[string]bool)