feat: add expand property to read one task

This commit is contained in:
kolaente
2025-01-24 11:20:23 +01:00
parent 7f6cb1e06e
commit 333e35e648
4 changed files with 19 additions and 4 deletions

View File

@@ -239,7 +239,7 @@ func getFilterValueForBucketFilter(filter string, view *ProjectView) (newFilter
// @Param filter query string false "The filter query to match tasks by. Check out https://vikunja.io/docs/filters for a full explanation of the feature."
// @Param filter_timezone query string false "The time zone which should be used for date match (statements like "now" resolve to different actual times)"
// @Param filter_include_nulls query string false "If set to true the result will include filtered fields whose value is set to `null`. Available values are `true` or `false`. Defaults to `false`."
// @Param expand query string false "If set to `subtasks`, Vikunja will fetch only tasks which do not have subtasks and then in a second step, will fetch all of these subtasks. This may result in more tasks than the pagination limit being returned, but all subtasks will be present in the response. You can only set this to `subtasks`."
// @Param expand query string false "If set to `subtasks`, Vikunja will fetch only tasks which do not have subtasks and then in a second step, will fetch all of these subtasks. This may result in more tasks than the pagination limit being returned, but all subtasks will be present in the response. If set to `buckets`, the buckets of each task will be present in the response. You can set this multiple times with different values.
// @Security JWTKeyAuth
// @Success 200 {array} models.Task "The tasks"
// @Failure 500 {object} models.Message "Internal error"

View File

@@ -49,7 +49,7 @@ const (
TaskRepeatModeFromCurrentDate
)
// Task represents an task in a project
// Task represents a task in a project
type Task struct {
// The unique, numeric id of this task.
ID int64 `xorm:"bigint autoincr not null unique pk" json:"id" param:"projecttask"`
@@ -122,6 +122,9 @@ type Task struct {
// All buckets across all views this task is part of. Only present when fetching tasks with the `expand` parameter set to `buckets`.
Buckets []*Bucket `xorm:"-" json:"buckets,omitempty"`
// Behaves exactly the same as with the TaskCollection.Expand parameter
Expand []TaskCollectionExpandable `xorm:"-" json:"-" query:"expand"`
// The position of the task - any task project can be sorted as usual by this parameter.
// When accessing tasks via views with buckets, this is primarily used to sort them based on a range.
// Positions are always saved per view. They will automatically be set if you request the tasks through a view
@@ -1674,6 +1677,7 @@ func (t *Task) Delete(s *xorm.Session, a web.Auth) (err error) {
// @Accept json
// @Produce json
// @Param id path int true "The task ID"
// @Param expand query string false "If set to `subtasks`, Vikunja will fetch only tasks which do not have subtasks and then in a second step, will fetch all of these subtasks. This may result in more tasks than the pagination limit being returned, but all subtasks will be present in the response. If set to `buckets`, the buckets of each task will be present in the response. You can set this multiple times with different values.
// @Security JWTKeyAuth
// @Success 200 {object} models.Task "The task"
// @Failure 404 {object} models.Message "Task not found"
@@ -1681,6 +1685,7 @@ func (t *Task) Delete(s *xorm.Session, a web.Auth) (err error) {
// @Router /tasks/{id} [get]
func (t *Task) ReadOne(s *xorm.Session, a web.Auth) (err error) {
expand := t.Expand
*t, err = GetTaskByIDSimple(s, t.ID)
if err != nil {
return
@@ -1688,8 +1693,14 @@ func (t *Task) ReadOne(s *xorm.Session, a web.Auth) (err error) {
taskMap := make(map[int64]*Task, 1)
taskMap[t.ID] = t
// TODO add expand here as well
err = addMoreInfoToTasks(s, taskMap, a, nil, nil)
for _, expandValue := range expand {
err = expandValue.Validate()
if err != nil {
return
}
}
err = addMoreInfoToTasks(s, taskMap, a, nil, expand)
if err != nil {
return
}

View File

@@ -40,12 +40,15 @@ func (t *Task) CanCreate(s *xorm.Session, a web.Auth) (bool, error) {
// CanRead determines if a user can read a task
func (t *Task) CanRead(s *xorm.Session, a web.Auth) (canRead bool, maxRight int, err error) {
expand := t.Expand
// Get the task, error out if it doesn't exist
*t, err = GetTaskByIDSimple(s, t.ID)
if err != nil {
return
}
t.Expand = expand
// A user can read a task if it has access to the project
l := &Project{ID: t.ProjectID}
return l.CanRead(s, a)

View File

@@ -38,6 +38,7 @@ import (
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/api/pkg/version"
"code.vikunja.io/api/pkg/web"
"xorm.io/xorm"
)