add new database table workspace

This commit is contained in:
Adrian Zürcher
2026-02-14 14:47:41 +01:00
parent 206639cf72
commit 1f4f9a12aa
4 changed files with 179 additions and 8 deletions

View File

@@ -82,6 +82,14 @@ func NewAccessHandler(path string, logger *logging.Logger) (aH *AccessHandler, e
return
}
logger.Debug("NewAccessHandler", "add workspace table")
// Add the role table to the database
err = aH.AddWorkspaceTable()
if err != nil {
aH.logger.Error("NewAccessHandler", err)
return
}
//indicator that user database was created
aH.NewDBCreated = aH.dbHandler.NewCreatedDB

154
handlers/workspace.go Normal file
View File

@@ -0,0 +1,154 @@
package handlers
import (
"fmt"
"net/http"
"strconv"
"gitea.tecamino.com/paadi/access-handler/models"
"github.com/gin-gonic/gin"
)
func (aH *AccessHandler) AddWorkspaceTable() error {
return aH.dbHandler.AddNewTable(models.Workspace{})
}
func (aH *AccessHandler) AddWorkspace(c *gin.Context) {
var workspace models.Workspace
err := c.BindJSON(&workspace)
if err != nil {
aH.logger.Error("AddWorkspace", err)
c.JSON(http.StatusBadRequest, models.NewJsonErrorResponse(err))
return
}
// Check if a workspace with this name already exists
if aH.dbHandler.Exists(&models.Workspace{}, "workspace", workspace.Name, false) {
aH.logger.Error("AddWorkspace", fmt.Sprintf("workspace with name %s already exists", workspace.Name))
c.JSON(http.StatusBadRequest, models.NewJsonMessageResponse(fmt.Sprintf("workspace with name %s already exists", workspace.Name)))
return
}
// Insert new workspace with provided permissions
aH.dbHandler.AddNewColum(&models.Workspace{
Name: workspace.Name,
Description: workspace.Description,
})
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("workspace '%s' successfully added", workspace.Name),
})
}
func (aH *AccessHandler) GetWorkspace(c *gin.Context) {
var i int
var err error
var workspaces []models.Workspace
workspace := c.Query("workspace")
id := c.Query("id")
if workspace != "" {
err = aH.dbHandler.GetByKey(&workspaces, "workspace", workspace, false)
} else if id != "" {
i, err = strconv.Atoi(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": err.Error(),
})
return
}
err = aH.dbHandler.GetById(&workspaces, uint(i))
} else {
err = aH.dbHandler.GetById(&workspaces, 0)
}
if err != nil {
aH.logger.Error("GetWorkspace", err)
c.JSON(http.StatusInternalServerError, nil)
return
}
c.JSON(http.StatusOK, workspaces)
}
func (aH *AccessHandler) UpdateWorkspace(c *gin.Context) {
var workspace models.Workspace
if err := c.BindJSON(&workspace); err != nil {
aH.logger.Error("UpdateWorkspace", err)
c.JSON(http.StatusInternalServerError, nil)
return
}
if workspace.Id != 0 {
err := aH.dbHandler.UpdateValuesById(&workspace, workspace.Id)
if err != nil {
aH.logger.Error("UpdateWorkspace", err)
c.JSON(http.StatusInternalServerError, nil)
return
}
} else {
err := aH.dbHandler.UpdateValuesByKey(&workspace, "", "workspace", workspace.Name)
if err != nil {
aH.logger.Error("UpdateWorkspace", err)
c.JSON(http.StatusInternalServerError, nil)
return
}
}
c.JSON(http.StatusOK, models.NewJsonMessageResponse("successfully updated workspace '"+workspace.Name+"'"))
}
func (aH *AccessHandler) DeleteWorkspace(c *gin.Context) {
queryWorkspace := c.Query("workspace")
if queryWorkspace == "" || queryWorkspace == "null" || queryWorkspace == "undefined" {
aH.logger.Error("DeleteWorkspace", "id query missing or wrong value: "+queryWorkspace)
c.JSON(http.StatusInternalServerError, nil)
return
}
var request struct {
Workspaces []string `json:"workspaces"`
}
err := c.BindJSON(&request)
if err != nil {
aH.logger.Error("DeleteWorkspace", err)
c.JSON(http.StatusBadRequest, nil)
return
}
if len(request.Workspaces) == 0 {
aH.logger.Error("DeleteWorkspace", "no ids given to be deleted")
c.JSON(http.StatusBadRequest, models.NewJsonMessageResponse("no workspaces given to be deleted"))
return
}
var ownWorkspace string
for _, workspace := range request.Workspaces {
if queryWorkspace == workspace {
ownWorkspace = workspace
continue
}
err = aH.dbHandler.DeleteByKey(&models.Workspace{}, "workspace", workspace, false)
if err != nil {
aH.logger.Error("DeleteWorkspace", err)
c.JSON(http.StatusInternalServerError, nil)
return
}
}
if ownWorkspace != "" {
aH.logger.Error("DeleteWorkspace", "can not delete logged in workspace id: "+ownWorkspace)
c.JSON(http.StatusBadRequest, gin.H{
"message": "can not delete logged in workspace id: " + ownWorkspace,
"workspace": ownWorkspace,
})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "workspace(s) deleted",
})
}