Refactor API routes to include "_op" prefix for last workspace and file operations

This commit is contained in:
2025-07-08 20:16:46 +02:00
parent a789c62a68
commit 48d42a92c9
5 changed files with 21 additions and 21 deletions

View File

@@ -114,8 +114,8 @@ func setupRouter(o Options) *chi.Mux {
r.Route("/workspaces", func(r chi.Router) { r.Route("/workspaces", func(r chi.Router) {
r.Get("/", handler.ListWorkspaces()) r.Get("/", handler.ListWorkspaces())
r.Post("/", handler.CreateWorkspace()) r.Post("/", handler.CreateWorkspace())
r.Get("/last", handler.GetLastWorkspaceName()) r.Get("/_op/last", handler.GetLastWorkspaceName())
r.Put("/last", handler.UpdateLastWorkspaceName()) r.Put("/_op/last", handler.UpdateLastWorkspaceName())
// Single workspace routes // Single workspace routes
r.Route("/{workspaceName}", func(r chi.Router) { r.Route("/{workspaceName}", func(r chi.Router) {
@@ -129,9 +129,9 @@ func setupRouter(o Options) *chi.Mux {
// File routes // File routes
r.Route("/files", func(r chi.Router) { r.Route("/files", func(r chi.Router) {
r.Get("/", handler.ListFiles()) r.Get("/", handler.ListFiles())
r.Get("/last", handler.GetLastOpenedFile()) r.Get("/_op/last", handler.GetLastOpenedFile())
r.Put("/last", handler.UpdateLastOpenedFile()) r.Put("/_op/last", handler.UpdateLastOpenedFile())
r.Get("/lookup", handler.LookupFileByName()) r.Get("/_op/lookup", handler.LookupFileByName())
r.Post("/*", handler.SaveFile()) r.Post("/*", handler.SaveFile())
r.Get("/*", handler.GetFileContent()) r.Get("/*", handler.GetFileContent())

View File

@@ -90,7 +90,7 @@ func (h *Handler) ListFiles() http.HandlerFunc {
// @Success 200 {object} LookupResponse // @Success 200 {object} LookupResponse
// @Failure 400 {object} ErrorResponse "Filename is required" // @Failure 400 {object} ErrorResponse "Filename is required"
// @Failure 404 {object} ErrorResponse "File not found" // @Failure 404 {object} ErrorResponse "File not found"
// @Router /workspaces/{workspace_name}/files/lookup [get] // @Router /workspaces/{workspace_name}/files/_op/lookup [get]
func (h *Handler) LookupFileByName() http.HandlerFunc { func (h *Handler) LookupFileByName() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
ctx, ok := context.GetRequestContext(w, r) ctx, ok := context.GetRequestContext(w, r)
@@ -382,7 +382,7 @@ func (h *Handler) DeleteFile() http.HandlerFunc {
// @Success 200 {object} LastOpenedFileResponse // @Success 200 {object} LastOpenedFileResponse
// @Failure 400 {object} ErrorResponse "Invalid file path" // @Failure 400 {object} ErrorResponse "Invalid file path"
// @Failure 500 {object} ErrorResponse "Failed to get last opened file" // @Failure 500 {object} ErrorResponse "Failed to get last opened file"
// @Router /workspaces/{workspace_name}/files/last [get] // @Router /workspaces/{workspace_name}/files/_op/last [get]
func (h *Handler) GetLastOpenedFile() http.HandlerFunc { func (h *Handler) GetLastOpenedFile() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
ctx, ok := context.GetRequestContext(w, r) ctx, ok := context.GetRequestContext(w, r)
@@ -433,7 +433,7 @@ func (h *Handler) GetLastOpenedFile() http.HandlerFunc {
// @Failure 400 {object} ErrorResponse "Invalid file path" // @Failure 400 {object} ErrorResponse "Invalid file path"
// @Failure 404 {object} ErrorResponse "File not found" // @Failure 404 {object} ErrorResponse "File not found"
// @Failure 500 {object} ErrorResponse "Failed to update file" // @Failure 500 {object} ErrorResponse "Failed to update file"
// @Router /workspaces/{workspace_name}/files/last [put] // @Router /workspaces/{workspace_name}/files/_op/last [put]
func (h *Handler) UpdateLastOpenedFile() http.HandlerFunc { func (h *Handler) UpdateLastOpenedFile() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
ctx, ok := context.GetRequestContext(w, r) ctx, ok := context.GetRequestContext(w, r)

View File

@@ -124,7 +124,7 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
require.Equal(t, http.StatusOK, rr.Code) require.Equal(t, http.StatusOK, rr.Code)
// Search for the file // Search for the file
rr = h.makeRequest(t, http.MethodGet, baseURL+"/lookup?filename="+filename, nil, h.RegularTestUser) rr = h.makeRequest(t, http.MethodGet, baseURL+"/_op/lookup?filename="+filename, nil, h.RegularTestUser)
require.Equal(t, http.StatusOK, rr.Code) require.Equal(t, http.StatusOK, rr.Code)
var response struct { var response struct {
@@ -135,7 +135,7 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
assert.Len(t, response.Paths, 2) assert.Len(t, response.Paths, 2)
// Search for non-existent file // Search for non-existent file
rr = h.makeRequest(t, http.MethodGet, baseURL+"/lookup?filename=nonexistent.md", nil, h.RegularTestUser) rr = h.makeRequest(t, http.MethodGet, baseURL+"/_op/lookup?filename=nonexistent.md", nil, h.RegularTestUser)
assert.Equal(t, http.StatusNotFound, rr.Code) assert.Equal(t, http.StatusNotFound, rr.Code)
}) })
@@ -158,7 +158,7 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
t.Run("last opened file", func(t *testing.T) { t.Run("last opened file", func(t *testing.T) {
// Initially should be empty // Initially should be empty
rr := h.makeRequest(t, http.MethodGet, baseURL+"/last", nil, h.RegularTestUser) rr := h.makeRequest(t, http.MethodGet, baseURL+"/_op/last", nil, h.RegularTestUser)
require.Equal(t, http.StatusOK, rr.Code) require.Equal(t, http.StatusOK, rr.Code)
var response struct { var response struct {
@@ -174,11 +174,11 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
}{ }{
FilePath: "docs/readme.md", FilePath: "docs/readme.md",
} }
rr = h.makeRequest(t, http.MethodPut, baseURL+"/last", updateReq, h.RegularTestUser) rr = h.makeRequest(t, http.MethodPut, baseURL+"/_op/last", updateReq, h.RegularTestUser)
require.Equal(t, http.StatusNoContent, rr.Code) require.Equal(t, http.StatusNoContent, rr.Code)
// Verify update // Verify update
rr = h.makeRequest(t, http.MethodGet, baseURL+"/last", nil, h.RegularTestUser) rr = h.makeRequest(t, http.MethodGet, baseURL+"/_op/last", nil, h.RegularTestUser)
require.Equal(t, http.StatusOK, rr.Code) require.Equal(t, http.StatusOK, rr.Code)
err = json.NewDecoder(rr.Body).Decode(&response) err = json.NewDecoder(rr.Body).Decode(&response)
@@ -187,7 +187,7 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
// Test invalid file path // Test invalid file path
updateReq.FilePath = "nonexistent.md" updateReq.FilePath = "nonexistent.md"
rr = h.makeRequest(t, http.MethodPut, baseURL+"/last", updateReq, h.RegularTestUser) rr = h.makeRequest(t, http.MethodPut, baseURL+"/_op/last", updateReq, h.RegularTestUser)
assert.Equal(t, http.StatusNotFound, rr.Code) assert.Equal(t, http.StatusNotFound, rr.Code)
}) })
@@ -202,8 +202,8 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
{"get file", http.MethodGet, baseURL + "/test.md", nil}, {"get file", http.MethodGet, baseURL + "/test.md", nil},
{"save file", http.MethodPost, baseURL + "/test.md", "content"}, {"save file", http.MethodPost, baseURL + "/test.md", "content"},
{"delete file", http.MethodDelete, baseURL + "/test.md", nil}, {"delete file", http.MethodDelete, baseURL + "/test.md", nil},
{"get last file", http.MethodGet, baseURL + "/last", nil}, {"get last file", http.MethodGet, baseURL + "/_op/last", nil},
{"update last file", http.MethodPut, baseURL + "/last", struct{ FilePath string }{"test.md"}}, {"update last file", http.MethodPut, baseURL + "/_op/last", struct{ FilePath string }{"test.md"}},
} }
for _, tc := range tests { for _, tc := range tests {

View File

@@ -407,7 +407,7 @@ func (h *Handler) DeleteWorkspace() http.HandlerFunc {
// @Produce json // @Produce json
// @Success 200 {object} LastWorkspaceNameResponse // @Success 200 {object} LastWorkspaceNameResponse
// @Failure 500 {object} ErrorResponse "Failed to get last workspace" // @Failure 500 {object} ErrorResponse "Failed to get last workspace"
// @Router /workspaces/last [get] // @Router /workspaces/_op/last [get]
func (h *Handler) GetLastWorkspaceName() http.HandlerFunc { func (h *Handler) GetLastWorkspaceName() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
ctx, ok := context.GetRequestContext(w, r) ctx, ok := context.GetRequestContext(w, r)
@@ -444,7 +444,7 @@ func (h *Handler) GetLastWorkspaceName() http.HandlerFunc {
// @Success 204 "No Content - Last workspace updated successfully" // @Success 204 "No Content - Last workspace updated successfully"
// @Failure 400 {object} ErrorResponse "Invalid request body" // @Failure 400 {object} ErrorResponse "Invalid request body"
// @Failure 500 {object} ErrorResponse "Failed to update last workspace" // @Failure 500 {object} ErrorResponse "Failed to update last workspace"
// @Router /workspaces/last [put] // @Router /workspaces/_op/last [put]
func (h *Handler) UpdateLastWorkspaceName() http.HandlerFunc { func (h *Handler) UpdateLastWorkspaceName() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
ctx, ok := context.GetRequestContext(w, r) ctx, ok := context.GetRequestContext(w, r)

View File

@@ -211,7 +211,7 @@ func testWorkspaceHandlers(t *testing.T, dbConfig DatabaseConfig) {
t.Run("last workspace", func(t *testing.T) { t.Run("last workspace", func(t *testing.T) {
t.Run("get last workspace", func(t *testing.T) { t.Run("get last workspace", func(t *testing.T) {
rr := h.makeRequest(t, http.MethodGet, "/api/v1/workspaces/last", nil, h.RegularTestUser) rr := h.makeRequest(t, http.MethodGet, "/api/v1/workspaces/_op/last", nil, h.RegularTestUser)
require.Equal(t, http.StatusOK, rr.Code) require.Equal(t, http.StatusOK, rr.Code)
var response struct { var response struct {
@@ -229,11 +229,11 @@ func testWorkspaceHandlers(t *testing.T, dbConfig DatabaseConfig) {
WorkspaceName: workspace.Name, WorkspaceName: workspace.Name,
} }
rr := h.makeRequest(t, http.MethodPut, "/api/v1/workspaces/last", req, h.RegularTestUser) rr := h.makeRequest(t, http.MethodPut, "/api/v1/workspaces/_op/last", req, h.RegularTestUser)
require.Equal(t, http.StatusNoContent, rr.Code) require.Equal(t, http.StatusNoContent, rr.Code)
// Verify the update // Verify the update
rr = h.makeRequest(t, http.MethodGet, "/api/v1/workspaces/last", nil, h.RegularTestUser) rr = h.makeRequest(t, http.MethodGet, "/api/v1/workspaces/_op/last", nil, h.RegularTestUser)
require.Equal(t, http.StatusOK, rr.Code) require.Equal(t, http.StatusOK, rr.Code)
var response struct { var response struct {