1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: insert label into database

This commit is contained in:
Diptesh Choudhuri 2023-01-24 13:23:41 +00:00
parent 57c1582ed1
commit 0b8181acab

View File

@ -3,6 +3,12 @@ package routes
import ( import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"net/http"
"sort"
"strconv"
"strings"
"time"
datastructure "github.com/duke-git/lancet/v2/datastructure/set" datastructure "github.com/duke-git/lancet/v2/datastructure/set"
"github.com/emvi/logbuch" "github.com/emvi/logbuch"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -15,11 +21,6 @@ import (
"github.com/muety/wakapi/services" "github.com/muety/wakapi/services"
"github.com/muety/wakapi/services/imports" "github.com/muety/wakapi/services/imports"
"github.com/muety/wakapi/utils" "github.com/muety/wakapi/utils"
"net/http"
"sort"
"strconv"
"strings"
"time"
) )
const criticalError = "a critical error has occurred, sorry" const criticalError = "a critical error has occurred, sorry"
@ -139,6 +140,8 @@ func (h *SettingsHandler) dispatchAction(action string) action {
return h.actionDeleteAlias return h.actionDeleteAlias
case "add_alias": case "add_alias":
return h.actionAddAlias return h.actionAddAlias
case "add_project_to_label":
return h.addProjectToLabel
case "add_label": case "add_label":
return h.actionAddLabel return h.actionAddLabel
case "delete_label": case "delete_label":
@ -382,6 +385,29 @@ func (h *SettingsHandler) actionAddLabel(w http.ResponseWriter, r *http.Request)
return http.StatusOK, "label added successfully", "" return http.StatusOK, "label added successfully", ""
} }
func (h *SettingsHandler) addProjectToLabel(w http.ResponseWriter, r *http.Request) (int, string, string) {
if h.config.IsDev() {
loadTemplates()
}
user := middlewares.GetPrincipal(r)
label := &models.ProjectLabel{
UserID: user.ID,
ProjectKey: r.PostFormValue("project"),
Label: r.PostFormValue("label"),
}
if !label.IsValid() {
return http.StatusBadRequest, "", "invalid input"
}
if _, err := h.projectLabelSrvc.Create(label); err != nil {
// TODO: distinguish between bad request, conflict and server error
return http.StatusBadRequest, "", "invalid input"
}
return http.StatusOK, "added project to label successfully", ""
}
func (h *SettingsHandler) actionDeleteLabel(w http.ResponseWriter, r *http.Request) (int, string, string) { func (h *SettingsHandler) actionDeleteLabel(w http.ResponseWriter, r *http.Request) (int, string, string) {
if h.config.IsDev() { if h.config.IsDev() {
loadTemplates() loadTemplates()