From b9410156327ea44deb58eb72da9bcb317deb712b Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Sat, 29 Jun 2024 17:15:21 +1200 Subject: [PATCH] Consolidate API tag functionality --- server/apiv1/api.go | 71 -------------------------------------------- server/apiv1/tags.go | 71 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 71 deletions(-) diff --git a/server/apiv1/api.go b/server/apiv1/api.go index f57a78e..80888c0 100644 --- a/server/apiv1/api.go +++ b/server/apiv1/api.go @@ -523,77 +523,6 @@ func SetReadStatus(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte("ok")) } -// GetAllTags (method: GET) will get all tags currently in use -func GetAllTags(w http.ResponseWriter, _ *http.Request) { - // swagger:route GET /api/v1/tags tags GetAllTags - // - // # Get all current tags - // - // Returns a JSON array of all unique message tags. - // - // Produces: - // - application/json - // - // Schemes: http, https - // - // Responses: - // 200: ArrayResponse - // default: ErrorResponse - - w.Header().Add("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(storage.GetAllTags()); err != nil { - httpError(w, err.Error()) - } -} - -// SetMessageTags (method: PUT) will set the tags for all provided IDs -func SetMessageTags(w http.ResponseWriter, r *http.Request) { - // swagger:route PUT /api/v1/tags tags SetTags - // - // # Set message tags - // - // This will overwrite any existing tags for selected message database IDs. To remove all tags from a message, pass an empty tags array. - // - // Consumes: - // - application/json - // - // Produces: - // - text/plain - // - // Schemes: http, https - // - // Responses: - // 200: OKResponse - // default: ErrorResponse - - decoder := json.NewDecoder(r.Body) - - var data struct { - Tags []string - IDs []string - } - - err := decoder.Decode(&data) - if err != nil { - httpError(w, err.Error()) - return - } - - ids := data.IDs - - if len(ids) > 0 { - for _, id := range ids { - if _, err := storage.SetMessageTags(id, data.Tags); err != nil { - httpError(w, err.Error()) - return - } - } - } - - w.Header().Add("Content-Type", "text/plain") - _, _ = w.Write([]byte("ok")) -} - // ReleaseMessage (method: POST) will release a message via a pre-configured external SMTP server. func ReleaseMessage(w http.ResponseWriter, r *http.Request) { // swagger:route POST /api/v1/message/{ID}/release message ReleaseMessage diff --git a/server/apiv1/tags.go b/server/apiv1/tags.go index 7eef448..f9e154b 100644 --- a/server/apiv1/tags.go +++ b/server/apiv1/tags.go @@ -9,6 +9,77 @@ import ( "github.com/gorilla/mux" ) +// GetAllTags (method: GET) will get all tags currently in use +func GetAllTags(w http.ResponseWriter, _ *http.Request) { + // swagger:route GET /api/v1/tags tags GetAllTags + // + // # Get all current tags + // + // Returns a JSON array of all unique message tags. + // + // Produces: + // - application/json + // + // Schemes: http, https + // + // Responses: + // 200: ArrayResponse + // default: ErrorResponse + + w.Header().Add("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(storage.GetAllTags()); err != nil { + httpError(w, err.Error()) + } +} + +// SetMessageTags (method: PUT) will set the tags for all provided IDs +func SetMessageTags(w http.ResponseWriter, r *http.Request) { + // swagger:route PUT /api/v1/tags tags SetTags + // + // # Set message tags + // + // This will overwrite any existing tags for selected message database IDs. To remove all tags from a message, pass an empty tags array. + // + // Consumes: + // - application/json + // + // Produces: + // - text/plain + // + // Schemes: http, https + // + // Responses: + // 200: OKResponse + // default: ErrorResponse + + decoder := json.NewDecoder(r.Body) + + var data struct { + Tags []string + IDs []string + } + + err := decoder.Decode(&data) + if err != nil { + httpError(w, err.Error()) + return + } + + ids := data.IDs + + if len(ids) > 0 { + for _, id := range ids { + if _, err := storage.SetMessageTags(id, data.Tags); err != nil { + httpError(w, err.Error()) + return + } + } + } + + w.Header().Add("Content-Type", "text/plain") + _, _ = w.Write([]byte("ok")) +} + // RenameTag (method: PUT) used to rename a tag func RenameTag(w http.ResponseWriter, r *http.Request) { // swagger:route PUT /api/v1/tags/{tag} tags RenameTag