feat: add filter of category for sites list

This commit is contained in:
Jacky 2024-10-25 11:24:09 +08:00
parent 207f80f858
commit aa556767f2
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
10 changed files with 278 additions and 80 deletions

View file

@ -49,7 +49,7 @@ func GetSite(c *gin.Context) {
}
s := query.Site
site, err := s.Where(s.Path.Eq(path)).FirstOrInit()
site, err := s.Where(s.Path.Eq(path)).FirstOrCreate()
if err != nil {
api.ErrHandler(c, err)
return
@ -300,6 +300,14 @@ func DeleteSite(c *gin.Context) {
var err error
name := c.Param("name")
availablePath := nginx.GetConfPath("sites-available", name)
s := query.Site
_, err = s.Where(s.Path.Eq(availablePath)).Unscoped().Delete(&model.Site{})
if err != nil {
api.ErrHandler(c, err)
return
}
enabledPath := nginx.GetConfPath("sites-enabled", name)
if _, err = os.Stat(availablePath); os.IsNotExist(err) {
c.JSON(http.StatusNotFound, gin.H{

View file

@ -4,9 +4,14 @@ import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/config"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"github.com/samber/lo"
"github.com/spf13/cast"
"net/http"
"os"
"path/filepath"
"strings"
)
@ -15,6 +20,7 @@ func GetSiteList(c *gin.Context) {
enabled := c.Query("enabled")
orderBy := c.Query("order_by")
sort := c.DefaultQuery("sort", "desc")
querySiteCategoryId := cast.ToUint64(c.Query("site_category_id"))
configFiles, err := os.ReadDir(nginx.GetConfPath("sites-available"))
if err != nil {
@ -28,6 +34,20 @@ func GetSiteList(c *gin.Context) {
return
}
s := query.Site
sTx := s.Preload(s.SiteCategory)
if querySiteCategoryId != 0 {
sTx.Where(s.SiteCategoryID.Eq(querySiteCategoryId))
}
sites, err := sTx.Find()
if err != nil {
api.ErrHandler(c, err)
return
}
sitesMap := lo.SliceToMap(sites, func(item *model.Site) (string, *model.Site) {
return filepath.Base(item.Path), item
})
enabledConfigMap := make(map[string]bool)
for i := range enabledConfig {
enabledConfigMap[enabledConfig[i].Name()] = true
@ -38,28 +58,46 @@ func GetSiteList(c *gin.Context) {
for i := range configFiles {
file := configFiles[i]
fileInfo, _ := file.Info()
if !file.IsDir() {
// name filter
if name != "" && !strings.Contains(file.Name(), name) {
if file.IsDir() {
continue
}
// name filter
if name != "" && !strings.Contains(file.Name(), name) {
continue
}
// status filter
if enabled != "" {
if enabled == "true" && !enabledConfigMap[file.Name()] {
continue
}
// status filter
if enabled != "" {
if enabled == "true" && !enabledConfigMap[file.Name()] {
continue
}
if enabled == "false" && enabledConfigMap[file.Name()] {
continue
}
if enabled == "false" && enabledConfigMap[file.Name()] {
continue
}
configs = append(configs, config.Config{
Name: file.Name(),
ModifiedAt: fileInfo.ModTime(),
Size: fileInfo.Size(),
IsDir: fileInfo.IsDir(),
Enabled: enabledConfigMap[file.Name()],
})
}
var (
siteCategoryId uint64
siteCategory *model.SiteCategory
)
if site, ok := sitesMap[file.Name()]; ok {
siteCategoryId = site.SiteCategoryID
siteCategory = site.SiteCategory
}
// site category filter
if querySiteCategoryId != 0 && siteCategoryId != querySiteCategoryId {
continue
}
configs = append(configs, config.Config{
Name: file.Name(),
ModifiedAt: fileInfo.ModTime(),
Size: fileInfo.Size(),
IsDir: fileInfo.IsDir(),
Enabled: enabledConfigMap[file.Name()],
SiteCategoryID: siteCategoryId,
SiteCategory: siteCategory,
})
}
configs = config.Sort(orderBy, sort, configs)