package handlers import ( "fmt" "net/http" "os" "path/filepath" "slices" "strconv" "strings" "gitea.tecamino.com/paadi/access-handler/models" "github.com/gin-gonic/gin" "github.com/google/uuid" ) 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{}, "name", 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 } uid, err := uuid.NewUUID() if err != nil { aH.logger.Error("AddWorkspace", err) c.JSON(http.StatusBadRequest, models.NewJsonErrorResponse(err)) return } // create new folder err = os.MkdirAll(uid.String(), 0644) if err != nil { aH.logger.Error("AddWorkspace", err) c.JSON(http.StatusBadRequest, models.NewJsonErrorResponse(err)) return } // Insert new workspace with provided permissions aH.dbHandler.AddNewColum(&models.Workspace{ Name: workspace.Name, Description: workspace.Description, Uuid: &uid, }) 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) { var request struct { Workspaces []models.Workspace `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 } for _, workspace := range request.Workspaces { err = aH.dbHandler.DeleteByKey(&models.Workspace{}, "id", workspace.Id, false) if err != nil { aH.logger.Error("DeleteWorkspace", err) c.JSON(http.StatusInternalServerError, nil) return } var users []models.User if err := aH.dbHandler.GetById(&users, 0); err != nil { aH.logger.Error("DeleteWorkspace", err) c.JSON(http.StatusInternalServerError, nil) return } for _, u := range users { if u.WorkspaceID == nil { continue } else if *u.WorkspaceID == workspace.Id { u.WorkspaceID = nil } u.Workspaces = slices.DeleteFunc(u.Workspaces, func(w *models.Workspace) bool { return w != nil && w.Name == workspace.Name }) if err := aH.dbHandler.UpdateValuesById(&u, u.Id); err != nil { aH.logger.Error("DeleteWorkspace", err) c.JSON(http.StatusInternalServerError, nil) return } } // remove folder err = os.RemoveAll(workspace.Uuid.String()) if err != nil { aH.logger.Error("DeleteWorkspace", err) c.JSON(http.StatusBadRequest, models.NewJsonErrorResponse(err)) return } } c.JSON(http.StatusOK, gin.H{ "message": "workspace(s) deleted", }) } func (aH *AccessHandler) ReadWorkspaceData(c *gin.Context) { var workspace models.Workspace err := c.ShouldBindBodyWithJSON(&workspace) if err != nil { aH.logger.Error("ReadWorkspaceData", err) c.JSON(http.StatusBadRequest, nil) return } if workspace.Uuid == nil { aH.logger.Error("ReadWorkspaceData", "uuid nil") c.JSON(http.StatusBadRequest, nil) return } fmt.Println(123, workspace) files, err := os.ReadDir(workspace.Uuid.String()) fmt.Println(124, err) if err != nil { aH.logger.Error("ReadWorkspaceData", err) c.JSON(http.StatusBadRequest, nil) return } var response []string for _, f := range files { if f.IsDir() || !strings.Contains(filepath.Ext(f.Name()), ".db") { continue } response = append(response, f.Name()) } c.JSON(http.StatusOK, gin.H{ "data": response, }) }