mirror of
https://github.com/aykhans/slash-e.git
synced 2025-06-14 20:07:50 +00:00
feat: migrate workspace user to api v1
This commit is contained in:
@ -64,7 +64,6 @@ func NewServer(profile *profile.Profile, store *store.Store) (*Server, error) {
|
||||
apiGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return JWTMiddleware(s, next, string(secret))
|
||||
})
|
||||
s.registerWorkspaceUserRoutes(apiGroup)
|
||||
s.registerShortcutRoutes(apiGroup)
|
||||
|
||||
// Register API v1 routes.
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/boojack/shortify/api"
|
||||
"github.com/boojack/shortify/store"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
@ -66,7 +67,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find shortcut").SetInternal(err)
|
||||
}
|
||||
|
||||
workspaceUser, err := s.Store.FindWordspaceUser(ctx, &api.WorkspaceUserFind{
|
||||
workspaceUser, err := s.Store.GetWorkspaceUser(ctx, &store.FindWorkspaceUser{
|
||||
UserID: &userID,
|
||||
WorkspaceID: &shortcut.WorkspaceID,
|
||||
})
|
||||
@ -74,7 +75,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
if shortcut.CreatorID != userID && workspaceUser.Role != api.RoleAdmin {
|
||||
if shortcut.CreatorID != userID && workspaceUser.Role != store.RoleAdmin {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "Forbidden to patch shortcut")
|
||||
}
|
||||
|
||||
|
@ -1,155 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/boojack/shortify/api"
|
||||
"github.com/boojack/shortify/internal/errorutil"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func (s *Server) registerWorkspaceUserRoutes(g *echo.Group) {
|
||||
g.POST("/workspace/:id/user", func(c echo.Context) error {
|
||||
ctx := c.Request().Context()
|
||||
userID, ok := c.Get(getUserIDContextKey()).(int)
|
||||
if !ok {
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "Missing auth session")
|
||||
}
|
||||
|
||||
workspaceID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted workspace id").SetInternal(err)
|
||||
}
|
||||
|
||||
currentWorkspaceUser, err := s.Store.FindWordspaceUser(ctx, &api.WorkspaceUserFind{
|
||||
WorkspaceID: &workspaceID,
|
||||
UserID: &userID,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find workspace user").SetInternal(err)
|
||||
}
|
||||
if currentWorkspaceUser.Role != api.RoleAdmin {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "Access forbidden to add workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
workspaceUserUpsert := &api.WorkspaceUserUpsert{
|
||||
WorkspaceID: workspaceID,
|
||||
}
|
||||
if err := json.NewDecoder(c.Request().Body).Decode(workspaceUserUpsert); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post workspace user request").SetInternal(err)
|
||||
}
|
||||
|
||||
workspaceUser, err := s.Store.UpsertWorkspaceUser(ctx, workspaceUserUpsert)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert workspace user").SetInternal(err)
|
||||
}
|
||||
if err := s.Store.ComposeWorkspaceUser(ctx, workspaceUser); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, composeResponse(workspaceUser))
|
||||
})
|
||||
|
||||
g.GET("/workspace/:id/user", func(c echo.Context) error {
|
||||
ctx := c.Request().Context()
|
||||
workspaceID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted workspace id").SetInternal(err)
|
||||
}
|
||||
|
||||
workspaceUserList, err := s.Store.FindWordspaceUserList(ctx, &api.WorkspaceUserFind{
|
||||
WorkspaceID: &workspaceID,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find workspace user list").SetInternal(err)
|
||||
}
|
||||
|
||||
for _, workspaceUser := range workspaceUserList {
|
||||
if err := s.Store.ComposeWorkspaceUser(ctx, workspaceUser); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose workspace user").SetInternal(err)
|
||||
}
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, composeResponse(workspaceUserList))
|
||||
})
|
||||
|
||||
g.GET("/workspace/:workspaceId/user/:userId", func(c echo.Context) error {
|
||||
ctx := c.Request().Context()
|
||||
workspaceID, err := strconv.Atoi(c.Param("workspaceId"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted workspace id").SetInternal(err)
|
||||
}
|
||||
userID, err := strconv.Atoi(c.Param("userId"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted user id").SetInternal(err)
|
||||
}
|
||||
|
||||
workspaceUser, err := s.Store.FindWordspaceUser(ctx, &api.WorkspaceUserFind{
|
||||
WorkspaceID: &workspaceID,
|
||||
UserID: &userID,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
if err := s.Store.ComposeWorkspaceUser(ctx, workspaceUser); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, composeResponse(workspaceUser))
|
||||
})
|
||||
|
||||
g.DELETE("/workspace/:workspaceId/user/:userId", func(c echo.Context) error {
|
||||
ctx := c.Request().Context()
|
||||
currentUserID, ok := c.Get(getUserIDContextKey()).(int)
|
||||
if !ok {
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "Missing auth session")
|
||||
}
|
||||
|
||||
workspaceID, err := strconv.Atoi(c.Param("workspaceId"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted workspace id").SetInternal(err)
|
||||
}
|
||||
|
||||
userID, err := strconv.Atoi(c.Param("userId"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted user id").SetInternal(err)
|
||||
}
|
||||
|
||||
currentWorkspaceUser, err := s.Store.FindWordspaceUser(ctx, &api.WorkspaceUserFind{
|
||||
WorkspaceID: &workspaceID,
|
||||
UserID: ¤tUserID,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find workspace user").SetInternal(err)
|
||||
}
|
||||
if currentUserID != userID && currentWorkspaceUser.Role != api.RoleAdmin {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "Access forbidden to delete workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
workspaceUserDelete := &api.WorkspaceUserDelete{
|
||||
WorkspaceID: workspaceID,
|
||||
UserID: userID,
|
||||
}
|
||||
if err := s.Store.DeleteWorkspaceUser(ctx, workspaceUserDelete); err != nil {
|
||||
if errorutil.ErrorCode(err) == errorutil.NotFound {
|
||||
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Workspace user not found with workspace id %d and user id %d", workspaceID, userID))
|
||||
}
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
shortcutDelete := &api.ShortcutDelete{
|
||||
CreatorID: &userID,
|
||||
WorkspaceID: &workspaceID,
|
||||
}
|
||||
if err := s.Store.DeleteShortcut(ctx, shortcutDelete); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete shortcut").SetInternal(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, true)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user