apidocfix/pkg/models/task_comments.go

80 lines
2.4 KiB
Go

// Copyright 2020 Vikunja and contriubtors. All rights reserved.
//
// This file is part of Vikunja.
//
// Vikunja is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vikunja 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vikunja. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"code.vikunja.io/api/pkg/timeutil"
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"
)
type TaskComment struct {
ID int64 `xorm:"autoincr pk unique not null" json:"id"`
Comment string `xorm:"text not null" json:"comment"`
AuthorID int64 `xorm:"not null" json:"-"`
Author *user.User `xorm:"extends" json:"author"`
TaskID int64 `xorm:"not null" json:"-" param:"task"`
Created timeutil.TimeStamp `xorm:"created"`
Updated timeutil.TimeStamp `xorm:"updated"`
web.CRUDable `xorm:"-" json:"-"`
web.Rights `xorm:"-" json:"-"`
}
func (tc *TaskComment) TableName() string {
return "task_comments"
}
// Create creates a new task comment
func (tc *TaskComment) Create(a web.Auth) (err error) {
tc.AuthorID = a.GetID()
_, err = x.Insert(tc)
return
}
// Delete removes a task comment
func (tc *TaskComment) Delete() (err error) {
_, err = x.ID(tc.ID).NoAutoCondition().Delete(tc)
return
}
// Update updates a task text by its ID
func (tc *TaskComment) Update() (err error) {
_, err = x.ID(tc.ID).Cols("comment").Update(tc)
return
}
// ReadAll returns all comments for a task
func (tc *TaskComment) ReadAll(auth web.Auth, search string, page int, perPage int) (result interface{}, resultCount int, numberOfTotalItems int64, err error) {
comments := []*TaskComment{}
err = x.
Where("task_id = ? AND comment like ?", tc.TaskID, "%"+search+"%").
Join("LEFT", "users", "users.id = task_comments.author_id").
Limit(getLimitFromPageIndex(page, perPage)).
Find(&comments)
if err != nil {
return
}
numberOfTotalItems, err = x.
Where("task_id = ? AND comment like ?", tc.TaskID, "%"+search+"%").
Count(&comments)
return comments, len(comments), numberOfTotalItems, err
}