108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
package controller
|
|
|
|
import (
|
|
"arno/skeletor/config"
|
|
"arno/skeletor/entity"
|
|
"arno/skeletor/repository"
|
|
"arno/skeletor/service"
|
|
"arno/skeletor/tool"
|
|
"net/http"
|
|
"github.com/martini-contrib/sessions"
|
|
"github.com/martini-contrib/render"
|
|
)
|
|
|
|
func ConfigList(ctn *service.Container, session sessions.Session, req *http.Request, r render.Render) {
|
|
myconfig := config.Must(ctn)
|
|
mydb := repository.Must(ctn)
|
|
|
|
var configs []entity.Config
|
|
mapconfigs := make(map[string]entity.Config)
|
|
mydb.Find(&configs)
|
|
for _, config := range configs {
|
|
mapconfigs[config.Id] = config
|
|
}
|
|
|
|
rendermap := map[string]interface{}{
|
|
"conf": myconfig,
|
|
"session": tool.Rendersession(session),
|
|
"useheader": true,
|
|
"usesidebar": true,
|
|
"usecontainer": true,
|
|
"configs": mapconfigs,
|
|
}
|
|
|
|
r.HTML(200, "config/list", rendermap)
|
|
}
|
|
|
|
func ConfigRefresh(ctn *service.Container, session sessions.Session, req *http.Request, r render.Render) {
|
|
myconfig := config.Must(ctn)
|
|
mydb := repository.Must(ctn)
|
|
|
|
myconfig.Refresh(mydb)
|
|
|
|
r.Redirect(myconfig.AppRoutes["configlist"])
|
|
}
|
|
|
|
func ConfigUpdate(ctn *service.Container, session sessions.Session, req *http.Request, r render.Render, id string) {
|
|
myconfig := config.Must(ctn)
|
|
mydb := repository.Must(ctn)
|
|
|
|
var config entity.Config
|
|
result := mydb.First(&config, "id = ?", id)
|
|
if result.RowsAffected == 0 {
|
|
r.Redirect(myconfig.AppRoutes["configlist"])
|
|
}
|
|
|
|
if req.Method == http.MethodPost {
|
|
req.ParseForm()
|
|
value := req.Form.Get("value")
|
|
if value != config.Defaultvalue {
|
|
config.Value = value
|
|
} else {
|
|
config.Value = ""
|
|
}
|
|
|
|
mydb.Save(&config)
|
|
|
|
r.Redirect(myconfig.AppRoutes["configrefresh"])
|
|
}
|
|
|
|
rendermap := map[string]interface{}{
|
|
"conf": myconfig,
|
|
"session": tool.Rendersession(session),
|
|
"useheader": true,
|
|
"usesidebar": true,
|
|
"usecontainer": true,
|
|
"config": config,
|
|
}
|
|
|
|
r.HTML(200, "config/edit", rendermap)
|
|
}
|
|
|
|
func ConfigDelete(ctn *service.Container, session sessions.Session, req *http.Request, r render.Render, id string) {
|
|
myconfig := config.Must(ctn)
|
|
mydb := repository.Must(ctn)
|
|
|
|
var config entity.Config
|
|
result := mydb.First(&config, "id = ?", id)
|
|
if result.RowsAffected == 0 {
|
|
r.Redirect(myconfig.AppRoutes["configlist"])
|
|
}
|
|
|
|
config.Value = config.Defaultvalue
|
|
mydb.Save(&config)
|
|
r.Redirect(myconfig.AppRoutes["configrefresh"])
|
|
}
|
|
|
|
func GetConfig(ctn *service.Container, id string) string {
|
|
mydb := repository.Must(ctn)
|
|
|
|
var myconfig entity.Config
|
|
mydb.First(&myconfig, "id = ?", id)
|
|
if myconfig.Value == "" {
|
|
return myconfig.Defaultvalue
|
|
} else {
|
|
return myconfig.Value
|
|
}
|
|
}
|