mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 15:44:21 +00:00
Refactor API routes to include "_op" prefix for last workspace and file operations
This commit is contained in:
@@ -114,8 +114,8 @@ func setupRouter(o Options) *chi.Mux {
|
||||
r.Route("/workspaces", func(r chi.Router) {
|
||||
r.Get("/", handler.ListWorkspaces())
|
||||
r.Post("/", handler.CreateWorkspace())
|
||||
r.Get("/last", handler.GetLastWorkspaceName())
|
||||
r.Put("/last", handler.UpdateLastWorkspaceName())
|
||||
r.Get("/_op/last", handler.GetLastWorkspaceName())
|
||||
r.Put("/_op/last", handler.UpdateLastWorkspaceName())
|
||||
|
||||
// Single workspace routes
|
||||
r.Route("/{workspaceName}", func(r chi.Router) {
|
||||
@@ -129,9 +129,9 @@ func setupRouter(o Options) *chi.Mux {
|
||||
// File routes
|
||||
r.Route("/files", func(r chi.Router) {
|
||||
r.Get("/", handler.ListFiles())
|
||||
r.Get("/last", handler.GetLastOpenedFile())
|
||||
r.Put("/last", handler.UpdateLastOpenedFile())
|
||||
r.Get("/lookup", handler.LookupFileByName())
|
||||
r.Get("/_op/last", handler.GetLastOpenedFile())
|
||||
r.Put("/_op/last", handler.UpdateLastOpenedFile())
|
||||
r.Get("/_op/lookup", handler.LookupFileByName())
|
||||
|
||||
r.Post("/*", handler.SaveFile())
|
||||
r.Get("/*", handler.GetFileContent())
|
||||
|
||||
@@ -90,7 +90,7 @@ func (h *Handler) ListFiles() http.HandlerFunc {
|
||||
// @Success 200 {object} LookupResponse
|
||||
// @Failure 400 {object} ErrorResponse "Filename is required"
|
||||
// @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 {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, ok := context.GetRequestContext(w, r)
|
||||
@@ -382,7 +382,7 @@ func (h *Handler) DeleteFile() http.HandlerFunc {
|
||||
// @Success 200 {object} LastOpenedFileResponse
|
||||
// @Failure 400 {object} ErrorResponse "Invalid file path"
|
||||
// @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 {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, ok := context.GetRequestContext(w, r)
|
||||
@@ -433,7 +433,7 @@ func (h *Handler) GetLastOpenedFile() http.HandlerFunc {
|
||||
// @Failure 400 {object} ErrorResponse "Invalid file path"
|
||||
// @Failure 404 {object} ErrorResponse "File not found"
|
||||
// @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 {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, ok := context.GetRequestContext(w, r)
|
||||
|
||||
@@ -124,7 +124,7 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
|
||||
require.Equal(t, http.StatusOK, rr.Code)
|
||||
|
||||
// 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)
|
||||
|
||||
var response struct {
|
||||
@@ -135,7 +135,7 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
|
||||
assert.Len(t, response.Paths, 2)
|
||||
|
||||
// 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)
|
||||
})
|
||||
|
||||
@@ -158,7 +158,7 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
|
||||
|
||||
t.Run("last opened file", func(t *testing.T) {
|
||||
// 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)
|
||||
|
||||
var response struct {
|
||||
@@ -174,11 +174,11 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
|
||||
}{
|
||||
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)
|
||||
|
||||
// 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)
|
||||
|
||||
err = json.NewDecoder(rr.Body).Decode(&response)
|
||||
@@ -187,7 +187,7 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
|
||||
|
||||
// Test invalid file path
|
||||
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)
|
||||
})
|
||||
|
||||
@@ -202,8 +202,8 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
|
||||
{"get file", http.MethodGet, baseURL + "/test.md", nil},
|
||||
{"save file", http.MethodPost, baseURL + "/test.md", "content"},
|
||||
{"delete file", http.MethodDelete, baseURL + "/test.md", nil},
|
||||
{"get last file", http.MethodGet, baseURL + "/last", nil},
|
||||
{"update last file", http.MethodPut, baseURL + "/last", struct{ FilePath string }{"test.md"}},
|
||||
{"get last file", http.MethodGet, baseURL + "/_op/last", nil},
|
||||
{"update last file", http.MethodPut, baseURL + "/_op/last", struct{ FilePath string }{"test.md"}},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
|
||||
@@ -407,7 +407,7 @@ func (h *Handler) DeleteWorkspace() http.HandlerFunc {
|
||||
// @Produce json
|
||||
// @Success 200 {object} LastWorkspaceNameResponse
|
||||
// @Failure 500 {object} ErrorResponse "Failed to get last workspace"
|
||||
// @Router /workspaces/last [get]
|
||||
// @Router /workspaces/_op/last [get]
|
||||
func (h *Handler) GetLastWorkspaceName() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, ok := context.GetRequestContext(w, r)
|
||||
@@ -444,7 +444,7 @@ func (h *Handler) GetLastWorkspaceName() http.HandlerFunc {
|
||||
// @Success 204 "No Content - Last workspace updated successfully"
|
||||
// @Failure 400 {object} ErrorResponse "Invalid request body"
|
||||
// @Failure 500 {object} ErrorResponse "Failed to update last workspace"
|
||||
// @Router /workspaces/last [put]
|
||||
// @Router /workspaces/_op/last [put]
|
||||
func (h *Handler) UpdateLastWorkspaceName() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, ok := context.GetRequestContext(w, r)
|
||||
|
||||
@@ -211,7 +211,7 @@ func testWorkspaceHandlers(t *testing.T, dbConfig DatabaseConfig) {
|
||||
|
||||
t.Run("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)
|
||||
|
||||
var response struct {
|
||||
@@ -229,11 +229,11 @@ func testWorkspaceHandlers(t *testing.T, dbConfig DatabaseConfig) {
|
||||
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)
|
||||
|
||||
// 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)
|
||||
|
||||
var response struct {
|
||||
|
||||
Reference in New Issue
Block a user