Files
nxxmdata/backend/routes/pens.js
2025-09-12 20:08:42 +08:00

353 lines
9.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 栏舍管理路由
* @file pens.js
* @description 定义栏舍管理相关的API路由
*/
const express = require('express');
const router = express.Router();
const penController = require('../controllers/penController');
const { verifyToken } = require('../middleware/auth');
// 公开API路由不需要验证token
const publicRoutes = express.Router();
router.use('/public', publicRoutes);
// 公开获取栏舍列表
publicRoutes.get('/', penController.getPens);
// 公开获取栏舍详情
publicRoutes.get('/:id', penController.getPenById);
// 公开获取栏舍统计信息
publicRoutes.get('/stats/summary', penController.getPenStats);
// 需要认证的路由
router.use(verifyToken);
// 创建栏舍
router.post('/', penController.createPen);
// 更新栏舍
router.put('/:id', penController.updatePen);
// 删除栏舍
router.delete('/:id', penController.deletePen);
// 批量删除栏舍
router.delete('/batch', penController.batchDeletePens);
/**
* @swagger
* tags:
* name: Pens
* description: 栏舍管理API
*/
/**
* @swagger
* /api/pens/public:
* get:
* summary: 获取栏舍列表
* tags: [Pens]
* parameters:
* - in: query
* name: page
* schema:
* type: integer
* default: 1
* description: 页码
* - in: query
* name: pageSize
* schema:
* type: integer
* default: 10
* description: 每页数量
* - in: query
* name: search
* schema:
* type: string
* description: 搜索关键词
* - in: query
* name: animalType
* schema:
* type: string
* enum: [马, 牛, 羊, 家禽, 猪]
* description: 动物类型
* - in: query
* name: status
* schema:
* type: string
* enum: [true, false]
* description: 状态
* - in: query
* name: farmId
* schema:
* type: integer
* description: 农场ID
* responses:
* 200:
* description: 成功获取栏舍列表
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* data:
* type: object
* properties:
* list:
* type: array
* items:
* $ref: '#/components/schemas/Pen'
* pagination:
* $ref: '#/components/schemas/Pagination'
*/
/**
* @swagger
* /api/pens/public/{id}:
* get:
* summary: 获取栏舍详情
* tags: [Pens]
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: 栏舍ID
* responses:
* 200:
* description: 成功获取栏舍详情
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* data:
* $ref: '#/components/schemas/Pen'
* 404:
* description: 栏舍不存在
*/
/**
* @swagger
* /api/pens:
* post:
* summary: 创建栏舍
* tags: [Pens]
* security:
* - bearerAuth: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - name
* - animal_type
* - responsible
* - capacity
* properties:
* name:
* type: string
* description: 栏舍名称
* animal_type:
* type: string
* enum: [马, 牛, 羊, 家禽, 猪]
* description: 动物类型
* pen_type:
* type: string
* description: 栏舍类型
* responsible:
* type: string
* description: 负责人
* capacity:
* type: integer
* description: 容量
* status:
* type: boolean
* default: true
* description: 状态
* description:
* type: string
* description: 备注信息
* farm_id:
* type: integer
* description: 所属农场ID
* responses:
* 201:
* description: 栏舍创建成功
* 400:
* description: 请求参数错误
* 500:
* description: 服务器内部错误
*/
/**
* @swagger
* /api/pens/{id}:
* put:
* summary: 更新栏舍
* tags: [Pens]
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: 栏舍ID
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* name:
* type: string
* description: 栏舍名称
* animal_type:
* type: string
* enum: [马, 牛, 羊, 家禽, 猪]
* description: 动物类型
* pen_type:
* type: string
* description: 栏舍类型
* responsible:
* type: string
* description: 负责人
* capacity:
* type: integer
* description: 容量
* status:
* type: boolean
* description: 状态
* description:
* type: string
* description: 备注信息
* farm_id:
* type: integer
* description: 所属农场ID
* responses:
* 200:
* description: 栏舍更新成功
* 404:
* description: 栏舍不存在
* 500:
* description: 服务器内部错误
*/
/**
* @swagger
* /api/pens/{id}:
* delete:
* summary: 删除栏舍
* tags: [Pens]
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: 栏舍ID
* responses:
* 200:
* description: 栏舍删除成功
* 404:
* description: 栏舍不存在
* 500:
* description: 服务器内部错误
*/
/**
* @swagger
* /api/pens/batch:
* delete:
* summary: 批量删除栏舍
* tags: [Pens]
* security:
* - bearerAuth: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* ids:
* type: array
* items:
* type: integer
* description: 栏舍ID数组
* responses:
* 200:
* description: 批量删除成功
* 400:
* description: 请求参数错误
* 500:
* description: 服务器内部错误
*/
/**
* @swagger
* components:
* schemas:
* Pen:
* type: object
* properties:
* id:
* type: integer
* description: 栏舍ID
* name:
* type: string
* description: 栏舍名称
* animal_type:
* type: string
* enum: [马, 牛, 羊, 家禽, 猪]
* description: 动物类型
* pen_type:
* type: string
* description: 栏舍类型
* responsible:
* type: string
* description: 负责人
* capacity:
* type: integer
* description: 容量
* status:
* type: boolean
* description: 状态
* description:
* type: string
* description: 备注信息
* farm_id:
* type: integer
* description: 所属农场ID
* creator:
* type: string
* description: 创建人
* created_at:
* type: string
* format: date-time
* description: 创建时间
* updated_at:
* type: string
* format: date-time
* description: 更新时间
* farm:
* $ref: '#/components/schemas/Farm'
*/
module.exports = router;