feat: add REST endpoints for project restore and deleted listing

- POST /projects/:project/restore - restores a soft-deleted project
- GET /projects/deleted - lists all soft-deleted projects for the user
This commit is contained in:
kolaente 2026-03-26 16:14:27 +01:00
parent 9aabd37b5d
commit 6c2e2cda4f
2 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,105 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package v1
import (
"net/http"
"strconv"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/models"
auth2 "code.vikunja.io/api/pkg/modules/auth"
"github.com/labstack/echo/v5"
)
// RestoreProject restores a soft-deleted project.
// @Summary Restore a deleted project
// @Description Restores a project that was previously deleted (soft-deleted). Also restores all descendant projects that were soft-deleted at the same time.
// @tags project
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param id path int true "Project ID"
// @Success 200 {object} models.Project "The restored project."
// @Failure 400 {object} web.HTTPError "Invalid project ID."
// @Failure 403 {object} web.HTTPError "The user does not have admin access to the project."
// @Failure 404 {object} web.HTTPError "The project does not exist or is not deleted."
// @Failure 500 {object} models.Message "Internal server error."
// @Router /projects/{id}/restore [post]
func RestoreProject(c *echo.Context) error {
projectID, err := strconv.ParseInt(c.Param("project"), 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": "Invalid project ID",
})
}
auth, err := auth2.GetAuthFromClaims(c)
if err != nil {
return err
}
s := db.NewSession()
defer s.Close()
project, err := models.RestoreProject(s, projectID, auth)
if err != nil {
_ = s.Rollback()
return err
}
if err := s.Commit(); err != nil {
_ = s.Rollback()
return err
}
return c.JSON(http.StatusOK, project)
}
// ListDeletedProjects returns all soft-deleted projects the user has admin access to.
// @Summary Get all deleted projects
// @Description Returns all soft-deleted projects that the current user has admin access to, along with their deletion date and days remaining before permanent purge.
// @tags project
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Success 200 {array} models.Project "All deleted projects."
// @Failure 500 {object} models.Message "Internal server error."
// @Router /projects/deleted [get]
func ListDeletedProjects(c *echo.Context) error {
auth, err := auth2.GetAuthFromClaims(c)
if err != nil {
return err
}
s := db.NewSession()
defer s.Close()
projects, err := models.GetDeletedProjects(s, auth)
if err != nil {
_ = s.Rollback()
return err
}
if err := s.Commit(); err != nil {
_ = s.Rollback()
return err
}
return c.JSON(http.StatusOK, projects)
}

View File

@ -478,10 +478,12 @@ func registerAPIRoutes(a *echo.Group) {
},
}
a.GET("/projects", projectHandler.ReadAllWeb)
a.GET("/projects/deleted", apiv1.ListDeletedProjects)
a.GET("/projects/:project", projectHandler.ReadOneWeb)
a.POST("/projects/:project", projectHandler.UpdateWeb)
a.DELETE("/projects/:project", projectHandler.DeleteWeb)
a.PUT("/projects", projectHandler.CreateWeb)
a.POST("/projects/:project/restore", apiv1.RestoreProject)
a.GET("/projects/:project/projectusers", apiv1.ListUsersForProject)
if config.ServiceEnableLinkSharing.GetBool() {