2024-03-13 22:16:08 +00:00
// 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 Licensee 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 Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
2024-03-14 15:07:03 +00:00
"encoding/json"
"fmt"
2024-03-13 22:16:08 +00:00
"time"
2024-03-18 23:36:33 +00:00
"code.vikunja.io/web"
2024-03-13 22:48:34 +00:00
"xorm.io/xorm"
2024-03-13 22:16:08 +00:00
)
type ProjectViewKind int
2024-03-14 15:07:03 +00:00
func ( p * ProjectViewKind ) MarshalJSON ( ) ( [ ] byte , error ) {
switch * p {
case ProjectViewKindList :
return [ ] byte ( ` "list" ` ) , nil
case ProjectViewKindGantt :
return [ ] byte ( ` "gantt" ` ) , nil
case ProjectViewKindTable :
return [ ] byte ( ` "table" ` ) , nil
case ProjectViewKindKanban :
return [ ] byte ( ` "kanban" ` ) , nil
}
return [ ] byte ( ` null ` ) , nil
}
func ( p * ProjectViewKind ) UnmarshalJSON ( bytes [ ] byte ) error {
var value string
err := json . Unmarshal ( bytes , & value )
if err != nil {
return err
}
switch value {
case "list" :
* p = ProjectViewKindList
case "gantt" :
* p = ProjectViewKindGantt
case "table" :
* p = ProjectViewKindTable
case "kanban" :
* p = ProjectViewKindKanban
2024-03-18 12:40:58 +00:00
default :
2024-03-18 23:36:33 +00:00
return fmt . Errorf ( "unknown project view kind: %s" , value )
2024-03-14 15:07:03 +00:00
}
2024-03-18 12:40:58 +00:00
return nil
2024-03-14 15:07:03 +00:00
}
2024-03-13 22:16:08 +00:00
const (
ProjectViewKindList ProjectViewKind = iota
ProjectViewKindGantt
ProjectViewKindTable
ProjectViewKindKanban
)
2024-03-14 14:58:14 +00:00
type BucketConfigurationModeKind int
const (
BucketConfigurationModeNone BucketConfigurationModeKind = iota
BucketConfigurationModeManual
BucketConfigurationModeFilter
)
2024-03-14 15:07:03 +00:00
func ( p * BucketConfigurationModeKind ) MarshalJSON ( ) ( [ ] byte , error ) {
switch * p {
case BucketConfigurationModeNone :
return [ ] byte ( ` "none" ` ) , nil
case BucketConfigurationModeManual :
return [ ] byte ( ` "manual" ` ) , nil
case BucketConfigurationModeFilter :
return [ ] byte ( ` "filter" ` ) , nil
}
return [ ] byte ( ` null ` ) , nil
}
func ( p * BucketConfigurationModeKind ) UnmarshalJSON ( bytes [ ] byte ) error {
var value string
err := json . Unmarshal ( bytes , & value )
if err != nil {
return err
}
switch value {
case "none" :
* p = BucketConfigurationModeNone
case "manual" :
* p = BucketConfigurationModeManual
case "filter" :
* p = BucketConfigurationModeFilter
2024-03-18 12:40:58 +00:00
default :
2024-03-18 23:36:33 +00:00
return fmt . Errorf ( "unknown bucket configuration mode kind: %s" , value )
2024-03-14 15:07:03 +00:00
}
2024-03-18 12:40:58 +00:00
return nil
2024-03-14 15:07:03 +00:00
}
2024-03-14 14:58:14 +00:00
type ProjectViewBucketConfiguration struct {
Title string
Filter string
}
2024-03-13 22:16:08 +00:00
type ProjectView struct {
// The unique numeric id of this view
ID int64 ` xorm:"autoincr not null unique pk" json:"id" param:"view" `
// The title of this view
2024-03-18 12:40:58 +00:00
Title string ` xorm:"varchar(255) not null" json:"title" valid:"required,runelength(1|250)" `
2024-03-13 22:16:08 +00:00
// The project this view belongs to
ProjectID int64 ` xorm:"not null index" json:"project_id" param:"project" `
// The kind of this view. Can be `list`, `gantt`, `table` or `kanban`.
ViewKind ProjectViewKind ` xorm:"not null" json:"view_kind" `
// The filter query to match tasks by. Check out https://vikunja.io/docs/filters for a full explanation.
Filter string ` xorm:"text null default null" query:"filter" json:"filter" `
// The position of this view in the list. The list of all views will be sorted by this parameter.
Position float64 ` xorm:"double null" json:"position" `
2024-03-14 14:58:14 +00:00
// The bucket configuration mode. Can be `none`, `manual` or `filter`. `manual` allows to move tasks between buckets as you normally would. `filter` creates buckets based on a filter for each bucket.
BucketConfigurationMode BucketConfigurationModeKind ` xorm:"default 0" json:"bucket_configuration_mode" `
// When the bucket configuration mode is not `manual`, this field holds the options of that configuration.
BucketConfiguration [ ] * ProjectViewBucketConfiguration ` xorm:"json" json:"bucket_configuration" `
2024-03-15 10:27:31 +00:00
// The ID of the bucket where new tasks without a bucket are added to. By default, this is the leftmost bucket in a view.
DefaultBucketID int64 ` xorm:"bigint INDEX null" json:"default_bucket_id" `
// If tasks are moved to the done bucket, they are marked as done. If they are marked as done individually, they are moved into the done bucket.
DoneBucketID int64 ` xorm:"bigint INDEX null" json:"done_bucket_id" `
2024-03-14 14:58:14 +00:00
2024-03-13 22:16:08 +00:00
// A timestamp when this view was updated. You cannot change this value.
Updated time . Time ` xorm:"updated not null" json:"updated" `
// A timestamp when this reaction was created. You cannot change this value.
Created time . Time ` xorm:"created not null" json:"created" `
web . CRUDable ` xorm:"-" json:"-" `
web . Rights ` xorm:"-" json:"-" `
}
func ( p * ProjectView ) TableName ( ) string {
return "project_views"
}
2024-03-13 22:48:34 +00:00
2024-03-15 12:12:56 +00:00
func getViewsForProject ( s * xorm . Session , projectID int64 ) ( views [ ] * ProjectView , err error ) {
views = [ ] * ProjectView { }
err = s .
Where ( "project_id = ?" , projectID ) .
Find ( & views )
return
}
2024-03-13 22:48:34 +00:00
// ReadAll gets all project views
// @Summary Get all project views for a project
// @Description Returns all project views for a sepcific project
// @tags project
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param project path int true "Project ID"
// @Success 200 {array} models.ProjectView "The project views"
// @Failure 500 {object} models.Message "Internal error"
// @Router /projects/{project}/views [get]
func ( p * ProjectView ) ReadAll ( s * xorm . Session , a web . Auth , _ string , _ int , _ int ) ( result interface { } , resultCount int , numberOfTotalItems int64 , err error ) {
pp := & Project { ID : p . ProjectID }
can , _ , err := pp . CanRead ( s , a )
if err != nil {
return nil , 0 , 0 , err
}
if ! can {
return nil , 0 , 0 , ErrGenericForbidden { }
}
2024-03-15 12:12:56 +00:00
projectViews , err := getViewsForProject ( s , p . ProjectID )
2024-03-13 22:48:34 +00:00
if err != nil {
2024-03-15 12:12:56 +00:00
return nil , 0 , 0 , err
2024-03-13 22:48:34 +00:00
}
totalCount , err := s .
Where ( "project_id = ?" , p . ProjectID ) .
Count ( & ProjectView { } )
if err != nil {
return
}
return projectViews , len ( projectViews ) , totalCount , nil
}
// ReadOne implements the CRUD method to get one project view
// @Summary Get one project view
// @Description Returns a project view by its ID.
// @tags project
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param project path int true "Project ID"
// @Param id path int true "Project View ID"
// @Success 200 {object} models.ProjectView "The project view"
// @Failure 403 {object} web.HTTPError "The user does not have access to this project view"
// @Failure 500 {object} models.Message "Internal error"
// @Router /projects/{project}/views/{id} [get]
func ( p * ProjectView ) ReadOne ( s * xorm . Session , _ web . Auth ) ( err error ) {
2024-03-15 12:45:30 +00:00
view , err := GetProjectViewByIDAndProject ( s , p . ID , p . ProjectID )
2024-03-13 22:48:34 +00:00
if err != nil {
return err
}
* p = * view
return
}
// Delete removes the project view
// @Summary Delete a project view
// @Description Deletes a project view.
// @tags project
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param project path int true "Project ID"
// @Param id path int true "Project View ID"
// @Success 200 {object} models.Message "The project view was successfully deleted."
// @Failure 403 {object} web.HTTPError "The user does not have access to the project view"
// @Failure 500 {object} models.Message "Internal error"
// @Router /projects/{project}/views/{id} [delete]
2024-03-18 23:36:33 +00:00
func ( p * ProjectView ) Delete ( s * xorm . Session , _ web . Auth ) ( err error ) {
2024-03-13 22:48:34 +00:00
_ , err = s .
2024-03-18 12:41:42 +00:00
Where ( "id = ? AND project_id = ?" , p . ID , p . ProjectID ) .
2024-03-13 22:48:34 +00:00
Delete ( & ProjectView { } )
return
}
// Create adds a new project view
// @Summary Create a project view
// @Description Create a project view in a specific project.
// @tags project
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param project path int true "Project ID"
// @Param view body models.ProjectView true "The project view you want to create."
// @Success 200 {object} models.ProjectView "The created project view"
// @Failure 403 {object} web.HTTPError "The user does not have access to create a project view"
// @Failure 500 {object} models.Message "Internal error"
// @Router /projects/{project}/views [put]
func ( p * ProjectView ) Create ( s * xorm . Session , a web . Auth ) ( err error ) {
2024-03-18 13:00:48 +00:00
return createProjectView ( s , p , a , true )
2024-03-13 22:48:34 +00:00
}
2024-03-16 13:19:36 +00:00
func createProjectView ( s * xorm . Session , p * ProjectView , a web . Auth , createBacklogBucket bool ) ( err error ) {
_ , err = s . Insert ( p )
if err != nil {
return
}
if createBacklogBucket && p . BucketConfigurationMode == BucketConfigurationModeManual {
// Create a new first bucket for this project
b := & Bucket {
ProjectViewID : p . ID ,
Title : "Backlog" ,
}
err = b . Create ( s , a )
if err != nil {
return
}
// Move all tasks into the new bucket when the project already has tasks
c := & TaskCollection {
ProjectID : p . ProjectID ,
}
ts , _ , _ , err := c . ReadAll ( s , a , "" , 0 , - 1 )
if err != nil {
return err
}
tasks := ts . ( [ ] * Task )
if len ( tasks ) == 0 {
return nil
}
taskBuckets := [ ] * TaskBucket { }
for _ , task := range tasks {
taskBuckets = append ( taskBuckets , & TaskBucket {
TaskID : task . ID ,
BucketID : b . ID ,
ProjectViewID : p . ID ,
} )
}
_ , err = s . Insert ( & taskBuckets )
if err != nil {
return err
}
}
2024-03-16 13:50:09 +00:00
return RecalculateTaskPositions ( s , p , a )
2024-03-16 13:19:36 +00:00
}
2024-03-13 22:48:34 +00:00
// Update is the handler to update a project view
// @Summary Updates a project view
// @Description Updates a project view.
// @tags project
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param project path int true "Project ID"
// @Param id path int true "Project View ID"
// @Param view body models.ProjectView true "The project view with updated values you want to change."
// @Success 200 {object} models.ProjectView "The updated project view."
// @Failure 400 {object} web.HTTPError "Invalid project view object provided."
// @Failure 500 {object} models.Message "Internal error"
// @Router /projects/{project}/views/{id} [post]
func ( p * ProjectView ) Update ( s * xorm . Session , _ web . Auth ) ( err error ) {
// Check if the project view exists
2024-03-15 12:45:30 +00:00
_ , err = GetProjectViewByIDAndProject ( s , p . ID , p . ProjectID )
2024-03-13 22:48:34 +00:00
if err != nil {
return
}
2024-03-29 17:19:16 +00:00
_ , err = s .
ID ( p . ID ) .
Cols (
"title" ,
"view_kind" ,
"filter" ,
"position" ,
"bucket_configuration_mode" ,
"bucket_configuration" ,
"default_bucket_id" ,
"done_bucket_id" ,
) .
Update ( p )
2024-03-13 22:48:34 +00:00
if err != nil {
return
}
return
}
2024-03-15 12:45:30 +00:00
func GetProjectViewByIDAndProject ( s * xorm . Session , id , projectID int64 ) ( view * ProjectView , err error ) {
2024-03-15 21:23:38 +00:00
view = & ProjectView { }
2024-03-13 22:48:34 +00:00
exists , err := s .
2024-03-14 14:45:50 +00:00
Where ( "id = ? AND project_id = ?" , id , projectID ) .
2024-03-13 22:48:34 +00:00
NoAutoCondition ( ) .
Get ( view )
if err != nil {
return nil , err
}
if ! exists {
return nil , & ErrProjectViewDoesNotExist {
ProjectViewID : id ,
}
}
return
}
2024-03-14 08:41:55 +00:00
2024-03-15 12:45:30 +00:00
func GetProjectViewByID ( s * xorm . Session , id int64 ) ( view * ProjectView , err error ) {
2024-03-16 10:36:37 +00:00
view = & ProjectView { }
2024-03-15 12:45:30 +00:00
exists , err := s .
Where ( "id = ?" , id ) .
NoAutoCondition ( ) .
Get ( view )
if err != nil {
return nil , err
}
if ! exists {
return nil , & ErrProjectViewDoesNotExist {
ProjectViewID : id ,
}
}
return
}
2024-03-15 09:32:38 +00:00
func CreateDefaultViewsForProject ( s * xorm . Session , project * Project , a web . Auth , createBacklogBucket bool ) ( err error ) {
2024-03-14 08:41:55 +00:00
list := & ProjectView {
ProjectID : project . ID ,
Title : "List" ,
ViewKind : ProjectViewKindList ,
Position : 100 ,
}
2024-03-16 13:19:36 +00:00
err = createProjectView ( s , list , a , createBacklogBucket )
2024-03-14 08:41:55 +00:00
if err != nil {
return
}
gantt := & ProjectView {
ProjectID : project . ID ,
Title : "Gantt" ,
ViewKind : ProjectViewKindGantt ,
Position : 200 ,
}
2024-03-16 13:19:36 +00:00
err = createProjectView ( s , gantt , a , createBacklogBucket )
2024-03-14 08:41:55 +00:00
if err != nil {
return
}
table := & ProjectView {
ProjectID : project . ID ,
Title : "Table" ,
ViewKind : ProjectViewKindTable ,
Position : 300 ,
}
2024-03-16 13:19:36 +00:00
err = createProjectView ( s , table , a , createBacklogBucket )
2024-03-14 08:41:55 +00:00
if err != nil {
return
}
kanban := & ProjectView {
2024-03-15 21:49:18 +00:00
ProjectID : project . ID ,
Title : "Kanban" ,
ViewKind : ProjectViewKindKanban ,
Position : 400 ,
BucketConfigurationMode : BucketConfigurationModeManual ,
2024-03-14 08:41:55 +00:00
}
2024-03-16 13:19:36 +00:00
err = createProjectView ( s , kanban , a , createBacklogBucket )
2024-03-15 09:32:38 +00:00
if err != nil {
return
}
2024-03-18 23:24:26 +00:00
project . Views = [ ] * ProjectView {
list ,
gantt ,
table ,
kanban ,
}
2024-03-14 08:41:55 +00:00
return
}