mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-05-10 03:58:05 +08:00
完善主题创建/删除
This commit is contained in:
parent
7392e4c26a
commit
5a74dae08a
@ -11,6 +11,7 @@ import (
|
||||
"gofiber.study.skcks.cn/common/utils"
|
||||
"gofiber.study.skcks.cn/controller"
|
||||
"gofiber.study.skcks.cn/global"
|
||||
"gofiber.study.skcks.cn/middleware"
|
||||
"gofiber.study.skcks.cn/services"
|
||||
)
|
||||
|
||||
@ -32,6 +33,7 @@ func Run() {
|
||||
reloadNanoId(global.Config)
|
||||
|
||||
global.App = reloadApp(global.Config)
|
||||
middleware.NewCasbin(global.App, global.DataSources)
|
||||
|
||||
if err := global.App.Listen(global.Config.Server.Addr); err != nil {
|
||||
logger.Log.Fatalf("[x] [Fiber] 致命错误: %s", err)
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"gofiber.study.skcks.cn/common/logger"
|
||||
"gofiber.study.skcks.cn/common/utils"
|
||||
"gofiber.study.skcks.cn/global"
|
||||
"gofiber.study.skcks.cn/middleware"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
@ -29,6 +28,4 @@ func reloadDataSources(c *config.BasicConfig) {
|
||||
if c.Mysql.AutoSync && !fiber.IsChild() {
|
||||
global.SyncModelToDataSource()
|
||||
}
|
||||
|
||||
middleware.NewCasbin(global.DataSources)
|
||||
}
|
||||
|
@ -52,6 +52,38 @@ func (c *Controller) CreateTopical() {
|
||||
return ctx.JSON(errorx.ParseError(err))
|
||||
}
|
||||
|
||||
return ctx.JSON(response.NewResponse(""))
|
||||
return ctx.JSON(response.NewResponse("主题创建成功"))
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteTopical 删除主题
|
||||
//
|
||||
// @Summary 删除主题
|
||||
// @Description 删除主题
|
||||
// @Tags Topical
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param vo query topical.DeleteTopicalDTO true "DeleteTopicalDTO"
|
||||
// @Success 200 {object} response.Response{data=string}
|
||||
// @Failure default {object} errorx.CodeErrorResponse
|
||||
// @Router /topical/delete [delete]
|
||||
// @Security Token
|
||||
func (c *Controller) DeleteTopical() {
|
||||
c.Router.Delete("/delete", func(ctx *fiber.Ctx) error {
|
||||
var dto = &topical2.DeleteTopicalDTO{}
|
||||
|
||||
_ = ctx.QueryParser(dto)
|
||||
|
||||
err := global.ValidateStruct(dto)
|
||||
if err != nil {
|
||||
return ctx.JSON(errorx.ParseError(err))
|
||||
}
|
||||
|
||||
err = topical.Services.DeleteTopical(*dto)
|
||||
if err != nil {
|
||||
return ctx.JSON(errorx.ParseError(err))
|
||||
}
|
||||
|
||||
return ctx.JSON(response.NewResponse("主题删除成功"))
|
||||
})
|
||||
}
|
||||
|
53
docs/docs.go
53
docs/docs.go
@ -865,6 +865,59 @@ const docTemplate = `{
|
||||
}
|
||||
}
|
||||
},
|
||||
"/topical/delete": {
|
||||
"delete": {
|
||||
"security": [
|
||||
{
|
||||
"Token": []
|
||||
}
|
||||
],
|
||||
"description": "删除主题",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"Topical"
|
||||
],
|
||||
"summary": "删除主题",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"name": "id",
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/response.Response"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/user/account": {
|
||||
"get": {
|
||||
"security": [
|
||||
|
@ -857,6 +857,59 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/topical/delete": {
|
||||
"delete": {
|
||||
"security": [
|
||||
{
|
||||
"Token": []
|
||||
}
|
||||
],
|
||||
"description": "删除主题",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"Topical"
|
||||
],
|
||||
"summary": "删除主题",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"name": "id",
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/response.Response"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/user/account": {
|
||||
"get": {
|
||||
"security": [
|
||||
|
@ -654,6 +654,36 @@ paths:
|
||||
summary: 创建主题
|
||||
tags:
|
||||
- Topical
|
||||
/topical/delete:
|
||||
delete:
|
||||
consumes:
|
||||
- application/json
|
||||
description: 删除主题
|
||||
parameters:
|
||||
- in: query
|
||||
name: id
|
||||
type: string
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
allOf:
|
||||
- $ref: '#/definitions/response.Response'
|
||||
- properties:
|
||||
data:
|
||||
type: string
|
||||
type: object
|
||||
default:
|
||||
description: ""
|
||||
schema:
|
||||
$ref: '#/definitions/errorx.CodeErrorResponse'
|
||||
security:
|
||||
- Token: []
|
||||
summary: 删除主题
|
||||
tags:
|
||||
- Topical
|
||||
/user/account:
|
||||
get:
|
||||
consumes:
|
||||
|
@ -20,7 +20,7 @@ const (
|
||||
CasBinSeparator = "::"
|
||||
)
|
||||
|
||||
func NewCasbin(engine *xorm.EngineGroup) {
|
||||
func NewCasbin(app *fiber.App, engine *xorm.EngineGroup) {
|
||||
casbinModels := make([]models.CasbinModel, 0)
|
||||
err := engine.Find(&casbinModels)
|
||||
if err != nil {
|
||||
@ -48,7 +48,7 @@ func NewCasbin(engine *xorm.EngineGroup) {
|
||||
}
|
||||
|
||||
// 初始化 role::root 组权限
|
||||
initRootGroupPermission(e)
|
||||
initRootGroupPermission(app, e)
|
||||
|
||||
global.Enforcer = e
|
||||
|
||||
@ -57,10 +57,15 @@ func NewCasbin(engine *xorm.EngineGroup) {
|
||||
})
|
||||
}
|
||||
|
||||
func initRootGroupPermission(e *casbin.Enforcer) {
|
||||
func initRootGroupPermission(app *fiber.App, e *casbin.Enforcer) {
|
||||
// 添加/修改 组策略
|
||||
// role::root 组 继承 root 的权限
|
||||
_, _ = e.AddGroupingPolicy("role::root", "user::root")
|
||||
for _, routes := range app.Stack() {
|
||||
for _, route := range routes {
|
||||
_, _ = e.AddPolicy("role::root", DefaultSystem, route.Path, route.Method)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CasbinMiddleWare(c *fiber.Ctx) error {
|
||||
|
@ -1,6 +1,10 @@
|
||||
package topical
|
||||
|
||||
type CreateTopicalDTO struct {
|
||||
Title string
|
||||
Content string
|
||||
Title string `json:"title" validate:"required"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
}
|
||||
|
||||
type DeleteTopicalDTO struct {
|
||||
Id string `json:"id" example:"123456" validate:"required"`
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
package topical
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gofiber.study.skcks.cn/global"
|
||||
"gofiber.study.skcks.cn/logger"
|
||||
"gofiber.study.skcks.cn/model/generic/models"
|
||||
"gofiber.study.skcks.cn/model/topical"
|
||||
"strconv"
|
||||
@ -16,6 +18,10 @@ func InitService() {
|
||||
Services = &Service{}
|
||||
}
|
||||
|
||||
var (
|
||||
NotExist = errors.New("主题不存在")
|
||||
)
|
||||
|
||||
func (s *Service) CreateTopical(dto topical.CreateTopicalDTO, userId string) error {
|
||||
id, _ := global.SonyFlake.NextID()
|
||||
_, err := global.DataSources.Insert(&models.Topical{
|
||||
@ -33,3 +39,27 @@ func (s *Service) CreateTopical(dto topical.CreateTopicalDTO, userId string) err
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) DeleteTopical(dto topical.DeleteTopicalDTO) error {
|
||||
t := &models.Topical{
|
||||
Id: dto.Id,
|
||||
}
|
||||
|
||||
logger.Logger().Infof("%#v", t)
|
||||
|
||||
exist, err := global.DataSources.Get(t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !exist {
|
||||
return NotExist
|
||||
}
|
||||
|
||||
_, err = global.DataSources.Limit(1).Delete(t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user