Add state
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"gitea.henriburau.de/haw-lan/cod4watcher/models"
|
||||
"gitea.henriburau.de/haw-lan/cod4watcher/views/capture"
|
||||
"github.com/go-chi/chi"
|
||||
)
|
||||
@ -22,3 +23,54 @@ func (s *Server) HandleCapture(w http.ResponseWriter, r *http.Request) error {
|
||||
|
||||
return Render(w, r, capture.Capture(foundCapture))
|
||||
}
|
||||
|
||||
func (s *Server) HandleCaptureForm(w http.ResponseWriter, r *http.Request) error {
|
||||
return Render(w, r, capture.CaptureForm(capture.CaptureFormValues{}, map[string]string{}))
|
||||
}
|
||||
|
||||
func (s *Server) HandleCaptureCreate(w http.ResponseWriter, r *http.Request) error {
|
||||
formValues, errors := parseCaptureFormAndValidate(r)
|
||||
if len(errors) > 0 {
|
||||
return Render(w, r, capture.CaptureForm(formValues, errors))
|
||||
}
|
||||
|
||||
capture := &models.Capture{
|
||||
Host: formValues.Host,
|
||||
Port: formValues.Port,
|
||||
Name: formValues.Name,
|
||||
Active: true,
|
||||
}
|
||||
|
||||
err := s.cs.CreateCapture(r.Context(), capture)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return hxRedirect(w, r, "/")
|
||||
}
|
||||
|
||||
func parseCaptureFormAndValidate(r *http.Request) (capture.CaptureFormValues, map[string]string) {
|
||||
r.ParseForm()
|
||||
|
||||
formValues := capture.CaptureFormValues{
|
||||
Host: r.FormValue("host"),
|
||||
Port: r.FormValue("port"),
|
||||
Name: r.FormValue("name"),
|
||||
}
|
||||
|
||||
errors := map[string]string{}
|
||||
|
||||
if len(formValues.Host) <= 0 {
|
||||
errors["host"] = "Host needs to be set"
|
||||
}
|
||||
|
||||
if len(formValues.Port) <= 0 {
|
||||
errors["port"] = "Port needs to be set"
|
||||
}
|
||||
|
||||
if len(formValues.Name) < 5 || len(formValues.Name) > 50 {
|
||||
errors["name"] = "Name needs to be between 5 and 50 characters long"
|
||||
}
|
||||
|
||||
return formValues, errors
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func (s *Server) HandleHome(w http.ResponseWriter, r *http.Request) error {
|
||||
captureList, err := s.cs.GetActiveCapures()
|
||||
captureList, err := s.cs.GetActiveCapures(r.Context())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -22,6 +22,23 @@ type Server struct {
|
||||
cs *services.CaptureService
|
||||
}
|
||||
|
||||
func NewServer(cs *services.CaptureService) *Server {
|
||||
return &Server{
|
||||
cs,
|
||||
}
|
||||
}
|
||||
|
||||
func Render(w http.ResponseWriter, r *http.Request, c templ.Component) error {
|
||||
return c.Render(r.Context(), w)
|
||||
}
|
||||
|
||||
func hxRedirect(w http.ResponseWriter, r *http.Request, url string) error {
|
||||
if len(r.Header.Get("HX-Request")) > 0 {
|
||||
w.Header().Set("hx-redirect", url)
|
||||
w.WriteHeader(http.StatusSeeOther)
|
||||
return nil
|
||||
}
|
||||
|
||||
http.Redirect(w, r, url, http.StatusSeeOther)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user