分类接口完成

This commit is contained in:
coward 2024-10-24 16:55:31 +08:00
parent f66fef2ce0
commit 980c81af11
5 changed files with 151 additions and 15 deletions

View File

@ -0,0 +1,73 @@
package admin
import (
"errors"
"github.com/gin-gonic/gin"
"website-nav/http/param"
"website-nav/http/response"
"website-nav/service"
)
type NavigationType struct{}
func NavigationTypeAPI() NavigationType {
return NavigationType{}
}
// List
// @description: 分页列表
// @receiver NavigationType
// @param c
func (NavigationType) List(c *gin.Context) {
var p param.Page
if err := c.ShouldBind(&p); err != nil {
response.R(c).Validator(err)
return
}
data, total, err := service.NavigationType().List(p)
if err != nil {
response.R(c).FailedWithError(err)
return
}
response.R(c).Paginate(data, total, p.Current, p.Size)
}
// Save
// @description: 新增/保存分类
// @receiver NavigationType
// @param c
func (NavigationType) Save(c *gin.Context) {
var p param.SaveNavigationType
if err := c.ShouldBind(&p); err != nil {
response.R(c).Validator(err)
return
}
if err := service.NavigationType().CreateType(p); err != nil {
response.R(c).FailedWithError(err)
return
}
response.R(c).OK()
}
// Delete
// @description: 删除分类
// @receiver NavigationType
// @param c
func (NavigationType) Delete(c *gin.Context) {
var id = c.Param("id")
if id == "" || id == "undefined" {
response.R(c).Validator(errors.New("id不能为空"))
return
}
if err := service.NavigationType().Delete(id); err != nil {
response.R(c).FailedWithError(err)
return
}
response.R(c).OK()
}

9
http/param/navigation.go Normal file
View File

@ -0,0 +1,9 @@
package param
// SaveNavigationType
// @description: 新增/编辑导航分类
type SaveNavigationType struct {
Id string `json:"id" form:"id" binding:"omitempty"`
Name string `json:"name" form:"name" binding:"required"`
Icon string `json:"icon" form:"icon" binding:"required"`
}

View File

@ -47,5 +47,11 @@ func userAPI(r *gin.RouterGroup) {
// @description: 导航分类API // @description: 导航分类API
// @param r // @param r
func navTypeAPI(r *gin.RouterGroup) { func navTypeAPI(r *gin.RouterGroup) {
navType := r.Group("/nav-type")
navType.Use(middleware.Authorization())
{
navType.GET("/list", admin.NavigationTypeAPI().List) // 分类列表
navType.POST("/save", admin.NavigationTypeAPI().Save) // 新增/编辑分类
navType.DELETE("/:id", admin.NavigationTypeAPI().Delete) // 删除分类
}
} }

View File

@ -1,18 +1,13 @@
package vo package vo
// TypeItem import "website-nav/model"
// @description: 导航分类
type TypeItem struct {
ID string `json:"id"`
Name string `json:"name"`
Icon string `json:"icon"`
}
type NavigationItem struct { // NavigationTypeItem
ID string `json:"id"` // @description: 导航分类
Title string `json:"title"` type NavigationTypeItem struct {
Url string `json:"url"` ID string `json:"id"`
Icon string `json:"icon"` Name string `json:"name"`
Desc string `json:"desc"` Icon string `json:"icon"`
Enabled bool `json:"enabled"` CreatedAt model.JsonTime `json:"createdAt"`
UpdatedAt model.JsonTime `json:"updatedAt"`
} }

View File

@ -0,0 +1,53 @@
package service
import (
"gorm.io/gorm"
"website-nav/global/client"
"website-nav/http/param"
"website-nav/http/vo"
"website-nav/model"
)
type navigationType struct {
*gorm.DB
}
func NavigationType() navigationType { return navigationType{DB: client.DB} }
// List
// @description: 分页列表
// @receiver s
// @return list
// @return err
func (s navigationType) List(p param.Page) (list []vo.NavigationTypeItem, total int64, err error) {
err = s.Model(&model.NavigationType{}).Scopes(Paginate(p.Current, p.Size)).Find(&list).Offset(-1).Limit(-1).Count(&total).Error
return
}
// CreateType
// @description: 新增/编辑分类
// @receiver s
// @param p
// @return error
func (s navigationType) CreateType(p param.SaveNavigationType) error {
if p.Id != "" {
return s.Model(&model.NavigationType{}).Where("id = ?", p.Id).Updates(map[string]any{
"name": p.Name,
"icon": p.Icon,
}).Error
}
return s.Create(&model.NavigationType{
Name: p.Name,
Icon: p.Icon,
}).Error
}
// Delete
// @description: 删除分类
// @receiver s
// @param id
// @return error
func (s navigationType) Delete(id string) error {
return s.Model(&model.NavigationType{}).Where("id = ?", id).Delete(&model.NavigationType{}).Error
}