updating web to be compatible with main

This commit is contained in:
viljarb
2026-02-18 20:33:32 +01:00
parent b654fb5bd3
commit ed8a0d02ec
25 changed files with 3800 additions and 793 deletions

View File

@@ -0,0 +1,42 @@
package handler
import (
"context"
"net/http"
"github.com/LazyBachelor/LazyPM/internal/service"
"github.com/donseba/go-htmx"
)
type contextKey string
const (
servicesKey contextKey = "services"
htmxKey contextKey = "htmx"
)
func ServicesMiddleware(svc *service.Services) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), servicesKey, svc)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
func HTMXMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
htmxInstance := htmx.New()
handler := htmxInstance.NewHandler(w, r)
ctx := context.WithValue(r.Context(), htmxKey, handler)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func Services(r *http.Request) *service.Services {
return r.Context().Value(servicesKey).(*service.Services)
}
func HTMX(r *http.Request) *htmx.Handler {
return r.Context().Value(htmxKey).(*htmx.Handler)
}

30
pkg/web/handler/forms.go Normal file
View File

@@ -0,0 +1,30 @@
package handler
import (
"net/http"
"github.com/go-playground/form/v4"
"github.com/go-playground/validator/v10"
)
var (
decoder = form.NewDecoder()
validate = validator.New(validator.WithRequiredStructEnabled())
)
func ParseForm[T any](r *http.Request) (*T, error) {
if err := r.ParseForm(); err != nil {
return nil, err
}
var data T
if err := decoder.Decode(&data, r.PostForm); err != nil {
return nil, err
}
return &data, nil
}
func ValidateForm[T any](data *T) error {
return validate.Struct(data)
}

View File

@@ -1,21 +0,0 @@
package handler
import (
"net/http"
"github.com/LazyBachelor/LazyPM/internal/service"
)
type Route struct {
Pattern string
Handler http.Handler
}
func GetRoutes(svc *service.Services) []Route {
var routes []Route
routes = append(routes, PagesRoutes(svc)...)
routes = append(routes, IssuesRoutes(svc)...)
return routes
}

26
pkg/web/handler/index.go Normal file
View File

@@ -0,0 +1,26 @@
package handler
import (
"net/http"
"github.com/LazyBachelor/LazyPM/pkg/web/components"
"github.com/LazyBachelor/LazyPM/pkg/web/routes"
)
func IndexHandler(w http.ResponseWriter, r *http.Request) {
svc := Services(r)
issues, err := svc.Beads.AllIssues(r.Context())
if err != nil {
http.Error(w, "failed to retrieve issues", http.StatusInternalServerError)
return
}
props := routes.IndexProps{
IssueTable: components.IssueTableProps{
Issues: issues,
},
}
routes.Index(props).Render(r.Context(), w)
}

View File

