refactor(store): 提取URL生成函数避免重复代码
- 将mustGenerateUrl函数提取到category_handler.go中- 在product_handler.go中移除重复的mustGenerateUrl函数实现 - 更新createCategoryUrlFunction和createPageUrlFunction调用方式 - 统一处理URL生成失败的情况,避免忽略错误返回值
This commit is contained in:
Binary file not shown.
@@ -27,7 +27,14 @@ func (handler CategoryHandler) GetButtons(selected int) actionresults.ActionResu
|
||||
|
||||
func (handler CategoryHandler) createCategoryUrlFunction() func(int) string {
|
||||
return func(category int) string {
|
||||
url, _ := handler.URLGenerator.GenerateUrl(ProductHandler.GetProducts, category, 1)
|
||||
return url
|
||||
return mustGenerateUrl(handler.URLGenerator, ProductHandler.GetProducts, category, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func mustGenerateUrl(generator handing.URLGenerator, method interface{}, data ...interface{}) string {
|
||||
url, err := generator.GenerateUrl(method, data...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
@@ -42,8 +42,7 @@ func (handler ProductHandler) GetProducts(category, page int) actionresults.Acti
|
||||
|
||||
func (handler ProductHandler) createPageUrlFunction(category int) func(int) string {
|
||||
return func(page int) string {
|
||||
url, _ := handler.URLGenerator.GenerateUrl(ProductHandler.GetProducts, category, page)
|
||||
return url
|
||||
return mustGenerateUrl(handler.URLGenerator, category, page)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,11 +53,3 @@ func (handler ProductHandler) generatePageNumbers(pageCount int) (pages []int) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func mustGenerateUrl(generator handing.URLGenerator, method interface{}, data ...interface{}) string {
|
||||
url, err := generator.GenerateUrl(method, data...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user