-
Notifications
You must be signed in to change notification settings - Fork 57
feat(favorites): server-persist cycle/module favorites with folders and ordering #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
martian56
merged 2 commits into
Devlaner:main
from
cavidelizade:feat/favorites-cycles-modules
Jul 13, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/Devlaner/devlane/api/internal/middleware" | ||
| "github.com/Devlaner/devlane/api/internal/service" | ||
| "github.com/gin-gonic/gin" | ||
| "github.com/google/uuid" | ||
| ) | ||
|
|
||
| // ListFavorites returns the user's full favorites tree (entities + folders) for | ||
| // a workspace. | ||
| // GET /api/workspaces/:slug/favorites/ | ||
| func (h *FavoriteHandler) ListFavorites(c *gin.Context) { | ||
| user := middleware.GetUser(c) | ||
| if user == nil { | ||
| c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication required"}) | ||
| return | ||
| } | ||
| if h.Fav == nil { | ||
| c.JSON(http.StatusOK, []any{}) | ||
| return | ||
| } | ||
| list, err := h.Fav.List(c.Request.Context(), c.Param("slug"), user.ID) | ||
| if err != nil { | ||
| h.favError(c, err) | ||
| return | ||
| } | ||
| c.JSON(http.StatusOK, list) | ||
| } | ||
|
|
||
| // CreateFavorite favorites a cycle/module, or creates a folder when is_folder is | ||
| // true. | ||
| // POST /api/workspaces/:slug/favorites/ | ||
| func (h *FavoriteHandler) CreateFavorite(c *gin.Context) { | ||
| user := middleware.GetUser(c) | ||
| if user == nil { | ||
| c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication required"}) | ||
| return | ||
| } | ||
| if h.Fav == nil { | ||
| c.JSON(http.StatusServiceUnavailable, gin.H{"error": "Favorites not available"}) | ||
| return | ||
| } | ||
| var body struct { | ||
| IsFolder bool `json:"is_folder"` | ||
| Name string `json:"name"` | ||
| EntityType string `json:"entity_type"` | ||
| EntityID string `json:"entity_id"` | ||
| ProjectID string `json:"project_id"` | ||
| } | ||
| if err := c.ShouldBindJSON(&body); err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request", "detail": err.Error()}) | ||
| return | ||
| } | ||
| ctx := c.Request.Context() | ||
| slug := c.Param("slug") | ||
| if body.IsFolder { | ||
| fav, err := h.Fav.CreateFolder(ctx, slug, user.ID, body.Name) | ||
| if err != nil { | ||
| h.favError(c, err) | ||
| return | ||
| } | ||
| c.JSON(http.StatusCreated, fav) | ||
| return | ||
| } | ||
| entityID, err := uuid.Parse(body.EntityID) | ||
| if err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid entity_id"}) | ||
| return | ||
| } | ||
| projectID, err := uuid.Parse(body.ProjectID) | ||
| if err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project_id"}) | ||
| return | ||
| } | ||
| fav, err := h.Fav.AddEntity(ctx, slug, user.ID, body.EntityType, entityID, projectID, body.Name) | ||
| if err != nil { | ||
| h.favError(c, err) | ||
| return | ||
| } | ||
| c.JSON(http.StatusCreated, fav) | ||
| } | ||
|
|
||
| // UpdateFavorite renames, moves, and/or reorders a favorite. | ||
| // PATCH /api/workspaces/:slug/favorites/:favId/ | ||
| func (h *FavoriteHandler) UpdateFavorite(c *gin.Context) { | ||
| user := middleware.GetUser(c) | ||
| if user == nil { | ||
| c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication required"}) | ||
| return | ||
| } | ||
| if h.Fav == nil { | ||
| c.JSON(http.StatusServiceUnavailable, gin.H{"error": "Favorites not available"}) | ||
| return | ||
| } | ||
| id, err := uuid.Parse(c.Param("favId")) | ||
| if err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid favorite ID"}) | ||
| return | ||
| } | ||
| var body struct { | ||
| Name *string `json:"name"` | ||
| ParentID *string `json:"parent_id"` | ||
| SortOrder *float64 `json:"sort_order"` | ||
| } | ||
| if err := c.ShouldBindJSON(&body); err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request", "detail": err.Error()}) | ||
| return | ||
| } | ||
| // parent_id present (even as null/"") means "move"; a non-empty string must parse. | ||
| var parentID *uuid.UUID | ||
| parentSet := body.ParentID != nil | ||
| if parentSet && *body.ParentID != "" { | ||
| pid, perr := uuid.Parse(*body.ParentID) | ||
| if perr != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid parent_id"}) | ||
| return | ||
| } | ||
| parentID = &pid | ||
| } | ||
| fav, err := h.Fav.Update(c.Request.Context(), c.Param("slug"), user.ID, id, body.Name, parentSet, parentID, body.SortOrder) | ||
| if err != nil { | ||
| h.favError(c, err) | ||
| return | ||
| } | ||
| c.JSON(http.StatusOK, fav) | ||
| } | ||
|
|
||
| // DeleteFavorite removes a favorite or folder. | ||
| // DELETE /api/workspaces/:slug/favorites/:favId/ | ||
| func (h *FavoriteHandler) DeleteFavorite(c *gin.Context) { | ||
| user := middleware.GetUser(c) | ||
| if user == nil { | ||
| c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication required"}) | ||
| return | ||
| } | ||
| if h.Fav == nil { | ||
| c.JSON(http.StatusServiceUnavailable, gin.H{"error": "Favorites not available"}) | ||
| return | ||
| } | ||
| id, err := uuid.Parse(c.Param("favId")) | ||
| if err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid favorite ID"}) | ||
| return | ||
| } | ||
| if err := h.Fav.Delete(c.Request.Context(), c.Param("slug"), user.ID, id); err != nil { | ||
| h.favError(c, err) | ||
| return | ||
| } | ||
| c.Status(http.StatusNoContent) | ||
| } | ||
|
|
||
| func (h *FavoriteHandler) favError(c *gin.Context, err error) { | ||
| switch err { | ||
| case service.ErrFavoriteWorkspace, service.ErrFavoriteForbidden, service.ErrFavoriteNotFound: | ||
| c.JSON(http.StatusNotFound, gin.H{"error": "Not found"}) | ||
| case service.ErrFavoriteBadEntity, service.ErrFavoriteBadParent, service.ErrFavoriteBadName: | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
| default: | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "Favorites request failed"}) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package handler_test | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/Devlaner/devlane/api/internal/testutil" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type favJSON struct { | ||
| ID string `json:"id"` | ||
| Name string `json:"name"` | ||
| IsFolder bool `json:"is_folder"` | ||
| ParentID *string `json:"parent_id"` | ||
| SortOrder float64 `json:"sort_order"` | ||
| EntityID string `json:"entity_identifier"` | ||
| Type string `json:"entity_type"` | ||
| } | ||
|
|
||
| func favList(t *testing.T, body string) []favJSON { | ||
| t.Helper() | ||
| var out []favJSON | ||
| require.NoError(t, json.Unmarshal([]byte(body), &out)) | ||
| return out | ||
| } | ||
|
|
||
| // The favorites tree: favorite a cycle, put it in a folder, reorder it, and | ||
| // deleting the folder keeps the favorite (moved to the top level). Covers #205. | ||
| func TestFavorites_CycleFolderOrdering(t *testing.T) { | ||
| ts := testutil.NewTestServer(t) | ||
| w := testutil.SeedWorld(t, ts.DB) | ||
| base := "/api/workspaces/" + w.Workspace.Slug + "/favorites/" | ||
| cycle := testutil.CreateCycle(t, ts.DB, w.Project.ID, w.Workspace.ID, w.User.ID) | ||
|
|
||
| // Favorite the cycle. | ||
| rr := ts.POST(base, map[string]any{ | ||
| "entity_type": "cycle", | ||
| "entity_id": cycle.ID.String(), | ||
| "project_id": w.Project.ID.String(), | ||
| "name": "Sprint 1", | ||
| }, w.Session) | ||
| require.Equal(t, http.StatusCreated, rr.Code, "body=%s", rr.Body.String()) | ||
| var favResp favJSON | ||
| require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &favResp)) | ||
| require.Equal(t, cycle.ID.String(), favResp.EntityID) | ||
|
|
||
| // Create a folder. | ||
| rr = ts.POST(base, map[string]any{"is_folder": true, "name": "Sprints"}, w.Session) | ||
| require.Equal(t, http.StatusCreated, rr.Code, "body=%s", rr.Body.String()) | ||
| var folder favJSON | ||
| require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &folder)) | ||
| require.True(t, folder.IsFolder) | ||
|
|
||
| // Move the cycle favorite into the folder and reorder it. | ||
| rr = ts.PATCH(base+favResp.ID+"/", map[string]any{"parent_id": folder.ID, "sort_order": 10}, w.Session) | ||
| require.Equal(t, http.StatusOK, rr.Code, "body=%s", rr.Body.String()) | ||
|
|
||
| list := favList(t, ts.GET(base, w.Session).Body.String()) | ||
| require.Len(t, list, 2) | ||
| for _, f := range list { | ||
| if f.ID == favResp.ID { | ||
| require.NotNil(t, f.ParentID) | ||
| require.Equal(t, folder.ID, *f.ParentID) | ||
| require.EqualValues(t, 10, f.SortOrder) | ||
| } | ||
| } | ||
|
|
||
| // Deleting the folder keeps the cycle favorite (parent cleared). | ||
| require.Equal(t, http.StatusNoContent, ts.DELETE(base+folder.ID+"/", w.Session).Code) | ||
| list = favList(t, ts.GET(base, w.Session).Body.String()) | ||
| require.Len(t, list, 1) | ||
| require.Equal(t, favResp.ID, list[0].ID) | ||
| require.Nil(t, list[0].ParentID) | ||
|
|
||
| // Unfavorite by deleting the favorite row. | ||
| require.Equal(t, http.StatusNoContent, ts.DELETE(base+favResp.ID+"/", w.Session).Code) | ||
| require.Len(t, favList(t, ts.GET(base, w.Session).Body.String()), 0) | ||
| } | ||
|
|
||
| // An empty name is rejected for both folders and entity favorites. | ||
| func TestFavorites_EmptyNameRejected(t *testing.T) { | ||
| ts := testutil.NewTestServer(t) | ||
| w := testutil.SeedWorld(t, ts.DB) | ||
| base := "/api/workspaces/" + w.Workspace.Slug + "/favorites/" | ||
|
|
||
| rr := ts.POST(base, map[string]any{"is_folder": true, "name": " "}, w.Session) | ||
| require.Equal(t, http.StatusBadRequest, rr.Code, "body=%s", rr.Body.String()) | ||
| } | ||
|
|
||
| // A non-member can't read a workspace's favorites. | ||
| func TestFavorites_NonMemberForbidden(t *testing.T) { | ||
| ts := testutil.NewTestServer(t) | ||
| w := testutil.SeedWorld(t, ts.DB) | ||
| stranger := testutil.CreateUser(t, ts.DB) | ||
| strangerSession := testutil.LoginAs(t, ts.DB, stranger) | ||
| rr := ts.GET("/api/workspaces/"+w.Workspace.Slug+"/favorites/", strangerSession) | ||
| require.Equal(t, http.StatusNotFound, rr.Code) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.