重构dto
This commit is contained in:
@@ -5,60 +5,13 @@ import (
|
||||
"strconv"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// --- 数据传输对象 (DTOs) ---
|
||||
|
||||
// PigHouseResponse 定义了猪舍信息的响应结构
|
||||
type PigHouseResponse struct {
|
||||
ID uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// PenResponse 定义了猪栏信息的响应结构
|
||||
type PenResponse struct {
|
||||
ID uint `json:"id"`
|
||||
PenNumber string `json:"pen_number"`
|
||||
HouseID uint `json:"house_id"`
|
||||
Capacity int `json:"capacity"`
|
||||
Status models.PenStatus `json:"status"`
|
||||
PigBatchID uint `json:"pig_batch_id"`
|
||||
}
|
||||
|
||||
// CreatePigHouseRequest 定义了创建猪舍的请求结构
|
||||
type CreatePigHouseRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// UpdatePigHouseRequest 定义了更新猪舍的请求结构
|
||||
type UpdatePigHouseRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// CreatePenRequest 定义了创建猪栏的请求结构
|
||||
type CreatePenRequest struct {
|
||||
PenNumber string `json:"pen_number" binding:"required"`
|
||||
HouseID uint `json:"house_id" binding:"required"`
|
||||
Capacity int `json:"capacity" binding:"required"`
|
||||
Status models.PenStatus `json:"status" binding:"required"`
|
||||
}
|
||||
|
||||
// UpdatePenRequest 定义了更新猪栏的请求结构
|
||||
type UpdatePenRequest struct {
|
||||
PenNumber string `json:"pen_number" binding:"required"`
|
||||
HouseID uint `json:"house_id" binding:"required"`
|
||||
Capacity int `json:"capacity" binding:"required"`
|
||||
Status models.PenStatus `json:"status" binding:"required"`
|
||||
}
|
||||
|
||||
// --- 控制器定义 ---
|
||||
|
||||
// PigFarmController 负责处理猪舍和猪栏相关的API请求
|
||||
@@ -83,12 +36,12 @@ func NewPigFarmController(logger *logs.Logger, service service.PigFarmService) *
|
||||
// @Tags 猪场管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body CreatePigHouseRequest true "猪舍信息"
|
||||
// @Success 201 {object} controller.Response{data=PigHouseResponse} "创建成功"
|
||||
// @Param body body dto.CreatePigHouseRequest true "猪舍信息"
|
||||
// @Success 201 {object} controller.Response{data=dto.PigHouseResponse} "创建成功"
|
||||
// @Router /api/v1/pighouses [post]
|
||||
func (c *PigFarmController) CreatePigHouse(ctx *gin.Context) {
|
||||
const action = "创建猪舍"
|
||||
var req CreatePigHouseRequest
|
||||
var req dto.CreatePigHouseRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
@@ -101,7 +54,7 @@ func (c *PigFarmController) CreatePigHouse(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp := PigHouseResponse{
|
||||
resp := dto.PigHouseResponse{
|
||||
ID: house.ID,
|
||||
Name: house.Name,
|
||||
Description: house.Description,
|
||||
@@ -115,7 +68,7 @@ func (c *PigFarmController) CreatePigHouse(ctx *gin.Context) {
|
||||
// @Tags 猪场管理
|
||||
// @Produce json
|
||||
// @Param id path int true "猪舍ID"
|
||||
// @Success 200 {object} controller.Response{data=PigHouseResponse} "获取成功"
|
||||
// @Success 200 {object} controller.Response{data=dto.PigHouseResponse} "获取成功"
|
||||
// @Router /api/v1/pighouses/{id} [get]
|
||||
func (c *PigFarmController) GetPigHouse(ctx *gin.Context) {
|
||||
const action = "获取猪舍"
|
||||
@@ -136,7 +89,7 @@ func (c *PigFarmController) GetPigHouse(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp := PigHouseResponse{
|
||||
resp := dto.PigHouseResponse{
|
||||
ID: house.ID,
|
||||
Name: house.Name,
|
||||
Description: house.Description,
|
||||
@@ -149,7 +102,7 @@ func (c *PigFarmController) GetPigHouse(ctx *gin.Context) {
|
||||
// @Description 获取所有猪舍的列表
|
||||
// @Tags 猪场管理
|
||||
// @Produce json
|
||||
// @Success 200 {object} controller.Response{data=[]PigHouseResponse} "获取成功"
|
||||
// @Success 200 {object} controller.Response{data=[]dto.PigHouseResponse} "获取成功"
|
||||
// @Router /api/v1/pighouses [get]
|
||||
func (c *PigFarmController) ListPigHouses(ctx *gin.Context) {
|
||||
const action = "获取猪舍列表"
|
||||
@@ -160,9 +113,9 @@ func (c *PigFarmController) ListPigHouses(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var resp []PigHouseResponse
|
||||
var resp []dto.PigHouseResponse
|
||||
for _, house := range houses {
|
||||
resp = append(resp, PigHouseResponse{
|
||||
resp = append(resp, dto.PigHouseResponse{
|
||||
ID: house.ID,
|
||||
Name: house.Name,
|
||||
Description: house.Description,
|
||||
@@ -179,8 +132,8 @@ func (c *PigFarmController) ListPigHouses(ctx *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪舍ID"
|
||||
// @Param body body UpdatePigHouseRequest true "猪舍信息"
|
||||
// @Success 200 {object} controller.Response{data=PigHouseResponse} "更新成功"
|
||||
// @Param body body dto.UpdatePigHouseRequest true "猪舍信息"
|
||||
// @Success 200 {object} controller.Response{data=dto.PigHouseResponse} "更新成功"
|
||||
// @Router /api/v1/pighouses/{id} [put]
|
||||
func (c *PigFarmController) UpdatePigHouse(ctx *gin.Context) {
|
||||
const action = "更新猪舍"
|
||||
@@ -190,7 +143,7 @@ func (c *PigFarmController) UpdatePigHouse(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var req UpdatePigHouseRequest
|
||||
var req dto.UpdatePigHouseRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
@@ -207,7 +160,7 @@ func (c *PigFarmController) UpdatePigHouse(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp := PigHouseResponse{
|
||||
resp := dto.PigHouseResponse{
|
||||
ID: house.ID,
|
||||
Name: house.Name,
|
||||
Description: house.Description,
|
||||
@@ -252,12 +205,12 @@ func (c *PigFarmController) DeletePigHouse(ctx *gin.Context) {
|
||||
// @Tags 猪场管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body CreatePenRequest true "猪栏信息"
|
||||
// @Success 201 {object} controller.Response{data=PenResponse} "创建成功"
|
||||
// @Param body body dto.CreatePenRequest true "猪栏信息"
|
||||
// @Success 201 {object} controller.Response{data=dto.PenResponse} "创建成功"
|
||||
// @Router /api/v1/pens [post]
|
||||
func (c *PigFarmController) CreatePen(ctx *gin.Context) {
|
||||
const action = "创建猪栏"
|
||||
var req CreatePenRequest
|
||||
var req dto.CreatePenRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
@@ -270,7 +223,7 @@ func (c *PigFarmController) CreatePen(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp := PenResponse{
|
||||
resp := dto.PenResponse{
|
||||
ID: pen.ID,
|
||||
PenNumber: pen.PenNumber,
|
||||
HouseID: pen.HouseID,
|
||||
@@ -287,7 +240,7 @@ func (c *PigFarmController) CreatePen(ctx *gin.Context) {
|
||||
// @Tags 猪场管理
|
||||
// @Produce json
|
||||
// @Param id path int true "猪栏ID"
|
||||
// @Success 200 {object} controller.Response{data=PenResponse} "获取成功"
|
||||
// @Success 200 {object} controller.Response{data=dto.PenResponse} "获取成功"
|
||||
// @Router /api/v1/pens/{id} [get]
|
||||
func (c *PigFarmController) GetPen(ctx *gin.Context) {
|
||||
const action = "获取猪栏"
|
||||
@@ -308,7 +261,7 @@ func (c *PigFarmController) GetPen(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp := PenResponse{
|
||||
resp := dto.PenResponse{
|
||||
ID: pen.ID,
|
||||
PenNumber: pen.PenNumber,
|
||||
HouseID: pen.HouseID,
|
||||
@@ -324,7 +277,7 @@ func (c *PigFarmController) GetPen(ctx *gin.Context) {
|
||||
// @Description 获取所有猪栏的列表
|
||||
// @Tags 猪场管理
|
||||
// @Produce json
|
||||
// @Success 200 {object} controller.Response{data=[]PenResponse} "获取成功"
|
||||
// @Success 200 {object} controller.Response{data=[]dto.PenResponse} "获取成功"
|
||||
// @Router /api/v1/pens [get]
|
||||
func (c *PigFarmController) ListPens(ctx *gin.Context) {
|
||||
const action = "获取猪栏列表"
|
||||
@@ -335,9 +288,9 @@ func (c *PigFarmController) ListPens(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var resp []PenResponse
|
||||
var resp []dto.PenResponse
|
||||
for _, pen := range pens {
|
||||
resp = append(resp, PenResponse{
|
||||
resp = append(resp, dto.PenResponse{
|
||||
ID: pen.ID,
|
||||
PenNumber: pen.PenNumber,
|
||||
HouseID: pen.HouseID,
|
||||
@@ -357,8 +310,8 @@ func (c *PigFarmController) ListPens(ctx *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪栏ID"
|
||||
// @Param body body UpdatePenRequest true "猪栏信息"
|
||||
// @Success 200 {object} controller.Response{data=PenResponse} "更新成功"
|
||||
// @Param body body dto.UpdatePenRequest true "猪栏信息"
|
||||
// @Success 200 {object} controller.Response{data=dto.PenResponse} "更新成功"
|
||||
// @Router /api/v1/pens/{id} [put]
|
||||
func (c *PigFarmController) UpdatePen(ctx *gin.Context) {
|
||||
const action = "更新猪栏"
|
||||
@@ -368,7 +321,7 @@ func (c *PigFarmController) UpdatePen(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var req UpdatePenRequest
|
||||
var req dto.UpdatePenRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
@@ -385,7 +338,7 @@ func (c *PigFarmController) UpdatePen(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp := PenResponse{
|
||||
resp := dto.PenResponse{
|
||||
ID: pen.ID,
|
||||
PenNumber: pen.PenNumber,
|
||||
HouseID: pen.HouseID,
|
||||
|
||||
Reference in New Issue
Block a user