package server import ( "errors" "fmt" "net/http" "github.com/gin-gonic/gin" "edge-infra.dev/pkg/lib/fog" rulesengine "edge-infra.dev/pkg/sds/emergencyaccess/rules" ) func (res RulesEngineService) deleteCommand(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") name := c.Param("name") log = log.WithValues("commandName", name) c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log)) result, err := res.RulesEngine.DeleteCommand(c.Request.Context(), name) if err != nil { log.Error(err, "Deletion error", "command", name) c.Status(http.StatusInternalServerError) return } if len(result.Errors) > 0 { for _, err := range result.Errors { if err.Type == rulesengine.Conflict { log.Error(fmt.Errorf("database query conflict"), "Conflict with database") c.JSON(http.StatusConflict, result) return } } log.Error(fmt.Errorf("error deleting command"), "Requested data not found. No deletion performend") c.JSON(http.StatusNotFound, result) return } c.Status(http.StatusOK) } func (res RulesEngineService) readCommands(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") vals, err := res.RulesEngine.ReadCommands(c.Request.Context()) if err != nil { log.Error(err, "Read error") c.Status(http.StatusInternalServerError) return } if len(vals) == 0 { log.Info("No commands returned") c.JSON(http.StatusOK, nil) return } c.JSON(http.StatusOK, vals) } //nolint:dupl func (res RulesEngineService) postCommands(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") var payload []rulesengine.PostCommandPayload if err := c.ShouldBindJSON(&payload); err != nil { log.Error(err, "bind error") c.Status(http.StatusBadRequest) return } if len(payload) == 0 { log.Error(errors.New("payload was nil"), "validation error") c.Status(http.StatusBadRequest) return } result, err := res.RulesEngine.AddCommands(c.Request.Context(), payload) if err != nil { log.Error(err, "post commands error") c.Status(http.StatusInternalServerError) return } if len(result.Conflicts) > 0 { log.Info("Conflicts with query") c.JSON(http.StatusConflict, result) return } c.Status(http.StatusOK) } func (res RulesEngineService) readCommand(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") name := c.Param("name") comm, err := res.RulesEngine.ReadCommand(c.Request.Context(), name) if err != nil { log.Error(err, "read error") c.Status(http.StatusInternalServerError) return } if comm.Name == "" || comm.ID == "" { log.Info("No command returned") c.JSON(http.StatusOK, nil) return } c.JSON(http.StatusOK, comm) }