summaryrefslogtreecommitdiff
path: root/internal/handlers/widget.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/handlers/widget.go')
-rw-r--r--internal/handlers/widget.go153
1 files changed, 153 insertions, 0 deletions
diff --git a/internal/handlers/widget.go b/internal/handlers/widget.go
index bff7dd7..5e515c3 100644
--- a/internal/handlers/widget.go
+++ b/internal/handlers/widget.go
@@ -777,3 +777,156 @@ func (h *Handler) HandleWidgetTaskNextDate(w http.ResponseWriter, r *http.Reques
}
w.WriteHeader(http.StatusOK)
}
+
+type availabilityBlockResponse struct {
+ ID string `json:"id"`
+ Weekday int `json:"weekday"`
+ StartTime string `json:"start_time"`
+ EndTime string `json:"end_time"`
+ Label string `json:"label"`
+}
+
+func availabilityBlockToResponse(b models.AvailabilityBlock) availabilityBlockResponse {
+ return availabilityBlockResponse{ID: b.ID, Weekday: b.Weekday, StartTime: b.StartTime, EndTime: b.EndTime, Label: b.Label}
+}
+
+// HandleWidgetAvailabilityGet returns every configured availability block.
+func (h *Handler) HandleWidgetAvailabilityGet(w http.ResponseWriter, r *http.Request) {
+ blocks, err := h.store.GetAvailabilityBlocks()
+ if err != nil {
+ http.Error(w, "failed to load availability", http.StatusInternalServerError)
+ return
+ }
+ resp := make([]availabilityBlockResponse, 0, len(blocks))
+ for _, b := range blocks {
+ resp = append(resp, availabilityBlockToResponse(b))
+ }
+ w.Header().Set("Content-Type", "application/json")
+ _ = json.NewEncoder(w).Encode(resp)
+}
+
+type availabilityCreateRequest struct {
+ Weekday int `json:"weekday"`
+ StartTime string `json:"start_time"`
+ EndTime string `json:"end_time"`
+ Label string `json:"label"`
+}
+
+// HandleWidgetAvailabilityCreate creates a new weekly availability block.
+func (h *Handler) HandleWidgetAvailabilityCreate(w http.ResponseWriter, r *http.Request) {
+ var req availabilityCreateRequest
+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+ http.Error(w, "bad request", http.StatusBadRequest)
+ return
+ }
+ if req.Weekday < 0 || req.Weekday > 6 || req.StartTime == "" || req.EndTime == "" {
+ http.Error(w, "weekday (0-6), start_time, and end_time are required", http.StatusBadRequest)
+ return
+ }
+ block, err := h.store.CreateAvailabilityBlock(req.Weekday, req.StartTime, req.EndTime, req.Label)
+ if err != nil {
+ http.Error(w, "failed to create availability block", http.StatusInternalServerError)
+ return
+ }
+ w.Header().Set("Content-Type", "application/json")
+ _ = json.NewEncoder(w).Encode(availabilityBlockToResponse(*block))
+}
+
+type availabilityDeleteRequest struct {
+ ID string `json:"id"`
+}
+
+// HandleWidgetAvailabilityDelete deletes an availability block.
+func (h *Handler) HandleWidgetAvailabilityDelete(w http.ResponseWriter, r *http.Request) {
+ var req availabilityDeleteRequest
+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+ http.Error(w, "bad request", http.StatusBadRequest)
+ return
+ }
+ if err := h.store.DeleteAvailabilityBlock(req.ID); err != nil {
+ if errors.Is(err, store.ErrNativeTaskNotFound) {
+ http.Error(w, "availability block not found", http.StatusNotFound)
+ return
+ }
+ http.Error(w, "failed to delete availability block", http.StatusInternalServerError)
+ return
+ }
+ w.WriteHeader(http.StatusOK)
+}
+
+type taskEstimateRequest struct {
+ ID string `json:"id"`
+ EstimatedMinutes int `json:"estimated_minutes"`
+}
+
+// HandleWidgetTaskEstimate sets a task's estimated duration in minutes.
+func (h *Handler) HandleWidgetTaskEstimate(w http.ResponseWriter, r *http.Request) {
+ var req taskEstimateRequest
+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+ http.Error(w, "bad request", http.StatusBadRequest)
+ return
+ }
+ if req.ID == "" {
+ http.Error(w, "id is required", http.StatusBadRequest)
+ return
+ }
+ if err := h.store.SetTaskEstimate(req.ID, req.EstimatedMinutes); err != nil {
+ if errors.Is(err, store.ErrNativeTaskNotFound) {
+ http.Error(w, "task not found", http.StatusNotFound)
+ return
+ }
+ http.Error(w, "failed to set estimate", http.StatusInternalServerError)
+ return
+ }
+ w.WriteHeader(http.StatusOK)
+}
+
+type projectBudgetTrackedRequest struct {
+ ID string `json:"id"`
+ Tracked bool `json:"tracked"`
+}
+
+// HandleWidgetProjectsBudgetTracked opts a project in or out of budget tracking.
+func (h *Handler) HandleWidgetProjectsBudgetTracked(w http.ResponseWriter, r *http.Request) {
+ var req projectBudgetTrackedRequest
+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+ http.Error(w, "bad request", http.StatusBadRequest)
+ return
+ }
+ if req.ID == "" {
+ http.Error(w, "id is required", http.StatusBadRequest)
+ return
+ }
+ if err := h.store.SetProjectBudgetTracked(req.ID, req.Tracked); err != nil {
+ if errors.Is(err, store.ErrNativeTaskNotFound) {
+ http.Error(w, "project not found", http.StatusNotFound)
+ return
+ }
+ http.Error(w, "failed to update project", http.StatusInternalServerError)
+ return
+ }
+ w.WriteHeader(http.StatusOK)
+}
+
+type labelBudgetTrackedRequest struct {
+ Name string `json:"name"`
+ Tracked bool `json:"tracked"`
+}
+
+// HandleWidgetLabelsBudgetTracked opts a label in or out of budget tracking.
+func (h *Handler) HandleWidgetLabelsBudgetTracked(w http.ResponseWriter, r *http.Request) {
+ var req labelBudgetTrackedRequest
+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+ http.Error(w, "bad request", http.StatusBadRequest)
+ return
+ }
+ if req.Name == "" {
+ http.Error(w, "name is required", http.StatusBadRequest)
+ return
+ }
+ if err := h.store.SetLabelBudgetTracked(req.Name, req.Tracked); err != nil {
+ http.Error(w, "failed to update label", http.StatusInternalServerError)
+ return
+ }
+ w.WriteHeader(http.StatusOK)
+}