115 lines
3.1 KiB
Go
115 lines
3.1 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"inbox/internal/app/workspaceprovision"
|
|
"inbox/internal/base/httpx"
|
|
"inbox/internal/domain/workspace"
|
|
)
|
|
|
|
func (h *Handler) listProjects(w http.ResponseWriter, r *http.Request) {
|
|
items, err := h.Workspaces.ListProjects(r.Context())
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
if items == nil {
|
|
items = []workspace.Project{}
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, map[string]any{"projects": items})
|
|
}
|
|
|
|
func (h *Handler) createProject(w http.ResponseWriter, r *http.Request) {
|
|
type request struct {
|
|
Slug string `json:"slug"`
|
|
Name string `json:"name"`
|
|
RootPath string `json:"root_path"`
|
|
DefaultBranch string `json:"default_branch"`
|
|
Status string `json:"status"`
|
|
}
|
|
var req request
|
|
if err := httpx.DecodeJSON(r, &req); err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
item, err := h.Workspaces.CreateProject(r.Context(), workspace.Project{
|
|
Slug: req.Slug,
|
|
Name: req.Name,
|
|
RootPath: req.RootPath,
|
|
DefaultBranch: req.DefaultBranch,
|
|
Status: req.Status,
|
|
})
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusCreated, item)
|
|
}
|
|
|
|
func (h *Handler) listWorkspaces(w http.ResponseWriter, r *http.Request) {
|
|
items, err := h.Workspaces.ListWorkspaces(r.Context(), r.URL.Query().Get("project_id"))
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
if items == nil {
|
|
items = []workspace.Workspace{}
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, map[string]any{"workspaces": items})
|
|
}
|
|
|
|
func (h *Handler) createWorkspace(w http.ResponseWriter, r *http.Request) {
|
|
type request struct {
|
|
ProjectID string `json:"project_id"`
|
|
Slug string `json:"slug"`
|
|
Name string `json:"name"`
|
|
RootPath string `json:"root_path"`
|
|
BaseBranch string `json:"base_branch"`
|
|
WorktreeBranch string `json:"worktree_branch"`
|
|
Status string `json:"status"`
|
|
}
|
|
var req request
|
|
if err := httpx.DecodeJSON(r, &req); err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
item, err := h.Workspaces.CreateWorkspace(r.Context(), workspace.Workspace{
|
|
ProjectID: req.ProjectID,
|
|
Slug: req.Slug,
|
|
Name: req.Name,
|
|
RootPath: req.RootPath,
|
|
BaseBranch: req.BaseBranch,
|
|
WorktreeBranch: req.WorktreeBranch,
|
|
Status: req.Status,
|
|
})
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusCreated, item)
|
|
}
|
|
|
|
func (h *Handler) provisionWorkspace(w http.ResponseWriter, r *http.Request) {
|
|
type request struct {
|
|
ProjectDir string `json:"project_dir"`
|
|
Name string `json:"name"`
|
|
}
|
|
var req request
|
|
if err := httpx.DecodeJSON(r, &req); err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
item, _, err := h.WorkspaceOps.Provision(r.Context(), workspaceprovision.ProvisionRequest{
|
|
ProjectDir: req.ProjectDir,
|
|
Name: req.Name,
|
|
})
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusCreated, map[string]any{
|
|
"workspace": item,
|
|
})
|
|
}
|