feat: add parse endpoint, refactor recurring tasks, and improve web task completion
Extract CreateRecurringTask into engine package for reuse by both CLI and API. Add POST /tasks/parse endpoint for CLI-style input parsing. Remove FK constraint on change_log to preserve history after task deletion. Update web frontend to filter completed tasks from view and add mock mode support for development. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.jnss.me/joakim/opal/internal/engine"
|
||||
@@ -30,10 +31,32 @@ func errorResponse(w http.ResponseWriter, status int, message string) {
|
||||
})
|
||||
}
|
||||
|
||||
// ListTasks returns tasks based on filter query parameters
|
||||
// ListTasks returns tasks based on filter query parameters or a named report
|
||||
func ListTasks(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query()
|
||||
|
||||
// Check for report mode
|
||||
if reportName := query.Get("report"); reportName != "" {
|
||||
report, err := engine.GetReport(reportName)
|
||||
if err != nil {
|
||||
errorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
tasks, err := report.Execute(nil)
|
||||
if err != nil {
|
||||
errorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
jsonResponse(w, http.StatusOK, map[string]interface{}{
|
||||
"report": reportName,
|
||||
"tasks": tasks,
|
||||
"count": len(tasks),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Build filter from query params
|
||||
filter := engine.NewFilter()
|
||||
|
||||
@@ -409,6 +432,77 @@ func AddTaskTag(w http.ResponseWriter, r *http.Request) {
|
||||
jsonResponse(w, http.StatusOK, task)
|
||||
}
|
||||
|
||||
// ParseTaskRequest represents the request body for parsing a CLI-style task input
|
||||
type ParseTaskRequest struct {
|
||||
Input string `json:"input"`
|
||||
}
|
||||
|
||||
// ParseTask accepts a raw CLI-style input string, parses it, and creates a task
|
||||
func ParseTask(w http.ResponseWriter, r *http.Request) {
|
||||
var req ParseTaskRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
errorResponse(w, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
input := strings.TrimSpace(req.Input)
|
||||
if input == "" {
|
||||
errorResponse(w, http.StatusBadRequest, "input is required")
|
||||
return
|
||||
}
|
||||
|
||||
// Split input on whitespace
|
||||
args := strings.Fields(input)
|
||||
|
||||
// Classify args: words with +/- prefix or containing : are modifiers
|
||||
var descParts []string
|
||||
var modifierArgs []string
|
||||
for _, arg := range args {
|
||||
if strings.HasPrefix(arg, "+") || strings.HasPrefix(arg, "-") || strings.Contains(arg, ":") {
|
||||
modifierArgs = append(modifierArgs, arg)
|
||||
} else {
|
||||
descParts = append(descParts, arg)
|
||||
}
|
||||
}
|
||||
|
||||
if len(descParts) == 0 {
|
||||
errorResponse(w, http.StatusBadRequest, "description is required")
|
||||
return
|
||||
}
|
||||
description := strings.Join(descParts, " ")
|
||||
|
||||
// Parse modifiers
|
||||
var mod *engine.Modifier
|
||||
if len(modifierArgs) > 0 {
|
||||
var err error
|
||||
mod, err = engine.ParseModifier(modifierArgs)
|
||||
if err != nil {
|
||||
errorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to parse modifiers: %v", err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Check for recurring task
|
||||
if mod != nil && mod.SetAttributes["recur"] != nil {
|
||||
instance, err := engine.CreateRecurringTask(description, mod)
|
||||
if err != nil {
|
||||
errorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
jsonResponse(w, http.StatusCreated, map[string]interface{}{"task": instance})
|
||||
return
|
||||
}
|
||||
|
||||
// Create regular task
|
||||
task, err := engine.CreateTaskWithModifier(description, mod)
|
||||
if err != nil {
|
||||
errorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
jsonResponse(w, http.StatusCreated, map[string]interface{}{"task": task})
|
||||
}
|
||||
|
||||
// RemoveTaskTag removes a tag from a task
|
||||
func RemoveTaskTag(w http.ResponseWriter, r *http.Request) {
|
||||
uuidStr := chi.URLParam(r, "uuid")
|
||||
|
||||
Reference in New Issue
Block a user