// SPDX-License-Identifier: AGPL-3.0-or-later package httpapi import ( "context" "net/http" "strconv" "docwell/server/internal/access" "docwell/server/internal/directory" ) // FavoriteService is the part of directory.Service for favorites and recent pages. type FavoriteService interface { ListFavorites(ctx context.Context, c access.Caller, wsID string) ([]directory.PageItem, error) SetFavorite(ctx context.Context, c access.Caller, pageID string, on bool) error RecordVisit(ctx context.Context, c access.Caller, pageID string) error ListRecent(ctx context.Context, c access.Caller, wsID string, limit int) ([]directory.PageItem, error) } func (a *api) favoriteRoutes(mux *routeMux) { mux.HandleFunc("GET /api/v1/workspaces/{id}/favorites", a.requireUser(func(w http.ResponseWriter, r *http.Request) { list, err := a.dir.ListFavorites(r.Context(), caller(r), r.PathValue("id")) a.respond(w, list, err) })) mux.HandleFunc("GET /api/v1/workspaces/{id}/recent", a.requireUser(func(w http.ResponseWriter, r *http.Request) { limit, _ := strconv.Atoi(r.URL.Query().Get("limit")) list, err := a.dir.ListRecent(r.Context(), caller(r), r.PathValue("id"), limit) a.respond(w, list, err) })) mux.HandleFunc("PUT /api/v1/pages/{id}/favorite", a.requireUser(func(w http.ResponseWriter, r *http.Request) { a.respondNoContent(w, a.dir.SetFavorite(r.Context(), caller(r), r.PathValue("id"), true)) })) mux.HandleFunc("DELETE /api/v1/pages/{id}/favorite", a.requireUser(func(w http.ResponseWriter, r *http.Request) { a.respondNoContent(w, a.dir.SetFavorite(r.Context(), caller(r), r.PathValue("id"), false)) })) mux.HandleFunc("POST /api/v1/pages/{id}/visit", a.requireUser(func(w http.ResponseWriter, r *http.Request) { a.respondNoContent(w, a.dir.RecordVisit(r.Context(), caller(r), r.PathValue("id"))) })) }