This commit is contained in:
@@ -2,13 +2,20 @@ package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"log"
|
||||
"net/http"
|
||||
"smt/models"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SafeInput struct {
|
||||
SafeId int `db:"SafeId" json:"safeId"`
|
||||
SafeName string `db:"SafeName" json:"safeName"`
|
||||
}
|
||||
|
||||
// GetSafesHandler provides a list of all safes that a user has access to
|
||||
func GetSafesHandler(c *gin.Context) {
|
||||
var UserId int
|
||||
@@ -44,3 +51,46 @@ func GetAllSafesHandler(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "success", "data": safes})
|
||||
}
|
||||
|
||||
func AddSafeHandler(c *gin.Context) {
|
||||
var input SafeInput
|
||||
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if len(input.SafeName) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "no safe name specified"})
|
||||
return
|
||||
}
|
||||
|
||||
s := models.Safe{SafeId: input.SafeId, SafeName: input.SafeName}
|
||||
|
||||
//remove leading/trailing spaces in groupname
|
||||
s.SafeName = html.EscapeString(strings.TrimSpace(s.SafeName))
|
||||
|
||||
// Check if safe already exists
|
||||
testSafe, _ := models.SafeGetByName(s.SafeName)
|
||||
log.Printf("AddSafeHandler checking if safe '%s' already exists\n", s.SafeName)
|
||||
|
||||
if (models.Safe{} == testSafe) {
|
||||
log.Printf("AddSafeHandler confirmed no existing safe name\n")
|
||||
} else {
|
||||
errorString := fmt.Sprintf("attempt to register conflicting safe '%s'", s.SafeName)
|
||||
log.Printf("Register error : '%s'\n", errorString)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": errorString})
|
||||
return
|
||||
}
|
||||
|
||||
_, err := s.SafeAdd()
|
||||
|
||||
if err != nil {
|
||||
errString := fmt.Sprintf("error creating safe : '%s'", err)
|
||||
log.Printf("AddSafeHandler %s\n", errString)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": errString})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "safe creation success"})
|
||||
}
|
||||
|
1
main.go
1
main.go
@@ -260,6 +260,7 @@ func main() {
|
||||
// Other functions for admin
|
||||
adminOnly.POST("/unlock", controllers.Unlock)
|
||||
adminOnly.GET("/safe/listall", controllers.GetAllSafesHandler)
|
||||
adminOnly.GET("/safe/add", controllers.AddSafeHandler)
|
||||
|
||||
// Deprecated
|
||||
//adminOnly.GET("/roles", controllers.GetRoles)
|
||||
|
@@ -85,3 +85,30 @@ func SafeListAllowed(userId int) ([]Safe, error) {
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// SafeAdd adds a new safe definition to the database
|
||||
func (s *Safe) SafeAdd() (*Safe, error) {
|
||||
var err error
|
||||
|
||||
// Validate group not already in use
|
||||
_, err = SafeGetByName(s.SafeName)
|
||||
|
||||
if err != nil && err.Error() == "safe not found" {
|
||||
log.Printf("SafeAdd confirmed no existing safe, continuing with creation of safe '%s'\n", s.SafeName)
|
||||
|
||||
result, err := db.NamedExec(("INSERT INTO safes (SafeName) VALUES (:SafeName);"), s)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("SafeAdd error executing sql record : '%s'\n", err)
|
||||
return &Safe{}, err
|
||||
} else {
|
||||
affected, _ := result.RowsAffected()
|
||||
id, _ := result.LastInsertId()
|
||||
log.Printf("SafeAdd insert returned result id '%d' affecting %d row(s).\n", id, affected)
|
||||
}
|
||||
} else {
|
||||
log.Printf("SafeAdd safe name already exists : '%v'\n", err)
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user