mirror of
https://github.com/aykhans/slash-e.git
synced 2025-07-03 20:21:40 +00:00
chore: remove workspace and workspace tables
This commit is contained in:
@ -39,12 +39,11 @@ type Shortcut struct {
|
||||
ID int `json:"id"`
|
||||
|
||||
// Standard fields
|
||||
CreatorID int `json:"creatorId"`
|
||||
Creator *User `json:"creator"`
|
||||
CreatedTs int64 `json:"createdTs"`
|
||||
UpdatedTs int64 `json:"updatedTs"`
|
||||
WorkspaceID int `json:"workspaceId"`
|
||||
RowStatus RowStatus `json:"rowStatus"`
|
||||
CreatorID int `json:"creatorId"`
|
||||
Creator *User `json:"creator"`
|
||||
CreatedTs int64 `json:"createdTs"`
|
||||
UpdatedTs int64 `json:"updatedTs"`
|
||||
RowStatus RowStatus `json:"rowStatus"`
|
||||
|
||||
// Domain specific fields
|
||||
Name string `json:"name"`
|
||||
@ -54,7 +53,6 @@ type Shortcut struct {
|
||||
}
|
||||
|
||||
type CreateShortcutRequest struct {
|
||||
WorkspaceID int `json:"workspaceId"`
|
||||
Name string `json:"name"`
|
||||
Link string `json:"link"`
|
||||
Description string `json:"description"`
|
||||
@ -83,7 +81,6 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
|
||||
|
||||
shortcut, err := s.Store.CreateShortcut(ctx, &store.Shortcut{
|
||||
CreatorID: userID,
|
||||
WorkspaceID: create.WorkspaceID,
|
||||
Name: create.Name,
|
||||
Link: create.Link,
|
||||
Description: create.Description,
|
||||
@ -113,17 +110,11 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find shortcut").SetInternal(err)
|
||||
}
|
||||
|
||||
workspaceUser, err := s.Store.GetWorkspaceUser(ctx, &store.FindWorkspaceUser{
|
||||
UserID: &userID,
|
||||
WorkspaceID: &shortcut.WorkspaceID,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find workspace user").SetInternal(err)
|
||||
if shortcut == nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "Shortcut not found")
|
||||
}
|
||||
|
||||
if shortcut.CreatorID != userID && workspaceUser.Role != store.RoleAdmin {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "Forbidden to patch shortcut")
|
||||
if shortcut.CreatorID != userID {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "Shortcut does not belong to user")
|
||||
}
|
||||
|
||||
patch := &PatchShortcutRequest{}
|
||||
@ -153,9 +144,6 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
|
||||
}
|
||||
|
||||
find := &store.FindShortcut{}
|
||||
if workspaceID, err := strconv.Atoi(c.QueryParam("workspaceId")); err == nil {
|
||||
find.WorkspaceID = &workspaceID
|
||||
}
|
||||
if name := c.QueryParam("name"); name != "" {
|
||||
find.Name = &name
|
||||
}
|
||||
|
@ -23,7 +23,5 @@ func (s *APIV1Service) Start(apiV1Group *echo.Group, secret string) {
|
||||
s.registerSystemRoutes(apiV1Group)
|
||||
s.registerAuthRoutes(apiV1Group, secret)
|
||||
s.registerUserRoutes(apiV1Group)
|
||||
s.registerWorkspaceRoutes(apiV1Group)
|
||||
s.registerWorkspaceUserRoutes(apiV1Group)
|
||||
s.registerShortcutRoutes(apiV1Group)
|
||||
}
|
||||
|
@ -1,189 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/boojack/shortify/store"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type Workspace struct {
|
||||
ID int `json:"id"`
|
||||
|
||||
// Standard fields
|
||||
CreatorID int `json:"creatorId"`
|
||||
CreatedTs int64 `json:"createdTs"`
|
||||
UpdatedTs int64 `json:"updatedTs"`
|
||||
RowStatus RowStatus `json:"rowStatus"`
|
||||
|
||||
// Domain specific fields
|
||||
Name string `json:"name"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type CreateWorkspaceRequest struct {
|
||||
Name string `json:"name"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type PatchWorkspaceRequest struct {
|
||||
RowStatus *RowStatus `json:"rowStatus"`
|
||||
Name *string `json:"name"`
|
||||
Title *string `json:"title"`
|
||||
Description *string `json:"description"`
|
||||
}
|
||||
|
||||
type WorkspaceFind struct {
|
||||
ID *int `json:"id"`
|
||||
|
||||
// Standard fields
|
||||
RowStatus *RowStatus `json:"rowStatus"`
|
||||
|
||||
// Domain specific fields
|
||||
Name *string `json:"name"`
|
||||
|
||||
// Related fields
|
||||
MemberID *int
|
||||
}
|
||||
|
||||
type WorkspaceDelete struct {
|
||||
ID int
|
||||
}
|
||||
|
||||
func (s *APIV1Service) registerWorkspaceRoutes(g *echo.Group) {
|
||||
g.POST("/workspace", 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")
|
||||
}
|
||||
|
||||
create := &CreateWorkspaceRequest{}
|
||||
if err := json.NewDecoder(c.Request().Body).Decode(create); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post workspace request").SetInternal(err)
|
||||
}
|
||||
if len(create.Name) > 24 {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Workspace name length should be less than 24")
|
||||
}
|
||||
|
||||
workspace, err := s.Store.CreateWorkspace(ctx, &store.Workspace{
|
||||
ResourceID: create.Name,
|
||||
Title: create.Title,
|
||||
Description: create.Description,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create workspace").SetInternal(err)
|
||||
}
|
||||
|
||||
_, err = s.Store.UpsertWorkspaceUser(ctx, &store.WorkspaceUser{
|
||||
WorkspaceID: workspace.ID,
|
||||
UserID: userID,
|
||||
Role: store.RoleAdmin,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, workspace)
|
||||
})
|
||||
|
||||
g.GET("/workspace/:id", func(c echo.Context) error {
|
||||
ctx := c.Request().Context()
|
||||
id, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted workspace id").SetInternal(err)
|
||||
}
|
||||
|
||||
workspace, err := s.Store.GetWorkspace(ctx, &store.FindWorkspace{
|
||||
ID: &id,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find workspace").SetInternal(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, workspace)
|
||||
})
|
||||
|
||||
g.PATCH("/workspace/:id", func(c echo.Context) error {
|
||||
ctx := c.Request().Context()
|
||||
userID, ok := c.Get(getUserIDContextKey()).(int)
|
||||
if !ok {
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
||||
}
|
||||
|
||||
workspaceID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err)
|
||||
}
|
||||
workspaceUser, err := s.Store.GetWorkspaceUser(ctx, &store.FindWorkspaceUser{
|
||||
WorkspaceID: &workspaceID,
|
||||
UserID: &userID,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find workspace user").SetInternal(err)
|
||||
}
|
||||
if workspaceUser == nil {
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "Current user is not a member of the workspace")
|
||||
}
|
||||
if workspaceUser.Role != store.RoleAdmin {
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "Current user is not an admin of the workspace")
|
||||
}
|
||||
|
||||
patch := &PatchWorkspaceRequest{}
|
||||
if err := json.NewDecoder(c.Request().Body).Decode(patch); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch workspace request").SetInternal(err)
|
||||
}
|
||||
|
||||
workspace, err := s.Store.UpdateWorkspace(ctx, &store.UpdateWorkspace{
|
||||
ID: workspaceID,
|
||||
RowStatus: (*store.RowStatus)(patch.RowStatus),
|
||||
ResourceID: patch.Name,
|
||||
Title: patch.Title,
|
||||
Description: patch.Description,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch workspace").SetInternal(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, workspace)
|
||||
})
|
||||
|
||||
g.DELETE("/workspace/:id", func(c echo.Context) error {
|
||||
ctx := c.Request().Context()
|
||||
userID, ok := c.Get(getUserIDContextKey()).(int)
|
||||
if !ok {
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
||||
}
|
||||
workspaceID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err)
|
||||
}
|
||||
workspaceUser, err := s.Store.GetWorkspaceUser(ctx, &store.FindWorkspaceUser{
|
||||
WorkspaceID: &workspaceID,
|
||||
UserID: &userID,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find workspace user").SetInternal(err)
|
||||
}
|
||||
if workspaceUser == nil {
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "not workspace user")
|
||||
}
|
||||
if workspaceUser.Role != store.RoleAdmin {
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "not workspace admin")
|
||||
}
|
||||
|
||||
if err := s.Store.DeleteWorkspace(ctx, &store.DeleteWorkspace{
|
||||
ID: workspaceID,
|
||||
}); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete user").SetInternal(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, true)
|
||||
})
|
||||
}
|
@ -1,213 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/boojack/shortify/store"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type WorkspaceUser struct {
|
||||
WorkspaceID int `json:"workspaceId"`
|
||||
UserID int `json:"userId"`
|
||||
Role Role `json:"role"`
|
||||
|
||||
// Related fields
|
||||
Username string `json:"username"`
|
||||
Nickname string `json:"nickname"`
|
||||
}
|
||||
|
||||
type UpsertWorkspaceUserRequest struct {
|
||||
WorkspaceID int `json:"workspaceId"`
|
||||
UserID int `json:"userId"`
|
||||
Role Role `json:"role"`
|
||||
}
|
||||
|
||||
func (s *APIV1Service) 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.GetWorkspaceUser(ctx, &store.FindWorkspaceUser{
|
||||
WorkspaceID: &workspaceID,
|
||||
UserID: &userID,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find workspace user").SetInternal(err)
|
||||
}
|
||||
if currentWorkspaceUser.Role != store.RoleAdmin {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "Access forbidden to add workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
upsert := &UpsertWorkspaceUserRequest{
|
||||
WorkspaceID: workspaceID,
|
||||
}
|
||||
if err := json.NewDecoder(c.Request().Body).Decode(upsert); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post workspace user request").SetInternal(err)
|
||||
}
|
||||
|
||||
workspaceUser, err := s.Store.UpsertWorkspaceUser(ctx, &store.WorkspaceUser{
|
||||
WorkspaceID: upsert.WorkspaceID,
|
||||
UserID: upsert.UserID,
|
||||
Role: convertRoleToStore(upsert.Role),
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
composedWorkspaceUser, err := s.composeWorkspaceUser(ctx, workspaceUser)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, composedWorkspaceUser)
|
||||
})
|
||||
|
||||
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.ListWorkspaceUsers(ctx, &store.FindWorkspaceUser{
|
||||
WorkspaceID: &workspaceID,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find workspace user list").SetInternal(err)
|
||||
}
|
||||
|
||||
composedList := make([]*WorkspaceUser, 0, len(workspaceUserList))
|
||||
for _, workspaceUser := range workspaceUserList {
|
||||
composedWorkspaceUser, err := s.composeWorkspaceUser(ctx, workspaceUser)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose workspace user").SetInternal(err)
|
||||
}
|
||||
composedList = append(composedList, composedWorkspaceUser)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, composedList)
|
||||
})
|
||||
|
||||
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.GetWorkspaceUser(ctx, &store.FindWorkspaceUser{
|
||||
WorkspaceID: &workspaceID,
|
||||
UserID: &userID,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
composedWorkspaceUser, err := s.composeWorkspaceUser(ctx, workspaceUser)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, composedWorkspaceUser)
|
||||
})
|
||||
|
||||
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.GetWorkspaceUser(ctx, &store.FindWorkspaceUser{
|
||||
WorkspaceID: &workspaceID,
|
||||
UserID: ¤tUserID,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find workspace user").SetInternal(err)
|
||||
}
|
||||
if currentUserID != userID && currentWorkspaceUser.Role != store.RoleAdmin {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "Access forbidden to delete workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
if err := s.Store.DeleteWorkspaceUser(ctx, &store.DeleteWorkspaceUser{
|
||||
WorkspaceID: workspaceID,
|
||||
UserID: userID,
|
||||
}); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete workspace user").SetInternal(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, true)
|
||||
})
|
||||
}
|
||||
|
||||
func convertRoleToStore(role Role) store.Role {
|
||||
switch role {
|
||||
case RoleAdmin:
|
||||
return store.RoleAdmin
|
||||
case RoleUser:
|
||||
return store.RoleUser
|
||||
default:
|
||||
return store.RoleUser
|
||||
}
|
||||
}
|
||||
|
||||
func convertRoleFromStore(role store.Role) Role {
|
||||
switch role {
|
||||
case store.RoleAdmin:
|
||||
return RoleAdmin
|
||||
case store.RoleUser:
|
||||
return RoleUser
|
||||
default:
|
||||
return RoleUser
|
||||
}
|
||||
}
|
||||
|
||||
func (s *APIV1Service) composeWorkspaceUser(ctx context.Context, workspaceUser *store.WorkspaceUser) (*WorkspaceUser, error) {
|
||||
user, err := s.Store.GetUser(ctx, &store.FindUser{
|
||||
ID: &workspaceUser.UserID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if user == nil {
|
||||
return nil, fmt.Errorf("Failed to find user %d", workspaceUser.UserID)
|
||||
}
|
||||
|
||||
composedWorkspaceUser := &WorkspaceUser{
|
||||
WorkspaceID: workspaceUser.WorkspaceID,
|
||||
UserID: workspaceUser.UserID,
|
||||
Role: convertRoleFromStore(workspaceUser.Role),
|
||||
Username: user.Username,
|
||||
Nickname: user.Nickname,
|
||||
}
|
||||
|
||||
return composedWorkspaceUser, nil
|
||||
}
|
Reference in New Issue
Block a user