|
| 1 | +// Vikunja is a to-do list application to facilitate your life. |
| 2 | +// Copyright 2018-present Vikunja and contributors. All rights reserved. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package apiv2 |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "net/http" |
| 23 | + |
| 24 | + "code.vikunja.io/api/pkg/models" |
| 25 | + "code.vikunja.io/api/pkg/web/handler" |
| 26 | + |
| 27 | + "github.com/danielgtaylor/huma/v2" |
| 28 | + "github.com/danielgtaylor/huma/v2/conditional" |
| 29 | +) |
| 30 | + |
| 31 | +// projectViewListBody is the list-response envelope. models.ProjectView.ReadAll |
| 32 | +// returns []*models.ProjectView, so that's the element type. |
| 33 | +type projectViewListBody struct { |
| 34 | + Body Paginated[*models.ProjectView] |
| 35 | +} |
| 36 | + |
| 37 | +// RegisterProjectViewRoutes wires the nested ProjectView CRUD onto the Huma API. |
| 38 | +// Every operation binds two path params: {project} → ProjectID and {view} → ID. |
| 39 | +// This is the reference shape every nested sub-resource copies. |
| 40 | +func RegisterProjectViewRoutes(api huma.API) { |
| 41 | + tags := []string{"project_views"} |
| 42 | + |
| 43 | + Register(api, huma.Operation{ |
| 44 | + OperationID: "project-views-list", |
| 45 | + Summary: "List the views of a project", |
| 46 | + Description: "Returns all views of the given project. Requires read access to the project; the list is not paginated by the server but is returned in the standard list envelope.", |
| 47 | + Method: http.MethodGet, |
| 48 | + Path: "/projects/{project}/views", |
| 49 | + Tags: tags, |
| 50 | + }, projectViewsList) |
| 51 | + |
| 52 | + Register(api, huma.Operation{ |
| 53 | + OperationID: "project-views-read", |
| 54 | + Summary: "Get a single view of a project", |
| 55 | + Description: "Returns one view of a project. The view must belong to the project in the path. Sends an ETag; pass it as If-None-Match on a later read to get a 304 Not Modified.", |
| 56 | + Method: http.MethodGet, |
| 57 | + Path: "/projects/{project}/views/{view}", |
| 58 | + Tags: tags, |
| 59 | + }, projectViewsRead) |
| 60 | + |
| 61 | + Register(api, huma.Operation{ |
| 62 | + OperationID: "project-views-create", |
| 63 | + Summary: "Create a view in a project", |
| 64 | + Description: "Creates a view in the given project. The parent project is taken from the URL, not the body. Only project admins may create a view.", |
| 65 | + Method: http.MethodPost, |
| 66 | + Path: "/projects/{project}/views", |
| 67 | + Tags: tags, |
| 68 | + }, projectViewsCreate) |
| 69 | + |
| 70 | + Register(api, huma.Operation{ |
| 71 | + OperationID: "project-views-update", |
| 72 | + Summary: "Update a view of a project", |
| 73 | + Description: "Replaces a project view's fields. The view must belong to the project in the path, and only project admins may update it. Use PATCH for a partial update.", |
| 74 | + Method: http.MethodPut, |
| 75 | + Path: "/projects/{project}/views/{view}", |
| 76 | + Tags: tags, |
| 77 | + }, projectViewsUpdate) |
| 78 | + |
| 79 | + Register(api, huma.Operation{ |
| 80 | + OperationID: "project-views-delete", |
| 81 | + Summary: "Delete a view of a project", |
| 82 | + Description: "Deletes a project view along with its buckets and task positions. Only project admins may delete it.", |
| 83 | + Method: http.MethodDelete, |
| 84 | + Path: "/projects/{project}/views/{view}", |
| 85 | + Tags: tags, |
| 86 | + }, projectViewsDelete) |
| 87 | +} |
| 88 | + |
| 89 | +func projectViewsList(ctx context.Context, in *struct { |
| 90 | + ProjectID int64 `path:"project"` |
| 91 | + ListParams |
| 92 | +}) (*projectViewListBody, error) { |
| 93 | + a, err := authFromCtx(ctx) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + result, _, total, err := handler.DoReadAll(ctx, &models.ProjectView{ProjectID: in.ProjectID}, a, in.Q, in.Page, in.PerPage) |
| 98 | + if err != nil { |
| 99 | + return nil, translateDomainError(err) |
| 100 | + } |
| 101 | + items, ok := result.([]*models.ProjectView) |
| 102 | + if !ok { |
| 103 | + return nil, fmt.Errorf("projectViews.ReadAll returned unexpected type %T (expected []*models.ProjectView)", result) |
| 104 | + } |
| 105 | + return &projectViewListBody{Body: NewPaginated(items, total, in.Page, in.PerPage)}, nil |
| 106 | +} |
| 107 | + |
| 108 | +func projectViewsRead(ctx context.Context, in *struct { |
| 109 | + ProjectID int64 `path:"project"` |
| 110 | + ID int64 `path:"view"` |
| 111 | + conditional.Params |
| 112 | +}) (*singleReadBody[models.ProjectView], error) { |
| 113 | + a, err := authFromCtx(ctx) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + // ReadOne resolves the view via GetProjectViewByIDAndProject, which needs |
| 118 | + // both ids — the parent project scopes the lookup. |
| 119 | + view := &models.ProjectView{ID: in.ID, ProjectID: in.ProjectID} |
| 120 | + if _, err := handler.DoReadOne(ctx, view, a); err != nil { |
| 121 | + return nil, translateDomainError(err) |
| 122 | + } |
| 123 | + // PreconditionFailed wants the unquoted etag; response header uses RFC 9110 quoted form. |
| 124 | + etag := fmt.Sprintf("%d-%d", view.ID, view.Updated.UnixNano()) |
| 125 | + if in.HasConditionalParams() { |
| 126 | + if err := in.PreconditionFailed(etag, view.Updated); err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + } |
| 130 | + return &singleReadBody[models.ProjectView]{ETag: `"` + etag + `"`, Body: view}, nil |
| 131 | +} |
| 132 | + |
| 133 | +func projectViewsCreate(ctx context.Context, in *struct { |
| 134 | + ProjectID int64 `path:"project"` |
| 135 | + Body models.ProjectView |
| 136 | +}) (*singleBody[models.ProjectView], error) { |
| 137 | + a, err := authFromCtx(ctx) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + in.Body.ProjectID = in.ProjectID // URL wins over body |
| 142 | + if err := handler.DoCreate(ctx, &in.Body, a); err != nil { |
| 143 | + return nil, translateDomainError(err) |
| 144 | + } |
| 145 | + return &singleBody[models.ProjectView]{Body: &in.Body}, nil |
| 146 | +} |
| 147 | + |
| 148 | +func projectViewsUpdate(ctx context.Context, in *struct { |
| 149 | + ProjectID int64 `path:"project"` |
| 150 | + ID int64 `path:"view"` |
| 151 | + Body models.ProjectView |
| 152 | +}) (*singleBody[models.ProjectView], error) { |
| 153 | + a, err := authFromCtx(ctx) |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + in.Body.ID = in.ID // URL wins over body |
| 158 | + in.Body.ProjectID = in.ProjectID // parent from the path scopes the update |
| 159 | + if err := handler.DoUpdate(ctx, &in.Body, a); err != nil { |
| 160 | + return nil, translateDomainError(err) |
| 161 | + } |
| 162 | + return &singleBody[models.ProjectView]{Body: &in.Body}, nil |
| 163 | +} |
| 164 | + |
| 165 | +func projectViewsDelete(ctx context.Context, in *struct { |
| 166 | + ProjectID int64 `path:"project"` |
| 167 | + ID int64 `path:"view"` |
| 168 | +}) (*emptyBody, error) { |
| 169 | + a, err := authFromCtx(ctx) |
| 170 | + if err != nil { |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + if err := handler.DoDelete(ctx, &models.ProjectView{ID: in.ID, ProjectID: in.ProjectID}, a); err != nil { |
| 174 | + return nil, translateDomainError(err) |
| 175 | + } |
| 176 | + return &emptyBody{}, nil |
| 177 | +} |
0 commit comments