nginx-proxy-manager-zh/backend/internal/entity/upstreamserver/methods.go

58 lines
1.3 KiB
Go
Raw Normal View History

package upstreamserver
import (
"npm/internal/database"
"npm/internal/entity"
"npm/internal/model"
)
// GetByID finds a Upstream Server by ID
func GetByID(id int) (Model, error) {
var m Model
err := m.LoadByID(id)
return m, err
}
// GetByUpstreamID finds all servers in the upstream
func GetByUpstreamID(upstreamID uint) ([]Model, error) {
items := make([]Model, 0)
db := database.GetDB()
result := db.Where("upstream_id = ?", upstreamID).Order("server ASC").Find(&items)
return items, result.Error
}
// List will return a list of Upstreams
func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse, error) {
var result entity.ListResponse
defaultSort := model.Sort{
Field: "server",
Direction: "ASC",
}
2023-05-30 23:52:58 -04:00
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
// Get count of items in this search
var totalRows int64
if res := dbo.Model(&Model{}).Count(&totalRows); res.Error != nil {
return result, res.Error
}
// Get rows
items := make([]Model, 0)
2023-05-30 23:52:58 -04:00
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
return result, res.Error
}
result = entity.ListResponse{
Items: items,
Total: totalRows,
Limit: pageInfo.Limit,
Offset: pageInfo.Offset,
Sort: pageInfo.GetSort(defaultSort),
Filter: filters,
}
return result, nil
}