feat: update delete module api

This commit is contained in:
Steven 2022-09-27 00:21:05 +08:00
parent a642465f86
commit d2e08de9bd
9 changed files with 46 additions and 13 deletions

View File

@ -83,5 +83,9 @@ type ShortcutFind struct {
}
type ShortcutDelete struct {
ID int
ID *int
// Standard fields
CreatorID *int
WorkspaceID *int
}

View File

@ -60,8 +60,6 @@ func aclMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
return next(c)
}
println("path", path)
if common.HasPrefixes(path, "/api/ping", "/api/status", "/api/user/:id") && c.Request().Method == http.MethodGet {
return next(c)
}

View File

@ -156,7 +156,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
}
shortcutDelete := &api.ShortcutDelete{
ID: shortcutID,
ID: &shortcutID,
}
if err := s.Store.DeleteShortcut(ctx, shortcutDelete); err != nil {
if common.ErrorCode(err) == common.NotFound {

View File

@ -134,8 +134,8 @@ func (s *Server) registerWorkspaceUserRoutes(g *echo.Group) {
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)
if currentWorkspaceUser.UserID != userID && currentWorkspaceUser.Role != api.RoleAdmin {
return echo.NewHTTPError(http.StatusForbidden, "Access forbidden to delete workspace user").SetInternal(err)
}
userID, err = strconv.Atoi(c.Param("userId"))
@ -154,6 +154,17 @@ func (s *Server) registerWorkspaceUserRoutes(g *echo.Group) {
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 {
if common.ErrorCode(err) == common.NotFound {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Shortcut not found with workspace id %d and user id %d", workspaceID, userID))
}
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete shortcut").SetInternal(err)
}
return c.JSON(http.StatusOK, true)
})
}

View File

@ -11,6 +11,19 @@ VALUES
'ADMIN'
);
INSERT INTO
workspace_user (
`workspace_id`,
`user_id`,
`role`
)
VALUES
(
11,
102,
'USER'
);
INSERT INTO
workspace_user (
`workspace_id`,

View File

@ -182,7 +182,7 @@ func (s *Store) DeleteShortcut(ctx context.Context, delete *api.ShortcutDelete)
return FormatError(err)
}
s.cache.DeleteCache(api.ShortcutCache, delete.ID)
s.cache.DeleteCache(api.ShortcutCache, *delete.ID)
return nil
}
@ -346,10 +346,20 @@ func findShortcutList(ctx context.Context, tx *sql.Tx, find *api.ShortcutFind) (
}
func deleteShortcut(ctx context.Context, tx *sql.Tx, delete *api.ShortcutDelete) error {
where, args := []string{"1 = 1"}, []interface{}{}
if v := delete.ID; v != nil {
where, args = append(where, "id = ?"), append(args, *v)
}
if v := delete.CreatorID; v != nil {
where, args = append(where, "creator_id = ?"), append(args, *v)
}
if v := delete.WorkspaceID; v != nil {
where, args = append(where, "workspace_id = ?"), append(args, *v)
}
result, err := tx.ExecContext(ctx, `
PRAGMA foreign_keys = ON;
DELETE FROM shortcut WHERE id = ?
`, delete.ID)
DELETE FROM shortcut WHERE `+strings.Join(where, " AND "), args...)
if err != nil {
return FormatError(err)
}

View File

@ -326,7 +326,6 @@ func findUserList(ctx context.Context, tx *sql.Tx, find *api.UserFind) ([]*userR
func deleteUser(ctx context.Context, tx *sql.Tx, delete *api.UserDelete) error {
result, err := tx.ExecContext(ctx, `
PRAGMA foreign_keys = ON;
DELETE FROM user WHERE id = ?
`, delete.ID)
if err != nil {

View File

@ -311,7 +311,6 @@ func findWorkspaceList(ctx context.Context, tx *sql.Tx, find *api.WorkspaceFind)
func deleteWorkspace(ctx context.Context, tx *sql.Tx, delete *api.WorkspaceDelete) error {
result, err := tx.ExecContext(ctx, `
PRAGMA foreign_keys = ON;
DELETE FROM workspace WHERE id = ?
`, delete.ID)
if err != nil {

View File

@ -212,7 +212,6 @@ func findWorkspaceUserList(ctx context.Context, tx *sql.Tx, find *api.WorkspaceU
func deleteWorkspaceUser(ctx context.Context, tx *sql.Tx, delete *api.WorkspaceUserDelete) error {
result, err := tx.ExecContext(ctx, `
PRAGMA foreign_keys = ON;
DELETE FROM workspace_user WHERE workspace_id = ? AND user_id = ?
`, delete.WorkspaceID, delete.UserID)
if err != nil {