添加category的模板以及相关的sql文件
This commit is contained in:
1 parent
598d5549c8
commit
fc259ba274
11 files changed
+1000
No files matched your search
@@ -0,0 +1,192 @@
|
||||
package apis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/api"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
|
||||
_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
|
||||
|
||||
"go-admin/app/jbxh/models"
|
||||
"go-admin/app/jbxh/service"
|
||||
"go-admin/app/jbxh/service/dto"
|
||||
"go-admin/common/actions"
|
||||
)
|
||||
|
||||
type Category struct {
|
||||
api.Api
|
||||
}
|
||||
|
||||
// GetPage 获取分类列表
|
||||
// @Summary 获取分类列表
|
||||
// @Description 获取分类列表
|
||||
// @Tags 分类
|
||||
// @Param pageSize query int false "页条数"
|
||||
// @Param pageIndex query int false "页码"
|
||||
// @Success 200 {object} response.Response{data=response.Page{list=[]models.Category}} "{"code": 200, "data": [...]}"
|
||||
// @Router /api/v1/category [get]
|
||||
// @Security Bearer
|
||||
func (e Category) GetPage(c *gin.Context) {
|
||||
req := dto.CategoryGetPageReq{}
|
||||
s := service.Category{}
|
||||
err := e.MakeContext(c).
|
||||
MakeOrm().
|
||||
Bind(&req, binding.Form).
|
||||
MakeService(&s.Service).
|
||||
Errors
|
||||
if err != nil {
|
||||
e.Logger.Error(err)
|
||||
e.Error(500, err, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
p := actions.GetPermissionFromContext(c)
|
||||
list := make([]models.Category, 0)
|
||||
var count int64
|
||||
|
||||
err = s.GetPage(&req, p, &list, &count)
|
||||
if err != nil {
|
||||
e.Error(500, err, fmt.Sprintf("获取分类失败,\r\n失败信息 %s", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
|
||||
}
|
||||
|
||||
// Get 获取分类
|
||||
// @Summary 获取分类
|
||||
// @Description 获取分类
|
||||
// @Tags 分类
|
||||
// @Param id path int false "id"
|
||||
// @Success 200 {object} response.Response{data=models.Category} "{"code": 200, "data": [...]}"
|
||||
// @Router /api/v1/category/{id} [get]
|
||||
// @Security Bearer
|
||||
func (e Category) Get(c *gin.Context) {
|
||||
req := dto.CategoryGetReq{}
|
||||
s := service.Category{}
|
||||
err := e.MakeContext(c).
|
||||
MakeOrm().
|
||||
Bind(&req).
|
||||
MakeService(&s.Service).
|
||||
Errors
|
||||
if err != nil {
|
||||
e.Logger.Error(err)
|
||||
e.Error(500, err, err.Error())
|
||||
return
|
||||
}
|
||||
var object models.Category
|
||||
|
||||
p := actions.GetPermissionFromContext(c)
|
||||
err = s.Get(&req, p, &object)
|
||||
if err != nil {
|
||||
e.Error(500, err, fmt.Sprintf("获取分类失败,\r\n失败信息 %s", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
e.OK(object, "查询成功")
|
||||
}
|
||||
|
||||
// Insert 创建分类
|
||||
// @Summary 创建分类
|
||||
// @Description 创建分类
|
||||
// @Tags 分类
|
||||
// @Accept application/json
|
||||
// @Product application/json
|
||||
// @Param data body dto.CategoryInsertReq true "data"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "message": "添加成功"}"
|
||||
// @Router /api/v1/category [post]
|
||||
// @Security Bearer
|
||||
func (e Category) Insert(c *gin.Context) {
|
||||
req := dto.CategoryInsertReq{}
|
||||
s := service.Category{}
|
||||
err := e.MakeContext(c).
|
||||
MakeOrm().
|
||||
Bind(&req).
|
||||
MakeService(&s.Service).
|
||||
Errors
|
||||
if err != nil {
|
||||
e.Logger.Error(err)
|
||||
e.Error(500, err, err.Error())
|
||||
return
|
||||
}
|
||||
// 设置创建人
|
||||
req.SetCreateBy(user.GetUserId(c))
|
||||
|
||||
err = s.Insert(&req)
|
||||
if err != nil {
|
||||
e.Error(500, err, fmt.Sprintf("创建分类失败,\r\n失败信息 %s", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
e.OK(req.GetId(), "创建成功")
|
||||
}
|
||||
|
||||
// Update 修改分类
|
||||
// @Summary 修改分类
|
||||
// @Description 修改分类
|
||||
// @Tags 分类
|
||||
// @Accept application/json
|
||||
// @Product application/json
|
||||
// @Param id path int true "id"
|
||||
// @Param data body dto.CategoryUpdateReq true "body"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}"
|
||||
// @Router /api/v1/category/{id} [put]
|
||||
// @Security Bearer
|
||||
func (e Category) Update(c *gin.Context) {
|
||||
req := dto.CategoryUpdateReq{}
|
||||
s := service.Category{}
|
||||
err := e.MakeContext(c).
|
||||
MakeOrm().
|
||||
Bind(&req).
|
||||
MakeService(&s.Service).
|
||||
Errors
|
||||
if err != nil {
|
||||
e.Logger.Error(err)
|
||||
e.Error(500, err, err.Error())
|
||||
return
|
||||
}
|
||||
req.SetUpdateBy(user.GetUserId(c))
|
||||
p := actions.GetPermissionFromContext(c)
|
||||
|
||||
err = s.Update(&req, p)
|
||||
if err != nil {
|
||||
e.Error(500, err, fmt.Sprintf("修改分类失败,\r\n失败信息 %s", err.Error()))
|
||||
return
|
||||
}
|
||||
e.OK(req.GetId(), "修改成功")
|
||||
}
|
||||
|
||||
// Delete 删除分类
|
||||
// @Summary 删除分类
|
||||
// @Description 删除分类
|
||||
// @Tags 分类
|
||||
// @Param data body dto.CategoryDeleteReq true "body"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}"
|
||||
// @Router /api/v1/category [delete]
|
||||
// @Security Bearer
|
||||
func (e Category) Delete(c *gin.Context) {
|
||||
s := service.Category{}
|
||||
req := dto.CategoryDeleteReq{}
|
||||
err := e.MakeContext(c).
|
||||
MakeOrm().
|
||||
Bind(&req).
|
||||
MakeService(&s.Service).
|
||||
Errors
|
||||
if err != nil {
|
||||
e.Logger.Error(err)
|
||||
e.Error(500, err, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// req.SetUpdateBy(user.GetUserId(c))
|
||||
p := actions.GetPermissionFromContext(c)
|
||||
|
||||
err = s.Remove(&req, p)
|
||||
if err != nil {
|
||||
e.Error(500, err, fmt.Sprintf("删除分类失败,\r\n失败信息 %s", err.Error()))
|
||||
return
|
||||
}
|
||||
e.OK(req.GetId(), "删除成功")
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"go-admin/common/models"
|
||||
)
|
||||
|
||||
type Category struct {
|
||||
models.Model
|
||||
|
||||
Title string `json:"title" gorm:"type:varchar(127);comment:标题"`
|
||||
Slug string `json:"slug" gorm:"type:varchar(127);comment:id"`
|
||||
Desc string `json:"desc" gorm:"type:varchar(127);comment:描述"`
|
||||
Parent string `json:"parent" gorm:"type:bigint;comment:父"`
|
||||
Order string `json:"order" gorm:"type:bigint;comment:排序"`
|
||||
Status string `json:"status" gorm:"type:int;comment:状态"`
|
||||
models.ModelTime
|
||||
models.ControlBy
|
||||
}
|
||||
|
||||
func (Category) TableName() string {
|
||||
return "category"
|
||||
}
|
||||
|
||||
func (e *Category) Generate() models.ActiveRecord {
|
||||
o := *e
|
||||
return &o
|
||||
}
|
||||
|
||||
func (e *Category) GetId() interface{} {
|
||||
return e.Id
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
|
||||
|
||||
"go-admin/app/jbxh/apis"
|
||||
"go-admin/common/actions"
|
||||
"go-admin/common/middleware"
|
||||
)
|
||||
|
||||
func init() {
|
||||
routerCheckRole = append(routerCheckRole, registerCategoryRouter)
|
||||
}
|
||||
|
||||
// registerCategoryRouter
|
||||
func registerCategoryRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
|
||||
api := apis.Category{}
|
||||
r := v1.Group("/category").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
|
||||
{
|
||||
r.GET("", actions.PermissionAction(), api.GetPage)
|
||||
r.GET("/:id", actions.PermissionAction(), api.Get)
|
||||
r.POST("", api.Insert)
|
||||
r.PUT("/:id", actions.PermissionAction(), api.Update)
|
||||
r.DELETE("", api.Delete)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
//"github.com/go-admin-team/go-admin-core/sdk/pkg"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/go-admin-team/go-admin-core/logger"
|
||||
"github.com/go-admin-team/go-admin-core/sdk"
|
||||
common "go-admin/common/middleware"
|
||||
)
|
||||
|
||||
// InitRouter 路由初始化,不要怀疑,这里用到了
|
||||
func InitRouter() {
|
||||
var r *gin.Engine
|
||||
h := sdk.Runtime.GetEngine()
|
||||
if h == nil {
|
||||
log.Fatal("not found engine...")
|
||||
os.Exit(-1)
|
||||
}
|
||||
switch h.(type) {
|
||||
case *gin.Engine:
|
||||
r = h.(*gin.Engine)
|
||||
default:
|
||||
log.Fatal("not support other engine")
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
authMiddleware, err := common.AuthInit()
|
||||
if err != nil {
|
||||
log.Fatalf("JWT Init Error, %s", err.Error())
|
||||
}
|
||||
|
||||
// 注册业务路由
|
||||
initRouter(r, authMiddleware)
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
|
||||
)
|
||||
|
||||
var (
|
||||
routerNoCheckRole = make([]func(*gin.RouterGroup), 0)
|
||||
routerCheckRole = make([]func(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware), 0)
|
||||
)
|
||||
|
||||
// initRouter 路由示例
|
||||
func initRouter(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gin.Engine {
|
||||
|
||||
// 无需认证的路由
|
||||
noCheckRoleRouter(r)
|
||||
// 需要认证的路由
|
||||
checkRoleRouter(r, authMiddleware)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// noCheckRoleRouter 无需认证的路由示例
|
||||
func noCheckRoleRouter(r *gin.Engine) {
|
||||
// 可根据业务需求来设置接口版本
|
||||
v1 := r.Group("/api/v1")
|
||||
|
||||
for _, f := range routerNoCheckRole {
|
||||
f(v1)
|
||||
}
|
||||
}
|
||||
|
||||
// checkRoleRouter 需要认证的路由示例
|
||||
func checkRoleRouter(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) {
|
||||
// 可根据业务需求来设置接口版本
|
||||
v1 := r.Group("/api/v1")
|
||||
|
||||
for _, f := range routerCheckRole {
|
||||
f(v1, authMiddleware)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/go-admin-team/go-admin-core/sdk/service"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-admin/app/jbxh/models"
|
||||
"go-admin/app/jbxh/service/dto"
|
||||
"go-admin/common/actions"
|
||||
cDto "go-admin/common/dto"
|
||||
)
|
||||
|
||||
type Category struct {
|
||||
service.Service
|
||||
}
|
||||
|
||||
// GetPage 获取Category列表
|
||||
func (e *Category) GetPage(c *dto.CategoryGetPageReq, p *actions.DataPermission, list *[]models.Category, count *int64) error {
|
||||
var err error
|
||||
var data models.Category
|
||||
|
||||
err = e.Orm.Model(&data).
|
||||
Scopes(
|
||||
cDto.MakeCondition(c.GetNeedSearch()),
|
||||
cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
|
||||
actions.Permission(data.TableName(), p),
|
||||
).
|
||||
Find(list).Limit(-1).Offset(-1).
|
||||
Count(count).Error
|
||||
if err != nil {
|
||||
e.Log.Errorf("CategoryService GetPage error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get 获取Category对象
|
||||
func (e *Category) Get(d *dto.CategoryGetReq, p *actions.DataPermission, model *models.Category) error {
|
||||
var data models.Category
|
||||
|
||||
err := e.Orm.Model(&data).
|
||||
Scopes(
|
||||
actions.Permission(data.TableName(), p),
|
||||
).
|
||||
First(model, d.GetId()).Error
|
||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
err = errors.New("查看对象不存在或无权查看")
|
||||
e.Log.Errorf("Service GetCategory error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
e.Log.Errorf("db error:%s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Insert 创建Category对象
|
||||
func (e *Category) Insert(c *dto.CategoryInsertReq) error {
|
||||
var err error
|
||||
var data models.Category
|
||||
c.Generate(&data)
|
||||
err = e.Orm.Create(&data).Error
|
||||
if err != nil {
|
||||
e.Log.Errorf("CategoryService Insert error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update 修改Category对象
|
||||
func (e *Category) Update(c *dto.CategoryUpdateReq, p *actions.DataPermission) error {
|
||||
var err error
|
||||
var data = models.Category{}
|
||||
e.Orm.Scopes(
|
||||
actions.Permission(data.TableName(), p),
|
||||
).First(&data, c.GetId())
|
||||
c.Generate(&data)
|
||||
|
||||
db := e.Orm.Save(&data)
|
||||
if err = db.Error; err != nil {
|
||||
e.Log.Errorf("CategoryService Save error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
if db.RowsAffected == 0 {
|
||||
return errors.New("无权更新该数据")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove 删除Category
|
||||
func (e *Category) Remove(d *dto.CategoryDeleteReq, p *actions.DataPermission) error {
|
||||
var data models.Category
|
||||
|
||||
db := e.Orm.Model(&data).
|
||||
Scopes(
|
||||
actions.Permission(data.TableName(), p),
|
||||
).Delete(&data, d.GetId())
|
||||
if err := db.Error; err != nil {
|
||||
e.Log.Errorf("Service RemoveCategory error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
if db.RowsAffected == 0 {
|
||||
return errors.New("无权删除该数据")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"go-admin/app/jbxh/models"
|
||||
"go-admin/common/dto"
|
||||
common "go-admin/common/models"
|
||||
)
|
||||
|
||||
type CategoryGetPageReq struct {
|
||||
dto.Pagination `search:"-"`
|
||||
CategoryOrder
|
||||
}
|
||||
|
||||
type CategoryOrder struct {
|
||||
Id string `form:"idOrder" search:"type:order;column:id;table:category"`
|
||||
Title string `form:"titleOrder" search:"type:order;column:title;table:category"`
|
||||
Slug string `form:"slugOrder" search:"type:order;column:slug;table:category"`
|
||||
Desc string `form:"descOrder" search:"type:order;column:desc;table:category"`
|
||||
Parent string `form:"parentOrder" search:"type:order;column:parent;table:category"`
|
||||
Order string `form:"orderOrder" search:"type:order;column:order;table:category"`
|
||||
Status string `form:"statusOrder" search:"type:order;column:status;table:category"`
|
||||
CreateBy string `form:"createByOrder" search:"type:order;column:create_by;table:category"`
|
||||
UpdateBy string `form:"updateByOrder" search:"type:order;column:update_by;table:category"`
|
||||
CreatedAt string `form:"createdAtOrder" search:"type:order;column:created_at;table:category"`
|
||||
UpdatedAt string `form:"updatedAtOrder" search:"type:order;column:updated_at;table:category"`
|
||||
DeletedAt string `form:"deletedAtOrder" search:"type:order;column:deleted_at;table:category"`
|
||||
}
|
||||
|
||||
func (m *CategoryGetPageReq) GetNeedSearch() interface{} {
|
||||
return *m
|
||||
}
|
||||
|
||||
type CategoryInsertReq struct {
|
||||
Id int `json:"-" comment:"编码"` // 编码
|
||||
Title string `json:"title" comment:"标题"`
|
||||
Slug string `json:"slug" comment:"id"`
|
||||
Desc string `json:"desc" comment:"描述"`
|
||||
Parent string `json:"parent" comment:"父"`
|
||||
Order string `json:"order" comment:"排序"`
|
||||
Status string `json:"status" comment:"状态"`
|
||||
common.ControlBy
|
||||
}
|
||||
|
||||
func (s *CategoryInsertReq) Generate(model *models.Category) {
|
||||
if s.Id == 0 {
|
||||
model.Model = common.Model{Id: s.Id}
|
||||
}
|
||||
model.Title = s.Title
|
||||
model.Slug = s.Slug
|
||||
model.Desc = s.Desc
|
||||
model.Parent = s.Parent
|
||||
model.Order = s.Order
|
||||
model.Status = s.Status
|
||||
model.CreateBy = s.CreateBy // 添加这而,需要记录是被谁创建的
|
||||
}
|
||||
|
||||
func (s *CategoryInsertReq) GetId() interface{} {
|
||||
return s.Id
|
||||
}
|
||||
|
||||
type CategoryUpdateReq struct {
|
||||
Id int `uri:"id" comment:"编码"` // 编码
|
||||
Title string `json:"title" comment:"标题"`
|
||||
Slug string `json:"slug" comment:"id"`
|
||||
Desc string `json:"desc" comment:"描述"`
|
||||
Parent string `json:"parent" comment:"父"`
|
||||
Order string `json:"order" comment:"排序"`
|
||||
Status string `json:"status" comment:"状态"`
|
||||
common.ControlBy
|
||||
}
|
||||
|
||||
func (s *CategoryUpdateReq) Generate(model *models.Category) {
|
||||
if s.Id == 0 {
|
||||
model.Model = common.Model{Id: s.Id}
|
||||
}
|
||||
model.Title = s.Title
|
||||
model.Slug = s.Slug
|
||||
model.Desc = s.Desc
|
||||
model.Parent = s.Parent
|
||||
model.Order = s.Order
|
||||
model.Status = s.Status
|
||||
model.UpdateBy = s.UpdateBy // 添加这而,需要记录是被谁更新的
|
||||
}
|
||||
|
||||
func (s *CategoryUpdateReq) GetId() interface{} {
|
||||
return s.Id
|
||||
}
|
||||
|
||||
// CategoryGetReq 功能获取请求参数
|
||||
type CategoryGetReq struct {
|
||||
Id int `uri:"id"`
|
||||
}
|
||||
|
||||
func (s *CategoryGetReq) GetId() interface{} {
|
||||
return s.Id
|
||||
}
|
||||
|
||||
// CategoryDeleteReq 功能删除请求参数
|
||||
type CategoryDeleteReq struct {
|
||||
Ids []int `json:"ids"`
|
||||
}
|
||||
|
||||
func (s *CategoryDeleteReq) GetId() interface{} {
|
||||
return s.Ids
|
||||
}
|
||||
Reference in new issue
Block a user