parent
d084a07bb5
commit
3192078b05
3 changed files with 101 additions and 0 deletions
|
@ -3,6 +3,8 @@ package webui
|
|||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
||||
|
@ -54,3 +56,79 @@ func handleStatesIdGET(db *database.DB) http.Handler {
|
|||
})
|
||||
})
|
||||
}
|
||||
|
||||
func handleStatesIdPOST(db *database.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
errorResponse(w, r, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
action := r.FormValue("action")
|
||||
var stateId uuid.UUID
|
||||
if err := stateId.Parse(r.PathValue("id")); err != nil {
|
||||
errorResponse(w, r, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
state, err := db.LoadStateById(stateId)
|
||||
if err != nil {
|
||||
errorResponse(w, r, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
versions, err := db.LoadVersionsByState(state)
|
||||
if err != nil {
|
||||
errorResponse(w, r, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
usernames, err := db.LoadAccountUsernames()
|
||||
if err != nil {
|
||||
errorResponse(w, r, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
switch action {
|
||||
case "delete":
|
||||
errorResponse(w, r, http.StatusNotImplemented, err)
|
||||
case "edit":
|
||||
statePath := r.FormValue("path")
|
||||
parsedStatePath, err := url.Parse(statePath)
|
||||
if err != nil || path.Clean(parsedStatePath.Path) != statePath || statePath[0] != '/' {
|
||||
render(w, statesIdTemplate, http.StatusBadRequest, StatesIdPage{
|
||||
Page: makePage(r, &Page{Title: state.Path, Section: "states"}),
|
||||
Path: statePath,
|
||||
PathError: true,
|
||||
State: state,
|
||||
Usernames: usernames,
|
||||
Versions: versions,
|
||||
})
|
||||
return
|
||||
}
|
||||
state.Path = statePath
|
||||
success, err := db.SaveState(state)
|
||||
if err != nil {
|
||||
errorResponse(w, r, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
if !success {
|
||||
render(w, statesIdTemplate, http.StatusBadRequest, StatesIdPage{
|
||||
Page: makePage(r, &Page{Title: state.Path, Section: "states"}),
|
||||
Path: statePath,
|
||||
PathDuplicate: true,
|
||||
State: state,
|
||||
Usernames: usernames,
|
||||
Versions: versions,
|
||||
})
|
||||
return
|
||||
}
|
||||
render(w, statesIdTemplate, http.StatusOK, StatesIdPage{
|
||||
Page: makePage(r, &Page{
|
||||
Section: "states",
|
||||
Title: state.Path,
|
||||
}),
|
||||
State: state,
|
||||
Usernames: usernames,
|
||||
Versions: versions,
|
||||
})
|
||||
default:
|
||||
errorResponse(w, r, http.StatusBadRequest, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue