⚡分类接口完成
This commit is contained in:
parent
f66fef2ce0
commit
980c81af11
73
http/api/admin/navigation_type.go
Normal file
73
http/api/admin/navigation_type.go
Normal 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
9
http/param/navigation.go
Normal 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"`
|
||||
}
|
@ -47,5 +47,11 @@ func userAPI(r *gin.RouterGroup) {
|
||||
// @description: 导航分类API
|
||||
// @param r
|
||||
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) // 删除分类
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,13 @@
|
||||
package vo
|
||||
|
||||
// TypeItem
|
||||
import "website-nav/model"
|
||||
|
||||
// NavigationTypeItem
|
||||
// @description: 导航分类
|
||||
type TypeItem struct {
|
||||
type NavigationTypeItem struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Icon string `json:"icon"`
|
||||
}
|
||||
|
||||
type NavigationItem struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Url string `json:"url"`
|
||||
Icon string `json:"icon"`
|
||||
Desc string `json:"desc"`
|
||||
Enabled bool `json:"enabled"`
|
||||
CreatedAt model.JsonTime `json:"createdAt"`
|
||||
UpdatedAt model.JsonTime `json:"updatedAt"`
|
||||
}
|
||||
|
53
service/navigation_type.go
Normal file
53
service/navigation_type.go
Normal 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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user