2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2020-01-09 18:33:22 +01:00
|
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program 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.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// 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 General Public License for more details.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-11-26 21:17:33 +01:00
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2019-07-06 22:12:26 +02:00
|
|
|
"code.vikunja.io/api/pkg/config"
|
2019-03-29 18:54:35 +01:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
2019-01-25 12:40:54 +01:00
|
|
|
"code.vikunja.io/api/pkg/log"
|
2018-06-10 11:11:41 +02:00
|
|
|
_ "github.com/go-sql-driver/mysql" // Because.
|
2020-02-16 22:42:04 +01:00
|
|
|
_ "github.com/lib/pq" // Because.
|
2020-06-27 19:04:01 +02:00
|
|
|
"time"
|
2020-02-14 17:34:25 +01:00
|
|
|
"xorm.io/xorm"
|
2019-10-16 22:52:29 +02:00
|
|
|
|
2018-09-13 22:14:53 +02:00
|
|
|
_ "github.com/mattn/go-sqlite3" // Because.
|
2018-06-10 11:11:41 +02:00
|
|
|
)
|
|
|
|
|
2018-06-10 14:14:10 +02:00
|
|
|
var (
|
|
|
|
x *xorm.Engine
|
2020-06-27 19:04:01 +02:00
|
|
|
|
|
|
|
testCreatedTime time.Time
|
|
|
|
testUpdatedTime time.Time
|
2018-06-10 14:14:10 +02:00
|
|
|
)
|
2018-06-10 11:11:41 +02:00
|
|
|
|
2019-03-29 18:54:35 +01:00
|
|
|
// GetTables returns all structs which are also a table.
|
|
|
|
func GetTables() []interface{} {
|
|
|
|
return []interface{}{
|
2018-12-14 17:45:31 +01:00
|
|
|
&List{},
|
2019-08-14 22:19:04 +02:00
|
|
|
&Task{},
|
2018-12-14 17:45:31 +01:00
|
|
|
&Team{},
|
|
|
|
&TeamMember{},
|
|
|
|
&TeamList{},
|
|
|
|
&TeamNamespace{},
|
|
|
|
&Namespace{},
|
|
|
|
&ListUser{},
|
|
|
|
&NamespaceUser{},
|
2019-08-14 22:19:04 +02:00
|
|
|
&TaskAssginee{},
|
2018-12-31 02:18:41 +01:00
|
|
|
&Label{},
|
|
|
|
&LabelTask{},
|
2019-05-25 09:33:57 +02:00
|
|
|
&TaskReminder{},
|
2019-08-31 22:56:41 +02:00
|
|
|
&LinkSharing{},
|
2019-09-25 20:44:41 +02:00
|
|
|
&TaskRelation{},
|
2019-10-16 22:52:29 +02:00
|
|
|
&TaskAttachment{},
|
2020-02-19 22:57:56 +01:00
|
|
|
&TaskComment{},
|
2020-04-19 09:27:28 +02:00
|
|
|
&Bucket{},
|
2020-06-16 18:57:08 +02:00
|
|
|
&UnsplashPhoto{},
|
2020-09-26 23:02:17 +02:00
|
|
|
&SavedFilter{},
|
2019-03-29 18:54:35 +01:00
|
|
|
}
|
2018-06-10 14:14:10 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
// SetEngine sets the xorm.Engine
|
|
|
|
func SetEngine() (err error) {
|
2019-03-29 18:54:35 +01:00
|
|
|
x, err = db.CreateDBEngine()
|
2018-06-10 11:11:41 +02:00
|
|
|
if err != nil {
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Criticalf("Could not connect to db: %v", err.Error())
|
2019-03-29 18:54:35 +01:00
|
|
|
return
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache
|
2019-10-16 22:52:29 +02:00
|
|
|
if config.CacheEnabled.GetBool() && config.CacheType.GetString() == "redis" {
|
|
|
|
db.RegisterTableStructsForCache(GetTables())
|
2018-09-13 19:53:03 +02:00
|
|
|
}
|
2018-06-10 11:11:41 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-09 11:30:17 +01:00
|
|
|
|
2019-10-23 23:11:40 +02:00
|
|
|
func getLimitFromPageIndex(page int, perPage int) (limit, start int) {
|
2018-11-09 11:30:17 +01:00
|
|
|
|
2020-04-12 19:29:24 +02:00
|
|
|
// Get everything when page index is -1 or 0 (= not set)
|
|
|
|
if page < 1 {
|
2018-11-09 11:30:17 +01:00
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
|
2019-10-23 23:11:40 +02:00
|
|
|
limit = config.ServiceMaxItemsPerPage.GetInt()
|
|
|
|
if perPage > 0 {
|
|
|
|
limit = perPage
|
|
|
|
}
|
|
|
|
|
2018-11-09 11:30:17 +01:00
|
|
|
start = limit * (page - 1)
|
|
|
|
return
|
|
|
|
}
|
2018-12-12 23:50:35 +01:00
|
|
|
|
|
|
|
// GetTotalCount returns the total amount of something
|
|
|
|
func GetTotalCount(counting interface{}) (count int64, err error) {
|
|
|
|
return x.Count(counting)
|
|
|
|
}
|