bot 类型 列表查询

This commit is contained in:
Shikong 2023-02-27 17:18:31 +08:00
parent 43b14c65ad
commit a509d108d4
9 changed files with 321 additions and 7 deletions

View File

@ -95,4 +95,5 @@ fabric.properties
.idea/caches/build_file_checksums.ser
config.toml
.$database.drawio.*
.$database.drawio.*
/tmp/build-errors.log

View File

@ -22,7 +22,99 @@ const docTemplate = `{
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {}
"paths": {
"/bot/type/list": {
"get": {
"description": "获取 bot 类型列表",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"BotType"
],
"summary": "获取 bot 类型列表",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/resp.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/model.BotType"
}
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
}
}
},
"definitions": {
"errorx.CodeErrorResponse": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"example": 200
},
"data": {},
"msg": {
"type": "string",
"example": "OK"
}
}
},
"model.BotType": {
"type": "object",
"properties": {
"active": {
"description": "是否启用",
"type": "boolean"
},
"id": {
"description": "id",
"type": "string"
},
"name": {
"description": "名称",
"type": "string"
}
}
},
"resp.Response": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"example": 200
},
"data": {},
"msg": {
"type": "string",
"example": "OK"
}
}
}
}
}`
// SwaggerInfo holds exported Swagger Info so clients can modify it

View File

@ -15,5 +15,97 @@
"version": "1.0"
},
"basePath": "/",
"paths": {}
"paths": {
"/bot/type/list": {
"get": {
"description": "获取 bot 类型列表",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"BotType"
],
"summary": "获取 bot 类型列表",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/resp.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/model.BotType"
}
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
}
}
},
"definitions": {
"errorx.CodeErrorResponse": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"example": 200
},
"data": {},
"msg": {
"type": "string",
"example": "OK"
}
}
},
"model.BotType": {
"type": "object",
"properties": {
"active": {
"description": "是否启用",
"type": "boolean"
},
"id": {
"description": "id",
"type": "string"
},
"name": {
"description": "名称",
"type": "string"
}
}
},
"resp.Response": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"example": 200
},
"data": {},
"msg": {
"type": "string",
"example": "OK"
}
}
}
}
}

View File

@ -1,4 +1,37 @@
basePath: /
definitions:
errorx.CodeErrorResponse:
properties:
code:
example: 200
type: integer
data: {}
msg:
example: OK
type: string
type: object
model.BotType:
properties:
active:
description: 是否启用
type: boolean
id:
description: id
type: string
name:
description: 名称
type: string
type: object
resp.Response:
properties:
code:
example: 200
type: integer
data: {}
msg:
example: OK
type: string
type: object
info:
contact:
email: 919411476@qq.com
@ -10,5 +43,31 @@ info:
termsOfService: http://swagger.io/terms/
title: matrix-middle-service API
version: "1.0"
paths: {}
paths:
/bot/type/list:
get:
consumes:
- application/json
description: 获取 bot 类型列表
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/resp.Response'
- properties:
data:
items:
$ref: '#/definitions/model.BotType'
type: array
type: object
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: 获取 bot 类型列表
tags:
- BotType
swagger: "2.0"

View File

@ -0,0 +1,32 @@
package controller
import (
"github.com/gofiber/fiber/v2"
"matrix-middle-service/pkg/database"
response "matrix-middle-service/pkg/resp"
"matrix-middle-service/pkg/resp/errorx"
)
func SetupBotController(r fiber.Router) {
api := r.Group("/bot")
api.Get("/type/list", GetBotTypeList)
}
// GetBotTypeList
// @Summary 获取 bot 类型列表
// @Description 获取 bot 类型列表
// @Tags BotType
// @Accept json
// @Produce json
// @Success 200 {object} response.Response{data=[]model.BotType}
// @Failure default {object} errorx.CodeErrorResponse
// @Router /bot/type/list [get]
func GetBotTypeList(ctx *fiber.Ctx) error {
return database.Exec(func(db database.DataBase) error {
list, err := db.GetAllBotTypes()
if err = errorx.ParseError(err); err != nil {
return ctx.JSON(err)
}
return ctx.JSON(response.NewResponse(list))
})
}

View File

@ -2,6 +2,7 @@ package route
import (
"github.com/gofiber/fiber/v2"
"matrix-middle-service/internel/controller"
"matrix-middle-service/pkg/config"
)
@ -9,4 +10,6 @@ func SetupRoute(app *fiber.App, conf *config.Conf) {
if conf.Server.EnableSwag {
SwaggerHandler(app)
}
controller.SetupBotController(app)
}

View File

@ -3,6 +3,7 @@ package database
import (
"github.com/mitchellh/mapstructure"
"matrix-middle-service/pkg/config/database"
"matrix-middle-service/pkg/database/model"
"matrix-middle-service/pkg/logger"
)
@ -14,6 +15,8 @@ type InitDSFn = func(conf database.DataSourcesType[any]) DataBase
type DataBase interface {
Open() error
Close() error
GetAllBotTypes() ([]*model.BotType, error)
}
func Init(conf []interface{}) {
@ -51,6 +54,29 @@ func Open() (err error) {
return
}
func Exec(fn func(db DataBase) error) error {
if len(drivers) > 0 {
return fn(drivers[0])
} else {
logger.Log().Error("[database] 没有可用的数据源")
}
return nil
}
func ExecMulti(fn func(db DataBase) error) error {
if len(drivers) == 0 {
logger.Log().Error("[database] 没有可用的数据源")
}
for _, driver := range drivers {
err := fn(driver)
if err != nil {
return err
}
}
return nil
}
func Close() {
for _, driver := range drivers {
_ = driver.Close()

View File

@ -53,6 +53,12 @@ type db struct {
config *conf.Config
}
func (d *db) GetAllBotTypes() ([]*model.BotType, error) {
list := make([]*model.BotType, 0)
err := d.db.Find(&list)
return list, err
}
func (d *db) Close() error {
logger.Log().Debug("[mysql] 关闭数据库连接")
return d.db.Close()

View File

@ -1,7 +1,10 @@
package model
type BotType struct {
Id string `xorm:"NOT NULL VARCHAR(255) pk"`
Name string `xorm:"NOT NULL VARCHAR(255)"`
Active bool `xorm:"NOT NULL BIT(1)"`
// id
Id string `json:"id" xorm:"NOT NULL VARCHAR(255) pk"`
// 名称
Name string `json:"name" xorm:"NOT NULL VARCHAR(255)"`
// 是否启用
Active bool `json:"active" xorm:"NOT NULL BIT(1)"`
}