mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 23:44:22 +00:00
Rework api
This commit is contained in:
@@ -2,15 +2,13 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"novamd/internal/db"
|
"novamd/internal/db"
|
||||||
"novamd/internal/filesystem"
|
"novamd/internal/filesystem"
|
||||||
"novamd/internal/models"
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ListFiles(fs *filesystem.FileSystem) http.HandlerFunc {
|
func ListFiles(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
@@ -63,7 +61,7 @@ func GetFileContent(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath := strings.TrimPrefix(r.URL.Path, "/api/v1/files/")
|
filePath := chi.URLParam(r, "*")
|
||||||
content, err := fs.GetFileContent(userID, workspaceID, filePath)
|
content, err := fs.GetFileContent(userID, workspaceID, filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to read file", http.StatusNotFound)
|
http.Error(w, "Failed to read file", http.StatusNotFound)
|
||||||
@@ -83,7 +81,7 @@ func SaveFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath := strings.TrimPrefix(r.URL.Path, "/api/v1/files/")
|
filePath := chi.URLParam(r, "*")
|
||||||
content, err := io.ReadAll(r.Body)
|
content, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to read request body", http.StatusBadRequest)
|
http.Error(w, "Failed to read request body", http.StatusBadRequest)
|
||||||
@@ -108,7 +106,7 @@ func DeleteFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath := strings.TrimPrefix(r.URL.Path, "/api/v1/files/")
|
filePath := chi.URLParam(r, "*")
|
||||||
err = fs.DeleteFile(userID, workspaceID, filePath)
|
err = fs.DeleteFile(userID, workspaceID, filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to delete file", http.StatusInternalServerError)
|
http.Error(w, "Failed to delete file", http.StatusInternalServerError)
|
||||||
@@ -120,60 +118,25 @@ func DeleteFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSettings(db *db.DB) http.HandlerFunc {
|
func GetLastOpenedFile(db *db.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
_, workspaceID, err := getUserAndWorkspaceIDs(r)
|
userID, _, err := getUserAndWorkspaceIDs(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
settings, err := db.GetWorkspaceSettings(workspaceID)
|
user, err := db.GetUserByID(userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, "Failed to get user", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
respondJSON(w, settings)
|
respondJSON(w, map[string]string{"lastOpenedFile": user.LastOpenedFilePath})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateSettings(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
func UpdateLastOpenedFile(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var settings models.WorkspaceSettings
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&settings); err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
settings.WorkspaceID = workspaceID
|
|
||||||
|
|
||||||
if err := db.SaveWorkspaceSettings(&settings); err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if settings.Settings.GitEnabled {
|
|
||||||
err := fs.SetupGitRepo(userID, workspaceID, settings.Settings.GitURL, settings.Settings.GitUser, settings.Settings.GitToken)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Failed to setup git repo", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fs.DisableGitRepo(userID, workspaceID)
|
|
||||||
}
|
|
||||||
|
|
||||||
respondJSON(w, settings)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -182,7 +145,7 @@ func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var requestBody struct {
|
var requestBody struct {
|
||||||
Message string `json:"message"`
|
FilePath string `json:"filePath"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
|
||||||
@@ -190,54 +153,18 @@ func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if requestBody.Message == "" {
|
// Validate that the file path is valid within the workspace
|
||||||
http.Error(w, "Commit message is required", http.StatusBadRequest)
|
_, err = fs.ValidatePath(userID, workspaceID, requestBody.FilePath)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = fs.StageCommitAndPush(userID, workspaceID, requestBody.Message)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to stage, commit, and push changes: "+err.Error(), http.StatusInternalServerError)
|
http.Error(w, "Invalid file path", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
respondJSON(w, map[string]string{"message": "Changes staged, committed, and pushed successfully"})
|
if err := db.UpdateLastOpenedFile(userID, requestBody.FilePath); err != nil {
|
||||||
}
|
http.Error(w, "Failed to update last opened file", http.StatusInternalServerError)
|
||||||
}
|
|
||||||
|
|
||||||
func PullChanges(fs *filesystem.FileSystem) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = fs.Pull(userID, workspaceID)
|
respondJSON(w, map[string]string{"message": "Last opened file updated successfully"})
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Failed to pull changes: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
respondJSON(w, map[string]string{"message": "Pulled changes from remote"})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUserAndWorkspaceIDs(r *http.Request) (int, int, error) {
|
|
||||||
userID, err := strconv.Atoi(r.URL.Query().Get("userId"))
|
|
||||||
if err != nil {
|
|
||||||
return 0, 0, errors.New("invalid userId")
|
|
||||||
}
|
|
||||||
|
|
||||||
workspaceID, err := strconv.Atoi(r.URL.Query().Get("workspaceId"))
|
|
||||||
if err != nil {
|
|
||||||
return 0, 0, errors.New("invalid workspaceId")
|
|
||||||
}
|
|
||||||
|
|
||||||
return userID, workspaceID, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func respondJSON(w http.ResponseWriter, data interface{}) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
json.NewEncoder(w).Encode(data)
|
|
||||||
}
|
|
||||||
58
backend/internal/api/git_handlers.go
Normal file
58
backend/internal/api/git_handlers.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"novamd/internal/filesystem"
|
||||||
|
)
|
||||||
|
|
||||||
|
func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var requestBody struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
|
||||||
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if requestBody.Message == "" {
|
||||||
|
http.Error(w, "Commit message is required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fs.StageCommitAndPush(userID, workspaceID, requestBody.Message)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Failed to stage, commit, and push changes: "+err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondJSON(w, map[string]string{"message": "Changes staged, committed, and pushed successfully"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PullChanges(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fs.Pull(userID, workspaceID)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Failed to pull changes: "+err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondJSON(w, map[string]string{"message": "Pulled changes from remote"})
|
||||||
|
}
|
||||||
|
}
|
||||||
37
backend/internal/api/handler_utils.go
Normal file
37
backend/internal/api/handler_utils.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getUserID(r *http.Request) (int, error) {
|
||||||
|
userIDStr := chi.URLParam(r, "userId")
|
||||||
|
return strconv.Atoi(userIDStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUserAndWorkspaceIDs(r *http.Request) (int, int, error) {
|
||||||
|
userID, err := getUserID(r)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, errors.New("invalid userId")
|
||||||
|
}
|
||||||
|
|
||||||
|
workspaceIDStr := chi.URLParam(r, "workspaceId")
|
||||||
|
workspaceID, err := strconv.Atoi(workspaceIDStr)
|
||||||
|
if err != nil {
|
||||||
|
return userID, 0, errors.New("invalid workspaceId")
|
||||||
|
}
|
||||||
|
|
||||||
|
return userID, workspaceID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func respondJSON(w http.ResponseWriter, data interface{}) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||||||
|
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,21 +8,50 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func SetupRoutes(r chi.Router, db *db.DB, fs *filesystem.FileSystem) {
|
func SetupRoutes(r chi.Router, db *db.DB, fs *filesystem.FileSystem) {
|
||||||
r.Route("/", func(r chi.Router) {
|
r.Route("/api/v1", func(r chi.Router) {
|
||||||
r.Route("/settings", func(r chi.Router) {
|
// User routes
|
||||||
r.Get("/", GetSettings(db))
|
r.Route("/users/{userId}", func(r chi.Router) {
|
||||||
r.Post("/", UpdateSettings(db, fs))
|
r.Get("/", GetUser(db))
|
||||||
})
|
|
||||||
r.Route("/files", func(r chi.Router) {
|
// Workspace routes
|
||||||
r.Get("/", ListFiles(fs))
|
r.Route("/workspaces", func(r chi.Router) {
|
||||||
r.Get("/*", GetFileContent(fs))
|
r.Get("/", ListWorkspaces(db))
|
||||||
r.Post("/*", SaveFile(fs))
|
r.Post("/", CreateWorkspace(db))
|
||||||
r.Delete("/*", DeleteFile(fs))
|
r.Get("/last", GetLastWorkspace(db))
|
||||||
r.Get("/lookup", LookupFileByName(fs))
|
r.Put("/last", UpdateLastWorkspace(db))
|
||||||
})
|
|
||||||
r.Route("/git", func(r chi.Router) {
|
r.Route("/{workspaceId}", func(r chi.Router) {
|
||||||
r.Post("/commit", StageCommitAndPush(fs))
|
r.Get("/", GetWorkspace(db))
|
||||||
r.Post("/pull", PullChanges(fs))
|
r.Put("/", UpdateWorkspace(db))
|
||||||
|
r.Delete("/", DeleteWorkspace(db))
|
||||||
|
|
||||||
|
// File routes
|
||||||
|
r.Route("/files", func(r chi.Router) {
|
||||||
|
r.Get("/", ListFiles(fs))
|
||||||
|
r.Get("/last", GetLastOpenedFile(db))
|
||||||
|
r.Put("/last", UpdateLastOpenedFile(db, fs))
|
||||||
|
r.Get("/lookup", LookupFileByName(fs)) // Moved here
|
||||||
|
|
||||||
|
r.Route("/*", func(r chi.Router) {
|
||||||
|
r.Post("/", SaveFile(fs))
|
||||||
|
r.Get("/", GetFileContent(fs))
|
||||||
|
r.Delete("/", DeleteFile(fs))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// Settings routes
|
||||||
|
r.Route("/settings", func(r chi.Router) {
|
||||||
|
r.Get("/", GetWorkspaceSettings(db))
|
||||||
|
r.Put("/", UpdateWorkspaceSettings(db, fs))
|
||||||
|
})
|
||||||
|
|
||||||
|
// Git routes
|
||||||
|
r.Route("/git", func(r chi.Router) {
|
||||||
|
r.Post("/commit", StageCommitAndPush(fs))
|
||||||
|
r.Post("/pull", PullChanges(fs))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
25
backend/internal/api/user_handlers.go
Normal file
25
backend/internal/api/user_handlers.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"novamd/internal/db"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetUser(db *db.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, err := getUserID(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := db.GetUserByID(userID)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Failed to get user", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondJSON(w, user)
|
||||||
|
}
|
||||||
|
}
|
||||||
246
backend/internal/api/workspace_handlers.go
Normal file
246
backend/internal/api/workspace_handlers.go
Normal file
@@ -0,0 +1,246 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"novamd/internal/db"
|
||||||
|
"novamd/internal/filesystem"
|
||||||
|
"novamd/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ListWorkspaces(db *db.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, err := getUserID(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
workspaces, err := db.GetWorkspacesByUserID(userID)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Failed to list workspaces", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondJSON(w, workspaces)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateWorkspace(db *db.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, err := getUserID(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var requestBody struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
|
||||||
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
workspace := &models.Workspace{
|
||||||
|
UserID: userID,
|
||||||
|
Name: requestBody.Name,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.CreateWorkspace(workspace); err != nil {
|
||||||
|
http.Error(w, "Failed to create workspace", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondJSON(w, workspace)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetWorkspace(db *db.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
workspace, err := db.GetWorkspaceByID(workspaceID)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Workspace not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if workspace.UserID != userID {
|
||||||
|
http.Error(w, "Unauthorized access to workspace", http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondJSON(w, map[string]string{"name": workspace.Name})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateWorkspace(db *db.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var requestBody struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
|
||||||
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
workspace, err := db.GetWorkspaceByID(workspaceID)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Workspace not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if workspace.UserID != userID {
|
||||||
|
http.Error(w, "Unauthorized access to workspace", http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
workspace.Name = requestBody.Name
|
||||||
|
if err := db.UpdateWorkspace(workspace); err != nil {
|
||||||
|
http.Error(w, "Failed to update workspace", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondJSON(w, map[string]string{"name": workspace.Name})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteWorkspace(db *db.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
workspace, err := db.GetWorkspaceByID(workspaceID)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Workspace not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if workspace.UserID != userID {
|
||||||
|
http.Error(w, "Unauthorized access to workspace", http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.DeleteWorkspace(workspaceID); err != nil {
|
||||||
|
http.Error(w, "Failed to delete workspace", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte("Workspace deleted successfully"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetLastWorkspace(db *db.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, err := getUserID(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
workspaceID, err := db.GetLastWorkspaceID(userID)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Failed to get last workspace", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondJSON(w, map[string]int{"lastWorkspaceId": workspaceID})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateLastWorkspace(db *db.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, err := getUserID(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var requestBody struct {
|
||||||
|
WorkspaceID int `json:"workspaceId"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
|
||||||
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.UpdateLastWorkspace(userID, requestBody.WorkspaceID); err != nil {
|
||||||
|
http.Error(w, "Failed to update last workspace", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondJSON(w, map[string]string{"message": "Last workspace updated successfully"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetWorkspaceSettings(db *db.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, workspaceID, err := getUserAndWorkspaceIDs(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
settings, err := db.GetWorkspaceSettings(workspaceID)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondJSON(w, settings)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateWorkspaceSettings(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var settings models.WorkspaceSettings
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&settings); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
settings.WorkspaceID = workspaceID
|
||||||
|
|
||||||
|
if err := db.SaveWorkspaceSettings(&settings); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if settings.Settings.GitEnabled {
|
||||||
|
err := fs.SetupGitRepo(userID, workspaceID, settings.Settings.GitURL, settings.Settings.GitUser, settings.Settings.GitToken)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Failed to setup git repo", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fs.DisableGitRepo(userID, workspaceID)
|
||||||
|
}
|
||||||
|
|
||||||
|
respondJSON(w, settings)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -46,3 +46,14 @@ func (db *DB) GetWorkspacesByUserID(userID int) ([]*models.Workspace, error) {
|
|||||||
}
|
}
|
||||||
return workspaces, nil
|
return workspaces, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) UpdateWorkspace(workspace *models.Workspace) error {
|
||||||
|
_, err := db.Exec("UPDATE workspaces SET name = ? WHERE id = ? AND user_id = ?",
|
||||||
|
workspace.Name, workspace.ID, workspace.UserID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) DeleteWorkspace(id int) error {
|
||||||
|
_, err := db.Exec("DELETE FROM workspaces WHERE id = ?", id)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user