🎨后台接口终于磨磨蹭蹭的写完了

This commit is contained in:
coward 2024-10-28 16:36:32 +08:00
parent 980c81af11
commit 3f8d68fcbf
7 changed files with 689 additions and 0 deletions

1
go.mod
View File

@ -25,6 +25,7 @@ require (
require (
github.com/aliyun/aliyun-oss-go-sdk v2.2.5+incompatible // indirect
github.com/badoux/goscraper v0.0.0-20190827161153-36995ce6b19f // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect

483
go.sum

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -7,3 +7,24 @@ type SaveNavigationType struct {
Name string `json:"name" form:"name" binding:"required"`
Icon string `json:"icon" form:"icon" binding:"required"`
}
// NavigationList
// @description: 导航分页列表查询
type NavigationList struct {
Type string `json:"type" form:"type" binding:"omitempty"`
Title string `json:"title" form:"title" binding:"omitempty"`
Link string `json:"link" form:"link" binding:"omitempty"`
Page
}
// SaveNavigation
// @description: 新增/编辑导航
type SaveNavigation struct {
Id string `json:"id" form:"id" binding:"omitempty"`
TypeId string `json:"typeId" form:"typeId" binding:"required"`
Title string `json:"title" form:"title" binding:"required"`
Url string `json:"url" form:"url" binding:"required"`
Icon string `json:"icon" form:"icon" binding:"omitempty"`
Enabled int `json:"enabled" form:"enabled" binding:"required,oneof=0 1"`
Description string `json:"description" form:"description" binding:"omitempty"`
}

View File

@ -12,6 +12,7 @@ func adminAPI(r *gin.RouterGroup) {
loginAPI(adminApi)
userAPI(adminApi)
navTypeAPI(adminApi)
navigation(adminApi)
}
}
@ -55,3 +56,16 @@ func navTypeAPI(r *gin.RouterGroup) {
navType.DELETE("/:id", admin.NavigationTypeAPI().Delete) // 删除分类
}
}
// navigation
// @description: 导航相关API
// @param r
func navigation(r *gin.RouterGroup) {
nav := r.Group("/navigation")
nav.Use(middleware.Authorization())
{
nav.GET("/list", admin.NavigationAPI().List) // 导航列表
nav.POST("/save", admin.NavigationAPI().Save) // 新增/编辑导航
nav.DELETE("/:id", admin.NavigationAPI().Delete) // 删除导航
}
}

View File

@ -11,3 +11,18 @@ type NavigationTypeItem struct {
CreatedAt model.JsonTime `json:"createdAt"`
UpdatedAt model.JsonTime `json:"updatedAt"`
}
// NavigationItem
// @description: 导航
type NavigationItem struct {
Id string `json:"id"`
Type string `json:"type"`
TypeId string `json:"typeId"`
Title string `json:"title"`
Url string `json:"url"`
Description string `json:"description"`
Icon string `json:"icon"`
Enabled bool `json:"enabled"`
CreatedAt model.JsonTime `json:"createdAt"`
UpdatedAt model.JsonTime `json:"updatedAt"`
}

84
service/navigation.go Normal file
View File

@ -0,0 +1,84 @@
package service
import (
"fmt"
"github.com/badoux/goscraper"
"github.com/spf13/cast"
"gorm.io/gorm"
"website-nav/global/client"
"website-nav/http/param"
"website-nav/http/vo"
"website-nav/model"
)
type navigation struct {
*gorm.DB
}
func Navigation() navigation { return navigation{DB: client.DB} }
// List
// @description: 导航分页列表
// @receiver navigation
// @param p
// @return data
// @return total
// @return err
func (s navigation) List(p param.NavigationList) (data []vo.NavigationItem, total int64, err error) {
sql := s.Table("t_navigation as tn").
Scopes(Paginate(p.Current, p.Size)).
Joins("LEFT JOIN t_navigation_type as tnt ON tnt.id = tn.type_id").
Select("tn.id,tn.title,tn.url,tn.description,tn.icon,tn.enabled,tn.created_at,tn.updated_at,tnt.id as type_id,tnt.name as type")
if p.Type != "" {
sql.Where("tn.type_id = ?", p.Type)
}
if p.Title != "" {
sql.Where("tn.title LIKE ?", "%"+p.Title+"%")
}
if p.Link != "" {
sql.Where("tn.url LIKE ?", "%"+p.Link+"%")
}
err = sql.Find(&data).Offset(-1).Limit(-1).Count(&total).Error
return
}
// SaveNavigation
// @description: 新增/编辑导航
// @receiver s
// @param p
// @return error
func (s navigation) SaveNavigation(p param.SaveNavigation) error {
if p.Id != "" {
return s.Model(&model.Navigation{}).Where("id = ?", p.Id).Updates(map[string]any{
"title": p.Title,
"url": p.Url,
"icon": p.Icon,
"enabled": p.Enabled,
"description": p.Description,
}).Error
}
websiteInfo, err := goscraper.Scrape(p.Url, 5)
if err != nil {
return err
}
return s.Create(&model.Navigation{
Title: p.Title,
Url: p.Url,
Icon: fmt.Sprintf("%s%s", p.Url, websiteInfo.Preview.Icon),
Enabled: cast.ToBool(p.Enabled),
Description: p.Description,
}).Error
}
// DeleteNavigation
// @description: 删除导航
// @receiver s
// @param id
// @return error
func (s navigation) DeleteNavigation(id string) error {
return s.Where("id = ?", id).Delete(&model.Navigation{}).Error
}