Files
LazyPM/pkg/web/handler/comments.go
Robin Olsen 2e4185e30f use beads service
use pointes for comments
use htmx routing
2026-02-22 11:03:31 +01:00

66 lines
1.6 KiB
Go

package handler
import (
"net/http"
"github.com/LazyBachelor/LazyPM/internal/models"
"github.com/LazyBachelor/LazyPM/pkg/web/components"
)
type CommentForm struct {
Author string `form:"author" validate:"required,max=100"`
Text string `form:"text" validate:"required,max=2000"`
}
func ListComments(w http.ResponseWriter, r *http.Request) {
comments := r.Context().Value(commentsKey).([]*models.Comment)
hx := HTMX(r)
if hx.IsHxRequest() {
components.CommentList(components.CommentListProps{
Comments: comments,
}).Render(r.Context(), w)
return
}
w.Header().Set("Content-Type", "application/json")
hx.WriteJSON(comments)
}
func CreateComment(w http.ResponseWriter, r *http.Request) {
issue := r.Context().Value(issueKey).(*models.Issue)
svc := Services(r)
hx := HTMX(r)
form, err := ParseForm[CommentForm](r)
if err != nil {
http.Error(w, "Failed to parse form", http.StatusBadRequest)
return
}
if err := ValidateForm(form); err != nil {
if hx.IsHxRequest() {
hx.WriteString(`<div class="alert alert-error">Please fix the form errors</div>`)
} else {
http.Error(w, "Validation error: "+err.Error(), http.StatusUnprocessableEntity)
}
return
}
comment, err := svc.Beads.AddIssueComment(r.Context(), issue.ID, form.Author, form.Text)
if err != nil {
http.Error(w, "Failed to create comment: "+err.Error(), http.StatusInternalServerError)
return
}
if hx.IsHxRequest() {
components.CommentItem(components.CommentItemProps{
Comment: comment,
}).Render(r.Context(), w)
return
}
w.Header().Set("Content-Type", "application/json")
hx.WriteJSON(comment)
}