定义接口

This commit is contained in:
2025-09-13 14:09:22 +08:00
parent 32e260ed73
commit 986bdf15a6
6 changed files with 900 additions and 4 deletions

View File

@@ -162,6 +162,211 @@ const docTemplate = `{
}
}
},
"/plans": {
"get": {
"description": "获取所有计划的列表",
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "获取计划列表",
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
},
"post": {
"description": "创建一个新的计划,包括其基本信息和所有关联的子计划/任务。",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "创建计划",
"parameters": [
{
"description": "计划信息",
"name": "plan",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/plan.CreatePlanRequest"
}
}
],
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
}
},
"/plans/{id}": {
"get": {
"description": "根据计划ID获取单个计划的详细信息。",
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "获取计划详情",
"parameters": [
{
"type": "integer",
"description": "计划ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 404, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
},
"put": {
"description": "根据计划ID更新计划的详细信息。",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "更新计划",
"parameters": [
{
"type": "integer",
"description": "计划ID",
"name": "id",
"in": "path",
"required": true
},
{
"description": "更新后的计划信息",
"name": "plan",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/plan.UpdatePlanRequest"
}
}
],
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 404, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
},
"delete": {
"description": "根据计划ID删除计划。",
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "删除计划",
"parameters": [
{
"type": "integer",
"description": "计划ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 404, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
}
},
"/plans/{id}/start": {
"post": {
"description": "根据计划ID启动一个计划的执行。",
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "启动计划",
"parameters": [
{
"type": "integer",
"description": "计划ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 404, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
}
},
"/plans/{id}/stop": {
"post": {
"description": "根据计划ID停止一个正在执行的计划。",
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "停止计划",
"parameters": [
{
"type": "integer",
"description": "计划ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 404, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
}
},
"/users": {
"post": {
"description": "根据用户名和密码创建一个新的系统用户。",
@@ -345,6 +550,67 @@ const docTemplate = `{
"DeviceTypeDevice"
]
},
"plan.CreatePlanRequest": {
"type": "object",
"required": [
"name"
],
"properties": {
"description": {
"type": "string",
"example": "根据温度自动调节风扇和加热器"
},
"name": {
"type": "string",
"example": "猪舍温度控制计划"
}
}
},
"plan.ListPlansResponse": {
"type": "object",
"properties": {
"plans": {
"type": "array",
"items": {
"$ref": "#/definitions/plan.PlanResponse"
}
},
"total": {
"type": "integer",
"example": 100
}
}
},
"plan.PlanResponse": {
"type": "object",
"properties": {
"description": {
"type": "string",
"example": "根据温度自动调节风扇和加热器"
},
"id": {
"type": "integer",
"example": 1
},
"name": {
"type": "string",
"example": "猪舍温度控制计划"
}
}
},
"plan.UpdatePlanRequest": {
"type": "object",
"properties": {
"description": {
"type": "string",
"example": "更新后的描述"
},
"name": {
"type": "string",
"example": "猪舍温度控制计划V2"
}
}
},
"user.CreateUserRequest": {
"type": "object",
"required": [

View File

@@ -151,6 +151,211 @@
}
}
},
"/plans": {
"get": {
"description": "获取所有计划的列表",
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "获取计划列表",
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
},
"post": {
"description": "创建一个新的计划,包括其基本信息和所有关联的子计划/任务。",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "创建计划",
"parameters": [
{
"description": "计划信息",
"name": "plan",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/plan.CreatePlanRequest"
}
}
],
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
}
},
"/plans/{id}": {
"get": {
"description": "根据计划ID获取单个计划的详细信息。",
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "获取计划详情",
"parameters": [
{
"type": "integer",
"description": "计划ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 404, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
},
"put": {
"description": "根据计划ID更新计划的详细信息。",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "更新计划",
"parameters": [
{
"type": "integer",
"description": "计划ID",
"name": "id",
"in": "path",
"required": true
},
{
"description": "更新后的计划信息",
"name": "plan",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/plan.UpdatePlanRequest"
}
}
],
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 404, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
},
"delete": {
"description": "根据计划ID删除计划。",
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "删除计划",
"parameters": [
{
"type": "integer",
"description": "计划ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 404, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
}
},
"/plans/{id}/start": {
"post": {
"description": "根据计划ID启动一个计划的执行。",
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "启动计划",
"parameters": [
{
"type": "integer",
"description": "计划ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 404, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
}
},
"/plans/{id}/stop": {
"post": {
"description": "根据计划ID停止一个正在执行的计划。",
"produces": [
"application/json"
],
"tags": [
"计划管理"
],
"summary": "停止计划",
"parameters": [
{
"type": "integer",
"description": "计划ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "业务失败具体错误码和信息见响应体例如400, 404, 500",
"schema": {
"$ref": "#/definitions/controller.Response"
}
}
}
}
},
"/users": {
"post": {
"description": "根据用户名和密码创建一个新的系统用户。",
@@ -334,6 +539,67 @@
"DeviceTypeDevice"
]
},
"plan.CreatePlanRequest": {
"type": "object",
"required": [
"name"
],
"properties": {
"description": {
"type": "string",
"example": "根据温度自动调节风扇和加热器"
},
"name": {
"type": "string",
"example": "猪舍温度控制计划"
}
}
},
"plan.ListPlansResponse": {
"type": "object",
"properties": {
"plans": {
"type": "array",
"items": {
"$ref": "#/definitions/plan.PlanResponse"
}
},
"total": {
"type": "integer",
"example": 100
}
}
},
"plan.PlanResponse": {
"type": "object",
"properties": {
"description": {
"type": "string",
"example": "根据温度自动调节风扇和加热器"
},
"id": {
"type": "integer",
"example": 1
},
"name": {
"type": "string",
"example": "猪舍温度控制计划"
}
}
},
"plan.UpdatePlanRequest": {
"type": "object",
"properties": {
"description": {
"type": "string",
"example": "更新后的描述"
},
"name": {
"type": "string",
"example": "猪舍温度控制计划V2"
}
}
},
"user.CreateUserRequest": {
"type": "object",
"required": [

View File

@@ -79,6 +79,48 @@ definitions:
x-enum-varnames:
- DeviceTypeAreaController
- DeviceTypeDevice
plan.CreatePlanRequest:
properties:
description:
example: 根据温度自动调节风扇和加热器
type: string
name:
example: 猪舍温度控制计划
type: string
required:
- name
type: object
plan.ListPlansResponse:
properties:
plans:
items:
$ref: '#/definitions/plan.PlanResponse'
type: array
total:
example: 100
type: integer
type: object
plan.PlanResponse:
properties:
description:
example: 根据温度自动调节风扇和加热器
type: string
id:
example: 1
type: integer
name:
example: 猪舍温度控制计划
type: string
type: object
plan.UpdatePlanRequest:
properties:
description:
example: 更新后的描述
type: string
name:
example: 猪舍温度控制计划V2
type: string
type: object
user.CreateUserRequest:
properties:
password:
@@ -225,6 +267,141 @@ paths:
summary: 更新设备信息
tags:
- 设备管理
/plans:
get:
description: 获取所有计划的列表
produces:
- application/json
responses:
"200":
description: 业务失败具体错误码和信息见响应体例如400, 500
schema:
$ref: '#/definitions/controller.Response'
summary: 获取计划列表
tags:
- 计划管理
post:
consumes:
- application/json
description: 创建一个新的计划,包括其基本信息和所有关联的子计划/任务。
parameters:
- description: 计划信息
in: body
name: plan
required: true
schema:
$ref: '#/definitions/plan.CreatePlanRequest'
produces:
- application/json
responses:
"200":
description: 业务失败具体错误码和信息见响应体例如400, 500
schema:
$ref: '#/definitions/controller.Response'
summary: 创建计划
tags:
- 计划管理
/plans/{id}:
delete:
description: 根据计划ID删除计划。
parameters:
- description: 计划ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: 业务失败具体错误码和信息见响应体例如400, 404, 500
schema:
$ref: '#/definitions/controller.Response'
summary: 删除计划
tags:
- 计划管理
get:
description: 根据计划ID获取单个计划的详细信息。
parameters:
- description: 计划ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: 业务失败具体错误码和信息见响应体例如400, 404, 500
schema:
$ref: '#/definitions/controller.Response'
summary: 获取计划详情
tags:
- 计划管理
put:
consumes:
- application/json
description: 根据计划ID更新计划的详细信息。
parameters:
- description: 计划ID
in: path
name: id
required: true
type: integer
- description: 更新后的计划信息
in: body
name: plan
required: true
schema:
$ref: '#/definitions/plan.UpdatePlanRequest'
produces:
- application/json
responses:
"200":
description: 业务失败具体错误码和信息见响应体例如400, 404, 500
schema:
$ref: '#/definitions/controller.Response'
summary: 更新计划
tags:
- 计划管理
/plans/{id}/start:
post:
description: 根据计划ID启动一个计划的执行。
parameters:
- description: 计划ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: 业务失败具体错误码和信息见响应体例如400, 404, 500
schema:
$ref: '#/definitions/controller.Response'
summary: 启动计划
tags:
- 计划管理
/plans/{id}/stop:
post:
description: 根据计划ID停止一个正在执行的计划。
parameters:
- description: 计划ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: 业务失败具体错误码和信息见响应体例如400, 404, 500
schema:
$ref: '#/definitions/controller.Response'
summary: 停止计划
tags:
- 计划管理
/users:
post:
consumes:

View File

@@ -17,6 +17,7 @@ import (
"time"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/device"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/plan"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/user"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service/token"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/config"
@@ -39,11 +40,12 @@ type API struct {
config config.ServerConfig // API 服务器的配置,使用 infra/config 包中的 ServerConfig
userController *user.Controller // 用户控制器实例
deviceController *device.Controller // 设备控制器实例
planController *plan.Controller // 计划控制器实例
}
// NewAPI 创建并返回一个新的 API 实例
// 负责初始化 Gin 引擎、设置全局中间件,并注入所有必要的依赖。
func NewAPI(cfg config.ServerConfig, logger *logs.Logger, userRepo repository.UserRepository, deviceRepository repository.DeviceRepository, tokenService token.TokenService) *API {
func NewAPI(cfg config.ServerConfig, logger *logs.Logger, userRepo repository.UserRepository, deviceRepository repository.DeviceRepository, planRepository repository.PlanRepository, tokenService token.TokenService) *API {
// 设置 Gin 模式,例如 gin.ReleaseMode (生产模式) 或 gin.DebugMode (开发模式)
// 从配置中获取 Gin 模式
gin.SetMode(cfg.Mode)
@@ -67,6 +69,8 @@ func NewAPI(cfg config.ServerConfig, logger *logs.Logger, userRepo repository.Us
userController: user.NewController(userRepo, logger, tokenService),
// 在 NewAPI 中初始化设备控制器,并将其作为 API 结构体的成员
deviceController: device.NewController(deviceRepository, logger),
// 在 NewAPI 中初始化计划控制器,并将其作为 API 结构体的成员
planController: plan.NewController(logger, planRepository),
}
api.setupRoutes() // 设置所有路由
@@ -95,6 +99,18 @@ func (a *API) setupRoutes() {
deviceGroup.PUT("/:id", a.deviceController.UpdateDevice)
deviceGroup.DELETE("/:id", a.deviceController.DeleteDevice)
}
// 计划相关路由组
planGroup := v1.Group("/plans")
{
planGroup.POST("", a.planController.CreatePlan)
planGroup.GET("", a.planController.ListPlans)
planGroup.GET("/:id", a.planController.GetPlan)
planGroup.PUT("/:id", a.planController.UpdatePlan)
planGroup.DELETE("/:id", a.planController.DeletePlan)
planGroup.POST("/:id/start", a.planController.StartPlan)
planGroup.POST("/:id/stop", a.planController.StopPlan)
}
}
// 添加 Swagger UI 路由

View File

@@ -0,0 +1,168 @@
package plan
import (
"net/http"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
"github.com/gin-gonic/gin"
)
// --- 请求和响应 DTO 定义 ---
// CreatePlanRequest 定义创建计划请求的结构体
type CreatePlanRequest struct {
Name string `json:"name" binding:"required" example:"猪舍温度控制计划"`
Description string `json:"description" example:"根据温度自动调节风扇和加热器"`
// 更多计划基本信息字段
// SubPlans 或 Tasks 可以在这里嵌套定义,以支持一次性创建
}
// PlanResponse 定义计划详情响应的结构体
type PlanResponse struct {
ID uint `json:"id" example:"1"`
Name string `json:"name" example:"猪舍温度控制计划"`
Description string `json:"description" example:"根据温度自动调节风扇和加热器"`
// 更多计划基本信息字段
// SubPlans 或 Tasks 也可以在这里返回
}
// ListPlansResponse 定义获取计划列表响应的结构体
type ListPlansResponse struct {
Plans []PlanResponse `json:"plans"`
Total int `json:"total" example:"100"`
}
// UpdatePlanRequest 定义更新计划请求的结构体
type UpdatePlanRequest struct {
Name string `json:"name" example:"猪舍温度控制计划V2"`
Description string `json:"description" example:"更新后的描述"`
// 更多可更新字段
}
// --- Controller 定义 ---
// Controller 定义了计划相关的控制器
type Controller struct {
logger *logs.Logger
planRepo repository.PlanRepository
}
// NewController 创建一个新的 Controller 实例
func NewController(logger *logs.Logger, planRepo repository.PlanRepository) *Controller {
return &Controller{
logger: logger,
planRepo: planRepo,
}
}
// --- 接口方法实现 ---
// CreatePlan godoc
// @Summary 创建计划
// @Description 创建一个新的计划,包括其基本信息和所有关联的子计划/任务。
// @Tags 计划管理
// @Accept json
// @Produce json
// @Param plan body CreatePlanRequest true "计划信息"
// @Success 200 {object} controller.Response{data=plan.PlanResponse} "业务码为201代表创建成功"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 500"
// @Router /plans [post]
func (pc *Controller) CreatePlan(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来创建计划
pc.logger.Infof("收到创建计划请求 (占位符)")
controller.SendResponse(c, http.StatusCreated, "创建计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"})
}
// GetPlan godoc
// @Summary 获取计划详情
// @Description 根据计划ID获取单个计划的详细信息。
// @Tags 计划管理
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response{data=plan.PlanResponse} "业务码为200代表成功获取"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id} [get]
func (pc *Controller) GetPlan(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来获取计划
pc.logger.Infof("收到获取计划 (占位符)")
controller.SendResponse(c, http.StatusOK, "获取计划接口占位符", ListPlansResponse{Plans: []PlanResponse{}, Total: 0})
}
// ListPlans godoc
// @Summary 获取计划列表
// @Description 获取所有计划的列表
// @Tags 计划管理
// @Produce json
// @Success 200 {object} controller.Response{data=plan.ListPlansResponse} "业务码为200代表成功获取列表"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 500"
// @Router /plans [get]
func (pc *Controller) ListPlans(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来获取计划列表
pc.logger.Infof("收到获取计划列表请求 (占位符)")
controller.SendResponse(c, http.StatusOK, "获取计划列表接口占位符", ListPlansResponse{Plans: []PlanResponse{}, Total: 0})
}
// UpdatePlan godoc
// @Summary 更新计划
// @Description 根据计划ID更新计划的详细信息。
// @Tags 计划管理
// @Accept json
// @Produce json
// @Param id path int true "计划ID"
// @Param plan body UpdatePlanRequest true "更新后的计划信息"
// @Success 200 {object} controller.Response{data=plan.PlanResponse} "业务码为200代表更新成功"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id} [put]
func (pc *Controller) UpdatePlan(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来更新计划
pc.logger.Infof("收到更新计划请求 (占位符)")
controller.SendResponse(c, http.StatusOK, "更新计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"})
}
// DeletePlan godoc
// @Summary 删除计划
// @Description 根据计划ID删除计划。
// @Tags 计划管理
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id} [delete]
func (pc *Controller) DeletePlan(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来删除计划
pc.logger.Infof("收到删除计划请求 (占位符)")
controller.SendResponse(c, http.StatusOK, "删除计划接口占位符", nil)
}
// StartPlan godoc
// @Summary 启动计划
// @Description 根据计划ID启动一个计划的执行。
// @Tags 计划管理
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response "业务码为200代表成功启动计划"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id}/start [post]
func (pc *Controller) StartPlan(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来启动计划
pc.logger.Infof("收到启动计划请求 (占位符)")
controller.SendResponse(c, http.StatusOK, "启动计划接口占位符", nil)
}
// StopPlan godoc
// @Summary 停止计划
// @Description 根据计划ID停止一个正在执行的计划。
// @Tags 计划管理
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response "业务码为200代表成功停止计划"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id}/stop [post]
func (pc *Controller) StopPlan(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来停止计划
pc.logger.Infof("收到停止计划请求 (占位符)")
controller.SendResponse(c, http.StatusOK, "停止计划接口占位符", nil)
}

View File

@@ -55,10 +55,13 @@ func NewApplication(configPath string) (*Application, error) {
// 7. 初始化设备仓库
deviceRepo := repository.NewGormDeviceRepository(storage.GetDB())
// 8. 初始化 API 服务器
apiServer := api.NewAPI(cfg.Server, logger, userRepo, deviceRepo, tokenService)
// 8. 初始化计划仓库
planRepo := repository.NewGormPlanRepository(storage.GetDB())
// 9. 组装 Application 对象
// 9. 初始化 API 服务器
apiServer := api.NewAPI(cfg.Server, logger, userRepo, deviceRepo, planRepo, tokenService)
// 10. 组装 Application 对象
app := &Application{
Config: cfg,
Logger: logger,