implement update issue
This commit is contained in:
@@ -18,14 +18,12 @@ type IssueForm struct {
|
|||||||
Priority int `form:"priority" validate:"gte=0,lte=4"`
|
Priority int `form:"priority" validate:"gte=0,lte=4"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *IssueForm) ToIssue() models.Issue {
|
type UpdateIssueForm struct {
|
||||||
return models.Issue{
|
Title *string `form:"title" validate:"omitempty,max=255"`
|
||||||
Title: f.Title,
|
Description *string `form:"description" validate:"omitempty,max=2000"`
|
||||||
Description: f.Description,
|
Status *models.Status `form:"status" validate:"omitempty,oneof=open in_progress closed"`
|
||||||
Status: f.Status,
|
IssueType *models.IssueType `form:"issue_type" validate:"omitempty,oneof=task bug feature chore"`
|
||||||
IssueType: f.IssueType,
|
Priority *int `form:"priority" validate:"omitempty,gte=0,lte=4"`
|
||||||
Priority: f.Priority,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateIssue(w http.ResponseWriter, r *http.Request) {
|
func CreateIssue(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -48,7 +46,7 @@ func CreateIssue(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
issue := form.ToIssue()
|
issue := form.toIssue()
|
||||||
if err := svc.Beads.CreateIssue(r.Context(), &issue, ""); err != nil {
|
if err := svc.Beads.CreateIssue(r.Context(), &issue, ""); err != nil {
|
||||||
http.Error(w, "Failed to create issue: "+err.Error(), http.StatusInternalServerError)
|
http.Error(w, "Failed to create issue: "+err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -59,10 +57,8 @@ func CreateIssue(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
hx.WriteJSON(map[string]any{
|
w.Header().Set("Content-Type", "application/json")
|
||||||
"title": issue.Title,
|
hx.WriteJSON(issue)
|
||||||
"status": issue.Status,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListIssues(w http.ResponseWriter, r *http.Request) {
|
func ListIssues(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -75,6 +71,7 @@ func ListIssues(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
hx.WriteJSON(issues)
|
hx.WriteJSON(issues)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,6 +82,10 @@ func IssueCtx(next http.Handler) http.Handler {
|
|||||||
id := chi.URLParam(r, "id")
|
id := chi.URLParam(r, "id")
|
||||||
issue, err := svc.Beads.GetIssue(r.Context(), id)
|
issue, err := svc.Beads.GetIssue(r.Context(), id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
http.Error(w, "Error getting issue: "+err.Error(), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if issue == nil {
|
||||||
http.Error(w, "Issue not found", http.StatusNotFound)
|
http.Error(w, "Issue not found", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -107,19 +108,36 @@ func UpdateIssue(w http.ResponseWriter, r *http.Request) {
|
|||||||
svc := Services(r)
|
svc := Services(r)
|
||||||
hx := HTMX(r)
|
hx := HTMX(r)
|
||||||
|
|
||||||
changes := make(map[string]any)
|
form, err := ParseForm[UpdateIssueForm](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>Please fix the form errors</div>")
|
||||||
|
} else {
|
||||||
|
http.Error(w, "Validation error: "+err.Error(), http.StatusUnprocessableEntity)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
changes := form.toChanges()
|
||||||
|
|
||||||
if err := svc.Beads.UpdateIssue(r.Context(), issue.ID, changes, ""); err != nil {
|
if err := svc.Beads.UpdateIssue(r.Context(), issue.ID, changes, ""); err != nil {
|
||||||
http.Error(w, "Failed to update issue", http.StatusInternalServerError)
|
http.Error(w, "Failed to update issue", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
issue, err := svc.Beads.GetIssue(r.Context(), issue.ID)
|
issue, err = svc.Beads.GetIssue(r.Context(), issue.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to retrieve updated issue", http.StatusInternalServerError)
|
http.Error(w, "Failed to retrieve updated issue", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
hx.WriteJSON(issue)
|
hx.WriteJSON(issue)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,5 +150,36 @@ func DeleteIssue(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 (f *UpdateIssueForm) toChanges() map[string]any {
|
||||||
|
changes := make(map[string]any)
|
||||||
|
if f.Title != nil {
|
||||||
|
changes["title"] = *f.Title
|
||||||
|
}
|
||||||
|
if f.Description != nil {
|
||||||
|
changes["description"] = *f.Description
|
||||||
|
}
|
||||||
|
if f.Status != nil {
|
||||||
|
changes["status"] = *f.Status
|
||||||
|
}
|
||||||
|
if f.IssueType != nil {
|
||||||
|
changes["issue_type"] = *f.IssueType
|
||||||
|
}
|
||||||
|
if f.Priority != nil {
|
||||||
|
changes["priority"] = *f.Priority
|
||||||
|
}
|
||||||
|
return changes
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user