feat(admin): 实现分类管理功能- 添加分类列表展示页面模板- 实现分类编辑和保存功能
- 集成会话管理用于编辑状态跟踪 - 添加分类数据持久化支持 - 更新配置文件以包含分类相关SQL脚本路径 - 实现分类的增删改查逻辑 - 添加分类保存和更新的SQL脚本
This commit is contained in:
@@ -2,15 +2,45 @@ package admin
|
||||
|
||||
import (
|
||||
"platform/http/actionresults"
|
||||
"platform/http/handing"
|
||||
"platform/sessions"
|
||||
"sportstore/models"
|
||||
"sportstore/store"
|
||||
)
|
||||
|
||||
type CategoriesHandler struct {
|
||||
models.Repository
|
||||
handing.URLGenerator
|
||||
sessions.Session
|
||||
}
|
||||
|
||||
func (handler CategoriesHandler) GetData() string {
|
||||
return "This is the categories handler"
|
||||
type CategoryTemplateContext struct {
|
||||
Categories []models.Category
|
||||
EditId int
|
||||
EditUrl string
|
||||
SaveUrl string
|
||||
}
|
||||
|
||||
const CategoryEditKey = "category_edit"
|
||||
|
||||
func (handler CategoriesHandler) GetData() actionresults.ActionResult {
|
||||
context := CategoryTemplateContext{
|
||||
Categories: handler.GetCategories(),
|
||||
EditId: handler.Session.GetValueDefault(CategoryEditKey, 0).(int),
|
||||
EditUrl: store.MustGenerateUrl(handler.URLGenerator, CategoriesHandler.PostCategoryEdit),
|
||||
SaveUrl: store.MustGenerateUrl(handler.URLGenerator, CategoriesHandler.PostCategorySave),
|
||||
}
|
||||
return actionresults.NewTemplateAction("admin_categories.html", context)
|
||||
}
|
||||
|
||||
func (handler CategoriesHandler) PostCategoryEdit(ref EditReference) actionresults.ActionResult {
|
||||
handler.Session.SetValue(CategoryEditKey, ref.ID)
|
||||
return actionresults.NewRedirectAction(store.MustGenerateUrl(handler.URLGenerator, AdminHandler.GetSection, "Categories"))
|
||||
}
|
||||
func (handler CategoriesHandler) PostCategorySave(c models.Category) actionresults.ActionResult {
|
||||
handler.SaveCategory(&c)
|
||||
handler.Session.SetValue(CategoryEditKey, 0)
|
||||
return actionresults.NewRedirectAction(store.MustGenerateUrl(handler.URLGenerator, AdminHandler.GetSection, "Categories"))
|
||||
}
|
||||
|
||||
func (handler CategoriesHandler) GetSelect(current int) actionresults.ActionResult {
|
||||
|
||||
@@ -33,7 +33,9 @@
|
||||
"SaveOrder": "sql/save_order.sql",
|
||||
"SaveOrderLine": "sql/save_order_line.sql",
|
||||
"SaveProduct": "sql/save_product.sql",
|
||||
"UpdateProduct": "sql/update_product.sql"
|
||||
"UpdateProduct": "sql/update_product.sql",
|
||||
"SaveCategory": "sql/save_category.sql",
|
||||
"UpdateCategory": "sql/update_category.sql"
|
||||
}
|
||||
}
|
||||
}
|
||||
34
32-platform/sportsstore/models/repo/sql_category_save.go
Normal file
34
32-platform/sportsstore/models/repo/sql_category_save.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package repo
|
||||
|
||||
import "sportstore/models"
|
||||
|
||||
func (repo *SqlRepository) SaveCategory(c *models.Category) {
|
||||
if c.ID == 0 {
|
||||
result, err := repo.Commands.SaveCategory.ExecContext(repo.Context, c.CategoryName)
|
||||
if err != nil {
|
||||
repo.Logger.Panicf("Cannot exec SaveCategory command: %v ", err.Error())
|
||||
return
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
repo.Logger.Panicf("Cannot exec SaveCategory get LastInsertId: %v ", err.Error())
|
||||
return
|
||||
}
|
||||
c.ID = int(id)
|
||||
return
|
||||
} else {
|
||||
result, err := repo.Commands.UpdateCategory.ExecContext(repo.Context, c.CategoryName, c.ID)
|
||||
if err != nil {
|
||||
repo.Logger.Panicf("Cannot exec UpdateCategory command: %v ", err.Error())
|
||||
return
|
||||
}
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
repo.Logger.Panicf("Cannot exec UpdateCategory get RowsAffected: %v ", err.Error())
|
||||
return
|
||||
}
|
||||
if rowsAffected != 1 {
|
||||
repo.Logger.Panicf("Got exec UpdateCategory unexpected rows affected: %v ", rowsAffected)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,5 +32,7 @@ type SqlCommands struct {
|
||||
SaveOrder,
|
||||
SaveOrderLine,
|
||||
SaveProduct,
|
||||
UpdateProduct *LoggedStmt
|
||||
UpdateProduct,
|
||||
SaveCategory,
|
||||
UpdateCategory *LoggedStmt
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ type Repository interface {
|
||||
|
||||
GetCategories() []Category
|
||||
|
||||
SaveCategory(*Category)
|
||||
|
||||
GetOrder(id int) Order
|
||||
|
||||
GetOrders() []Order
|
||||
|
||||
2
32-platform/sportsstore/sql/save_category.sql
Normal file
2
32-platform/sportsstore/sql/save_category.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
INSERT INTO Categories (Name)
|
||||
VALUES (?)
|
||||
3
32-platform/sportsstore/sql/update_category.sql
Normal file
3
32-platform/sportsstore/sql/update_category.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
UPDATE Categories
|
||||
SET Name = ?
|
||||
WHERE Id = ?
|
||||
Binary file not shown.
57
32-platform/sportsstore/templates/admin_categories.html
Normal file
57
32-platform/sportsstore/templates/admin_categories.html
Normal file
@@ -0,0 +1,57 @@
|
||||
{{ $context := . }}
|
||||
|
||||
<table class="table table-sm table-striped table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Id</th>
|
||||
<th>Name</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{ range $context.Categories }}
|
||||
|
||||
{{ if ne $context.EditId .ID }}
|
||||
<tr>
|
||||
<td>{{ .ID }}</td>
|
||||
<td>{{ .CategoryName }}</td>
|
||||
<td class="text-center">
|
||||
<form method="post" action="{{ $context.EditUrl }}">
|
||||
<input type="hidden" name="id" value="{{ .ID }}"/>
|
||||
<button type="submit" class="btn btn-sm btn-warning">Edit</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{{ else }}
|
||||
<tr>
|
||||
<form method="post" action="{{ $context.SaveUrl }}">
|
||||
<td><input type="hidden" name="id" value="{{ .ID }}"/><input
|
||||
type="text" value="{{ .ID }}" class="form-control" size="3" disabled/></td>
|
||||
<td><input type="text" name="CategoryName" value="{{ .CategoryName }}" class="form-control" size="12"/></td>
|
||||
<td>
|
||||
<button type="submit" class="btn btn-sm btn-danger">Save</button>
|
||||
</td>
|
||||
</form>
|
||||
</tr>
|
||||
{{ end }}
|
||||
|
||||
|
||||
{{ end }}
|
||||
</tbody>
|
||||
{{ if eq $context.EditId 0 }}
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="6" class="text-center">Add New Product</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<form method="post" action="{{ $context.SaveUrl }}">
|
||||
<td>-</td>
|
||||
<td><input type="text" name="CategoryName" class="form-control" size="12"/></td>
|
||||
<td>
|
||||
<button type="submit" class="btn btn-sm btn-danger">Save</button>
|
||||
</td>
|
||||
</form>
|
||||
</tr>
|
||||
</tfoot>
|
||||
{{ end }}
|
||||
</table>
|
||||
Reference in New Issue
Block a user