v0.1
This commit is contained in:
		| @@ -1,6 +1,7 @@ | ||||
| package handlers | ||||
|  | ||||
| import ( | ||||
| 	"strconv" | ||||
| 	"tyapi-server/internal/application/product" | ||||
| 	"tyapi-server/internal/application/product/dto/commands" | ||||
| 	"tyapi-server/internal/application/product/dto/queries" | ||||
| @@ -13,6 +14,7 @@ import ( | ||||
| // ProductHandler 产品相关HTTP处理器 | ||||
| type ProductHandler struct { | ||||
| 	appService      product.ProductApplicationService | ||||
| 	apiConfigService product.ProductApiConfigApplicationService | ||||
| 	categoryService product.CategoryApplicationService | ||||
| 	subAppService   product.SubscriptionApplicationService | ||||
| 	responseBuilder interfaces.ResponseBuilder | ||||
| @@ -23,6 +25,7 @@ type ProductHandler struct { | ||||
| // NewProductHandler 创建产品HTTP处理器 | ||||
| func NewProductHandler( | ||||
| 	appService product.ProductApplicationService, | ||||
| 	apiConfigService product.ProductApiConfigApplicationService, | ||||
| 	categoryService product.CategoryApplicationService, | ||||
| 	subAppService product.SubscriptionApplicationService, | ||||
| 	responseBuilder interfaces.ResponseBuilder, | ||||
| @@ -31,6 +34,7 @@ func NewProductHandler( | ||||
| ) *ProductHandler { | ||||
| 	return &ProductHandler{ | ||||
| 		appService:      appService, | ||||
| 		apiConfigService: apiConfigService, | ||||
| 		categoryService: categoryService, | ||||
| 		subAppService:   subAppService, | ||||
| 		responseBuilder: responseBuilder, | ||||
| @@ -49,8 +53,6 @@ func NewProductHandler( | ||||
| // @Param page_size query int false "每页数量" default(10) | ||||
| // @Param keyword query string false "搜索关键词" | ||||
| // @Param category_id query string false "分类ID" | ||||
| // @Param min_price query number false "最低价格" | ||||
| // @Param max_price query number false "最高价格" | ||||
| // @Param is_enabled query bool false "是否启用" | ||||
| // @Param is_visible query bool false "是否可见" | ||||
| // @Param is_package query bool false "是否组合包" | ||||
| @@ -61,23 +63,65 @@ func NewProductHandler( | ||||
| // @Failure 500 {object} map[string]interface{} "服务器内部错误" | ||||
| // @Router /api/v1/products [get] | ||||
| func (h *ProductHandler) ListProducts(c *gin.Context) { | ||||
| 	var query queries.ListProductsQuery | ||||
| 	if err := h.validator.ValidateQuery(c, &query); err != nil { | ||||
| 		return | ||||
| 	// 解析查询参数 | ||||
| 	page := h.getIntQuery(c, "page", 1) | ||||
| 	pageSize := h.getIntQuery(c, "page_size", 10) | ||||
|  | ||||
| 	// 构建筛选条件 | ||||
| 	filters := make(map[string]interface{}) | ||||
|  | ||||
| 	// 搜索关键词筛选 | ||||
| 	if keyword := c.Query("keyword"); keyword != "" { | ||||
| 		filters["keyword"] = keyword | ||||
| 	} | ||||
|  | ||||
| 	// 设置默认值 | ||||
| 	if query.Page <= 0 { | ||||
| 		query.Page = 1 | ||||
| 	} | ||||
| 	if query.PageSize <= 0 { | ||||
| 		query.PageSize = 10 | ||||
| 	} | ||||
| 	if query.PageSize > 100 { | ||||
| 		query.PageSize = 100 | ||||
| 	// 分类ID筛选 | ||||
| 	if categoryID := c.Query("category_id"); categoryID != "" { | ||||
| 		filters["category_id"] = categoryID | ||||
| 	} | ||||
|  | ||||
| 	result, err := h.appService.ListProducts(c.Request.Context(), &query) | ||||
| 	// 启用状态筛选 | ||||
| 	if isEnabled := c.Query("is_enabled"); isEnabled != "" { | ||||
| 		if enabled, err := strconv.ParseBool(isEnabled); err == nil { | ||||
| 			filters["is_enabled"] = enabled | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// 可见状态筛选 | ||||
| 	if isVisible := c.Query("is_visible"); isVisible != "" { | ||||
| 		if visible, err := strconv.ParseBool(isVisible); err == nil { | ||||
| 			filters["is_visible"] = visible | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// 产品类型筛选 | ||||
| 	if isPackage := c.Query("is_package"); isPackage != "" { | ||||
| 		if pkg, err := strconv.ParseBool(isPackage); err == nil { | ||||
| 			filters["is_package"] = pkg | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// 排序字段 | ||||
| 	sortBy := c.Query("sort_by") | ||||
| 	if sortBy == "" { | ||||
| 		sortBy = "created_at" | ||||
| 	} | ||||
|  | ||||
| 	// 排序方向 | ||||
| 	sortOrder := c.Query("sort_order") | ||||
| 	if sortOrder == "" { | ||||
| 		sortOrder = "desc" | ||||
| 	} | ||||
|  | ||||
| 	// 构建分页选项 | ||||
| 	options := interfaces.ListOptions{ | ||||
| 		Page:     page, | ||||
| 		PageSize: pageSize, | ||||
| 		Sort:     sortBy, | ||||
| 		Order:    sortOrder, | ||||
| 	} | ||||
|  | ||||
| 	result, err := h.appService.ListProducts(c.Request.Context(), filters, options) | ||||
| 	if err != nil { | ||||
| 		h.logger.Error("获取产品列表失败", zap.Error(err)) | ||||
| 		h.responseBuilder.InternalError(c, "获取产品列表失败") | ||||
| @@ -87,6 +131,16 @@ func (h *ProductHandler) ListProducts(c *gin.Context) { | ||||
| 	h.responseBuilder.Success(c, result, "获取产品列表成功") | ||||
| } | ||||
|  | ||||
| // getIntQuery 获取整数查询参数 | ||||
| func (h *ProductHandler) getIntQuery(c *gin.Context, key string, defaultValue int) int { | ||||
| 	if value := c.Query(key); value != "" { | ||||
| 		if intValue, err := strconv.Atoi(value); err == nil && intValue > 0 { | ||||
| 			return intValue | ||||
| 		} | ||||
| 	} | ||||
| 	return defaultValue | ||||
| } | ||||
|  | ||||
| // GetProductDetail 获取产品详情 | ||||
| // @Summary 获取产品详情 | ||||
| // @Description 根据产品ID获取产品详细信息 | ||||
| @@ -176,6 +230,62 @@ func (h *ProductHandler) GetProductStats(c *gin.Context) { | ||||
| 	h.responseBuilder.Success(c, result, "获取产品统计成功") | ||||
| } | ||||
|  | ||||
| // GetProductApiConfig 获取产品API配置 | ||||
| // @Summary 获取产品API配置 | ||||
| // @Description 根据产品ID获取API配置信息 | ||||
| // @Tags 产品API配置 | ||||
| // @Accept json | ||||
| // @Produce json | ||||
| // @Param id path string true "产品ID" | ||||
| // @Success 200 {object} responses.ProductApiConfigResponse "获取成功" | ||||
| // @Failure 400 {object} interfaces.APIResponse "请求参数错误" | ||||
| // @Failure 404 {object} interfaces.APIResponse "配置不存在" | ||||
| // @Router /api/v1/products/{id}/api-config [get] | ||||
| func (h *ProductHandler) GetProductApiConfig(c *gin.Context) { | ||||
| 	productID := c.Param("id") | ||||
| 	if productID == "" { | ||||
| 		h.responseBuilder.BadRequest(c, "产品ID不能为空") | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	config, err := h.apiConfigService.GetProductApiConfig(c.Request.Context(), productID) | ||||
| 	if err != nil { | ||||
| 		h.logger.Error("获取产品API配置失败", zap.Error(err), zap.String("product_id", productID)) | ||||
| 		h.responseBuilder.NotFound(c, "产品API配置不存在") | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	h.responseBuilder.Success(c, config, "获取产品API配置成功") | ||||
| } | ||||
|  | ||||
| // GetProductApiConfigByCode 根据产品代码获取API配置 | ||||
| // @Summary 根据产品代码获取API配置 | ||||
| // @Description 根据产品代码获取API配置信息 | ||||
| // @Tags 产品API配置 | ||||
| // @Accept json | ||||
| // @Produce json | ||||
| // @Param product_code path string true "产品代码" | ||||
| // @Success 200 {object} responses.ProductApiConfigResponse "获取成功" | ||||
| // @Failure 400 {object} interfaces.APIResponse "请求参数错误" | ||||
| // @Failure 404 {object} interfaces.APIResponse "配置不存在" | ||||
| // @Router /api/v1/products/code/{product_code}/api-config [get] | ||||
| func (h *ProductHandler) GetProductApiConfigByCode(c *gin.Context) { | ||||
| 	productCode := c.Param("product_code") | ||||
| 	if productCode == "" { | ||||
| 		h.responseBuilder.BadRequest(c, "产品代码不能为空") | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	config, err := h.apiConfigService.GetProductApiConfigByCode(c.Request.Context(), productCode) | ||||
| 	if err != nil { | ||||
| 		h.logger.Error("根据产品代码获取API配置失败", zap.Error(err), zap.String("product_code", productCode)) | ||||
| 		h.responseBuilder.NotFound(c, "产品API配置不存在") | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	h.responseBuilder.Success(c, config, "获取产品API配置成功") | ||||
| } | ||||
|  | ||||
| // ================ 分类相关方法 ================ | ||||
|  | ||||
| // ListCategories 获取分类列表 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user