admin control features and admin dashboard
This commit is contained in:
1 parent
c630f13c57
commit
37910e410b
69 files changed
+4959
-400
No files matched your search
@@ -0,0 +1,66 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type sitesPageData struct {
|
||||
PageData
|
||||
Items []siteDTO // always exactly 3 entries, index 1..3, SiteID=="" for an empty slot
|
||||
}
|
||||
|
||||
// fillSlots pads control-api's response (which only lists assigned slots)
|
||||
// out to all three fixed slots, so the table always renders 3 rows.
|
||||
func fillSlots(sites []siteDTO) []siteDTO {
|
||||
byIndex := make(map[int]string, len(sites))
|
||||
for _, s := range sites {
|
||||
byIndex[s.Index] = s.SiteID
|
||||
}
|
||||
out := make([]siteDTO, 3)
|
||||
for i := 0; i < 3; i++ {
|
||||
out[i] = siteDTO{Index: i + 1, SiteID: byIndex[i+1]}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Server) handleSitesPage(w http.ResponseWriter, r *http.Request) {
|
||||
items, err := s.CA.ListSites(r.Context())
|
||||
data := sitesPageData{Items: fillSlots(items)}
|
||||
data.ActiveNav = "sites"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "sites_page", data)
|
||||
}
|
||||
|
||||
func (s *Server) renderSitesTable(w http.ResponseWriter, r *http.Request, actionErr error) {
|
||||
items, listErr := s.CA.ListSites(r.Context())
|
||||
if actionErr == nil {
|
||||
actionErr = listErr
|
||||
}
|
||||
s.renderFragment(w, "sites_table", sitesPageData{Items: fillSlots(items)}, actionErr)
|
||||
}
|
||||
|
||||
func (s *Server) handleSitePut(w http.ResponseWriter, r *http.Request) {
|
||||
index, convErr := strconv.Atoi(r.PathValue("index"))
|
||||
if convErr != nil {
|
||||
s.renderSitesTable(w, r, &apiErr{Status: http.StatusBadRequest, Message: "index должен быть числом"})
|
||||
return
|
||||
}
|
||||
if err := r.ParseForm(); err != nil {
|
||||
s.renderSitesTable(w, r, fmt.Errorf("invalid form: %w", err))
|
||||
return
|
||||
}
|
||||
err := s.CA.PutSite(r.Context(), index, r.PostFormValue("site_id"))
|
||||
s.renderSitesTable(w, r, err)
|
||||
}
|
||||
|
||||
func (s *Server) handleSiteDelete(w http.ResponseWriter, r *http.Request) {
|
||||
index, convErr := strconv.Atoi(r.PathValue("index"))
|
||||
if convErr != nil {
|
||||
s.renderSitesTable(w, r, &apiErr{Status: http.StatusBadRequest, Message: "index должен быть числом"})
|
||||
return
|
||||
}
|
||||
err := s.CA.DeleteSite(r.Context(), index)
|
||||
s.renderSitesTable(w, r, err)
|
||||
}
|
||||
Reference in new issue
Block a user