You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
266 lines
5.2 KiB
266 lines
5.2 KiB
2 years ago
|
package panel
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"github.com/han-joker/moo-layout/api/moo/dbm"
|
||
|
"github.com/han-joker/moo-layout/api/moo/logm"
|
||
|
"github.com/han-joker/moo-layout/api/panel/models"
|
||
|
"github.com/han-joker/moo-layout/api/tables"
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
func CategoryPost(c *gin.Context) {
|
||
|
req := models.CategoryReq{}
|
||
|
if err := c.ShouldBind(&req); err != nil {
|
||
|
c.JSON(200, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
row := &tables.Category{
|
||
|
Name: req.Name,
|
||
|
Description: req.Description,
|
||
|
ParentID: req.ParentID,
|
||
|
Sorter: req.Sorter,
|
||
|
Status: req.Status,
|
||
|
}
|
||
|
|
||
|
if err := dbm.Get().Create(&row).Error; err != nil {
|
||
|
c.JSON(200, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(200, gin.H{
|
||
|
"error": nil,
|
||
|
"row": row,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func CategoryPut(c *gin.Context) {
|
||
|
id, err := strconv.Atoi(c.Query("id"))
|
||
|
if err != nil {
|
||
|
logm.Get().Info(err.Error())
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
row := tables.Category{}
|
||
|
if err := dbm.Get().Where("id=?", id).First(&row).Error; err != nil {
|
||
|
logm.Get().Info(err.Error())
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
req := models.CategoryReq{}
|
||
|
if err := c.ShouldBind(&req); err != nil {
|
||
|
logm.Get().Info(err.Error())
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
row.Name = req.Name
|
||
|
row.Description = req.Description
|
||
|
row.Status = req.Status
|
||
|
row.Sorter = req.Sorter
|
||
|
row.ParentID = req.ParentID
|
||
|
|
||
|
if err := dbm.Get().Save(&row).Error; err != nil {
|
||
|
logm.Get().Info(err.Error())
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": nil,
|
||
|
"row": row,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func CategoryGets(c *gin.Context) {
|
||
|
query := models.CategoryQuery{}
|
||
|
if err := c.ShouldBind(&query); err != nil {
|
||
|
}
|
||
|
|
||
|
rows := []tables.Category{}
|
||
|
if err := dbm.Get().Order("sorter asc").Find(&rows).Error; err != nil {
|
||
|
logm.Get().Info(err.Error())
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
nested := make([]models.CategoryNested, 0)
|
||
|
for _, row := range rows {
|
||
|
if row.ID == query.Filters.ID {
|
||
|
nested = append(nested, models.CategoryNested{
|
||
|
Category: row,
|
||
|
})
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if len(nested) == 0 {
|
||
|
nested = categoryChildren(rows, query.Filters.ID)
|
||
|
} else if len(nested) == 1 {
|
||
|
nested[0].Children = categoryChildren(rows, query.Filters.ID)
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": nil,
|
||
|
"rows": nested,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func categoryChildren(rows []tables.Category, parentID uint) []models.CategoryNested {
|
||
|
children := make([]models.CategoryNested, 0)
|
||
|
|
||
|
for _, row := range rows {
|
||
|
if row.ParentID == parentID {
|
||
|
item := models.CategoryNested{
|
||
|
Category: row,
|
||
|
}
|
||
|
item.Children = categoryChildren(rows, row.ID)
|
||
|
children = append(children, item)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return children
|
||
|
}
|
||
|
|
||
|
func CategoryGet(c *gin.Context) {
|
||
|
|
||
|
id, err := strconv.Atoi(c.Query("id"))
|
||
|
if err != nil {
|
||
|
logm.Get().Info(err.Error())
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error":err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
row := tables.Category{}
|
||
|
if err := dbm.Get().Where("id=?", id).First(&row).Error; err != nil {
|
||
|
logm.Get().Info(err.Error())
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": nil,
|
||
|
"row": row,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func CategoryDelete(c *gin.Context) {
|
||
|
id, err := strconv.Atoi(c.Query("id"))
|
||
|
if err != nil {
|
||
|
logm.Get().Info(err.Error())
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
row := tables.Category{}
|
||
|
if err := dbm.Get().Where("id=?", id).First(&row).Error; err != nil {
|
||
|
logm.Get().Info(err.Error())
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
childrenSize := int64(0)
|
||
|
if err := dbm.Get().Model(&tables.Category{}).Where("parent_id=?", row.ID).Count(&childrenSize).Error; err != nil {
|
||
|
logm.Get().Info(err.Error())
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
if childrenSize > 0 {
|
||
|
err := errors.New("has children")
|
||
|
logm.Get().Info(err.Error())
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if err := dbm.Get().Delete(&row).Error; err != nil {
|
||
|
logm.Get().Info(err.Error())
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": nil,
|
||
|
"row": row,
|
||
|
})
|
||
|
return
|
||
|
|
||
|
}
|
||
|
|
||
|
func CategoryCount(c *gin.Context) {
|
||
|
|
||
|
query := models.CategoryQuery{}
|
||
|
if err := c.ShouldBind(&query); err != nil {
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
rows := []tables.Category{}
|
||
|
if err := dbm.Get().Find(&rows).Error; err != nil {
|
||
|
logm.Get().Info(err.Error())
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
count := categoryCount(rows, query)
|
||
|
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"error": nil,
|
||
|
"count": count,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func categoryCount(rows []tables.Category, query models.CategoryQuery) int64 {
|
||
|
count := int64(0)
|
||
|
|
||
|
for _, row := range rows {
|
||
|
statusCond := true
|
||
|
if query.Filters.Status > 0 {
|
||
|
statusCond = row.Status == query.Filters.Status
|
||
|
}
|
||
|
|
||
|
if row.ParentID == query.Filters.ParentID && statusCond {
|
||
|
count += 1
|
||
|
count += categoryCount(rows, models.CategoryQuery{
|
||
|
Filters: models.CategoryQueryFilters{
|
||
|
ParentID: row.ID,
|
||
|
Status: query.Filters.Status,
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return count
|
||
|
}
|