@@ -1,72 +1,134 @@
package handler
import (
"encoding/json"
"fmt"
"context"
"net/http"
"github.com/LazyBachelor/LazyPM/internal/models"
"github.com/LazyBachelor/LazyPM/internal/service"
"github.com/go-chi/chi/v5"
)
func IssuesRoutes(svc *service.Services) []Route {
return []Route{
{Pattern: "/issues", Handler: GetAllIssues(svc)},
{Pattern: "POST /create-issue", Handler: CreateIssue(svc)},
type IssueForm struct {
Title string `form:"title" validate:"required,max=255"`
Description string `form:"description" validate:"required,max=2000"`
Status models.Status `form:"status" validate:"required,oneof=open in_progress closed"`
IssueType models.IssueType `form:"issue_type" validate:"required,oneof=task bug feature chore"`
Priority int `form:"priority" validate:"gte=0,lte=4"`
}
func (f *IssueForm) ToIssue() models.Issue {
return models.Issue{
Title: f.Title,
Description: f.Description,
Status: f.Status,
IssueType: f.IssueType,
Priority: f.Priority,
}
}
func CreateIssue(svc *service.Services) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
func CreateIssue(w http.ResponseWriter, r *http.Request) {
svc := Services(r)
hx := HTMX(r)
// 1. Parse Form instead of JSON
if err := r.ParseForm(); err != nil {
http.Error(w, "Failed to parse form", http.StatusBadRequest)
return
}
// 2. Map form values to your struct manually
// (Or use a library like 'gorilla/schema')
issue := models.Issue{
Title: r.FormValue("title"),
Description: r.FormValue("description"),
Status: models.Status(r.FormValue("status")),
IssueType: models.IssueType(r.FormValue("issue_type")),
}
err := svc.Beads.CreateIssue(r.Context(), &issue, "")
if err != nil {
http.Error(w, "Failed to create issue: "+err.Error(), http.StatusInternalServerError)
return
}
// 3. HTMX usually expects HTML back, not JSON
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, "<p>Created issue: %s</p>", issue.Title)
form, err := ParseForm[IssueForm](r)
if err != nil {
http.Error(w, "Failed to parse form", http.StatusBadRequest)
return
}
if err := ValidateForm(form); err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
if hx.IsHxRequest() {
hx.WriteString("<div class='alert alert-error'>Please fix the form errors</div>")
} else {
hx.WriteJSON(map[string]interface{}{"error": err.Error()})
}
return
}
issue := form.ToIssue()
if err := svc.Beads.CreateIssue(r.Context(), &issue, ""); err != nil {
http.Error(w, "Failed to create issue: "+err.Error(), http.StatusInternalServerError)
return
}
if hx.IsHxRequest() {
hx.WriteString("<div>Issue created successfully</div>")
return
}
hx.WriteJSON(map[string]any{
"title": issue.Title,
"status": issue.Status,
})
}
func GetAllIssues(svc *service.Services) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
issues, err := svc.Beads.AllIssues(r.Context())
func ListIssues(w http.ResponseWriter, r *http.Request) {
svc := Services(r)
hx := HTMX(r)
if err != nil {
http.Error(w, "Failed to retrieve issues", http.StatusInternalServerError)
return
}
jsonData, err := json.Marshal(issues)
if err != nil {
http.Error(w, "Failed to marshal issues", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
issues, err := svc.Beads.AllIssues(r.Context())
if err != nil {
http.Error(w, "Failed to retrieve issues", http.StatusInternalServerError)
return
}
hx.WriteJSON(issues)
}
func IssueCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svc := Services(r)
id := chi.URLParam(r, "id")
issue, err := svc.Beads.GetIssue(r.Context(), id)
if err != nil {
http.Error(w, "Issue not found", http.StatusNotFound)
return
}
ctx := context.WithValue(r.Context(), "issue", issue)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func GetIssue(w http.ResponseWriter, r *http.Request) {
issue := r.Context().Value("issue").(*models.Issue)
hx := HTMX(r)
hx.WriteJSON(issue)
}
func UpdateIssue(w http.ResponseWriter, r *http.Request) {
issue := r.Context().Value("issue").(*models.Issue)
svc := Services(r)
hx := HTMX(r)
changes := make(map[string]any)
if err := svc.Beads.UpdateIssue(r.Context(), issue.ID, changes, ""); err != nil {
http.Error(w, "Failed to update issue", http.StatusInternalServerError)
return
}
issue, err := svc.Beads.GetIssue(r.Context(), issue.ID)
if err != nil {
http.Error(w, "Failed to retrieve updated issue", http.StatusInternalServerError)
return
}
hx.WriteJSON(issue)
}
func DeleteIssue(w http.ResponseWriter, r *http.Request) {
issue := r.Context().Value("issue").(*models.Issue)
svc := Services(r)
if err := svc.Beads.DeleteIssue(r.Context(), issue.ID); err != nil {
http.Error(w, "Failed to delete issue", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}

View File

@@ -1,44 +0,0 @@
package handler
import (
"net/http"
"github.com/LazyBachelor/LazyPM/internal/service"
"github.com/LazyBachelor/LazyPM/pkg/web/components"
"github.com/LazyBachelor/LazyPM/pkg/web/routes"
)
func PagesRoutes(svc *service.Services) []Route {
return []Route{
{Pattern: "/", Handler: IndexHandler(svc)},
}
}
func IndexHandler(svc *service.Services) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
handleNotFound(w, r)
return
}
issues, err := svc.Beads.AllIssues(r.Context())
if err != nil {
http.Error(w, "Failed to retrieve issues",
http.StatusInternalServerError)
return
}
props := routes.IndexProps{
IssueTable: components.IssueTableProps{
Issues: issues,
},
}
routes.Index(props).Render(r.Context(), w)
}
}
func handleNotFound(w http.ResponseWriter, _ *http.Request) {
http.Error(w, "Page not found", http.StatusNotFound)
}