change router struct
This commit is contained in:
156
internal/erpserver/handler/system/config/config.go
Normal file
156
internal/erpserver/handler/system/config/config.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"management/internal/erpserver/model/dto"
|
||||
systemModel "management/internal/erpserver/model/system"
|
||||
"management/internal/erpserver/model/view"
|
||||
systemService "management/internal/erpserver/service/v1"
|
||||
"management/internal/erpserver/templ/system/config"
|
||||
"management/internal/pkg/convertor"
|
||||
"management/internal/pkg/database"
|
||||
"management/internal/pkg/render"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
render render.Renderer
|
||||
configService systemService.ConfigService
|
||||
}
|
||||
|
||||
func NewApp(render render.Renderer, configService systemService.ConfigService) *App {
|
||||
return &App{
|
||||
render: render,
|
||||
configService: configService,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) list(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
ctx := r.Context()
|
||||
a.render.Render(ctx, w, config.List(ctx))
|
||||
case http.MethodPost:
|
||||
var q dto.SearchDto
|
||||
q.SearchTimeBegin, q.SearchTimeEnd = convertor.DefaultStartTimeAndEndTime(r.PostFormValue("timeBegin"), r.PostFormValue("timeEnd"))
|
||||
q.SearchName = r.PostFormValue("name")
|
||||
q.Page = convertor.ConvertInt(r.PostFormValue("page"), 1)
|
||||
q.Rows = convertor.ConvertInt(r.PostFormValue("rows"), 10)
|
||||
ctx := r.Context()
|
||||
res, count, err := a.configService.List(ctx, q)
|
||||
if err != nil {
|
||||
a.render.JSONErr(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
data := render.ResponseList{
|
||||
Code: 0,
|
||||
Message: "ok",
|
||||
Count: count,
|
||||
Data: res,
|
||||
}
|
||||
a.render.JSON(w, data)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) add(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
a.render.Render(ctx, w, config.Edit(ctx, &view.EditSysConfig{Config: &systemModel.Config{}}))
|
||||
}
|
||||
|
||||
func (a *App) edit(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
vars := r.URL.Query()
|
||||
id := convertor.QueryInt[int32](vars, "id", 0)
|
||||
vm := &view.EditSysConfig{}
|
||||
if id > 0 {
|
||||
if conf, err := a.configService.Get(r.Context(), id); err == nil {
|
||||
vm.Config = conf
|
||||
b, _ := json.Marshal(&conf.Value)
|
||||
vm.Result = string(b)
|
||||
}
|
||||
}
|
||||
a.render.Render(ctx, w, config.Edit(ctx, vm))
|
||||
}
|
||||
|
||||
func (a *App) save(w http.ResponseWriter, r *http.Request) {
|
||||
id := convertor.ConvertInt[int32](r.PostFormValue("ID"), 0)
|
||||
key := r.PostFormValue("Key")
|
||||
value := r.PostFormValue("Value")
|
||||
if len(key) == 0 {
|
||||
a.render.JSONErr(w, "Key不能为空")
|
||||
return
|
||||
}
|
||||
if len(value) == 0 {
|
||||
a.render.JSONErr(w, "Value不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
if id == 0 {
|
||||
arg := &systemModel.Config{
|
||||
Key: key,
|
||||
Value: []byte(value),
|
||||
}
|
||||
err := a.configService.Create(ctx, arg)
|
||||
if err != nil {
|
||||
if database.IsUniqueViolation(err) {
|
||||
a.render.JSONErr(w, "数据已存在")
|
||||
return
|
||||
}
|
||||
a.render.JSONErr(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
a.render.JSONOk(w, "添加成功")
|
||||
} else {
|
||||
res, err := a.configService.Get(ctx, id)
|
||||
if err != nil {
|
||||
a.render.JSONErr(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
res.Value = []byte(value)
|
||||
err = a.configService.Update(ctx, res)
|
||||
if err != nil {
|
||||
a.render.JSONErr(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
a.render.JSONOk(w, "更新成功")
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) refreshCache(w http.ResponseWriter, r *http.Request) {
|
||||
key := r.FormValue("key")
|
||||
err := a.configService.RefreshCache(r.Context(), strings.ToLower(key))
|
||||
if err != nil {
|
||||
a.render.JSONErr(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
a.render.JSONOk(w, "刷新成功")
|
||||
}
|
||||
|
||||
func (a *App) resetPear(w http.ResponseWriter, r *http.Request) {
|
||||
err := a.configService.ResetPear(r.Context())
|
||||
if err != nil {
|
||||
a.render.JSONErr(w, err.Error())
|
||||
return
|
||||
}
|
||||
a.render.JSONOk(w, "重置成功")
|
||||
}
|
||||
|
||||
func (a *App) pear(w http.ResponseWriter, r *http.Request) {
|
||||
pear, err := a.configService.Pear(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
a.render.JSON(w, pear)
|
||||
}
|
||||
35
internal/erpserver/handler/system/config/route.go
Normal file
35
internal/erpserver/handler/system/config/route.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
v1 "management/internal/erpserver/service/v1"
|
||||
"management/internal/pkg/mid"
|
||||
"management/internal/pkg/render"
|
||||
"management/internal/pkg/session"
|
||||
|
||||
"github.com/drhin/logger"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Log *logger.Logger
|
||||
Sm session.Manager
|
||||
Render render.Renderer
|
||||
MenuService v1.MenuService
|
||||
ConfigService v1.ConfigService
|
||||
}
|
||||
|
||||
func Routes(r chi.Router, cfg Config) {
|
||||
app := NewApp(cfg.Render, cfg.ConfigService)
|
||||
|
||||
r.Get("/pear.json", app.pear)
|
||||
r.Route("/config", func(r chi.Router) {
|
||||
r.Use(mid.Audit(cfg.Sm, cfg.Log))
|
||||
r.Get("/list", app.list)
|
||||
r.Post("/list", app.list)
|
||||
r.Get("/add", app.add)
|
||||
r.Get("/edit", app.edit)
|
||||
r.Post("/save", app.save)
|
||||
r.Post("/refresh_cache", app.refreshCache)
|
||||
r.Post("/reset_pear", app.resetPear)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user