api/pkg/models/models.go

93 lines
2.2 KiB
Go
Raw Normal View History

2020-02-07 16:27:45 +00:00
// Vikunja is a to-do list application to facilitate your life.
2020-01-09 17:33:22 +00:00
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
2018-11-26 20:17:33 +00: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 20:17:33 +00: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 20:17:33 +00: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 20:17:33 +00:00
2018-06-10 09:11:41 +00:00
package models
import (
"code.vikunja.io/api/pkg/config"
2019-03-29 17:54:35 +00:00
"code.vikunja.io/api/pkg/db"
2019-01-25 11:40:54 +00:00
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/user"
2018-06-10 09:11:41 +00:00
_ "github.com/go-sql-driver/mysql" // Because.
"github.com/go-xorm/xorm"
2019-10-16 20:52:29 +00:00
2018-09-13 20:14:53 +00:00
_ "github.com/mattn/go-sqlite3" // Because.
2018-06-10 09:11:41 +00:00
)
2018-06-10 12:14:10 +00:00
var (
x *xorm.Engine
)
2018-06-10 09:11:41 +00:00
2019-03-29 17:54:35 +00:00
// GetTables returns all structs which are also a table.
func GetTables() []interface{} {
return []interface{}{
&user.User{},
2018-12-14 16:45:31 +00:00
&List{},
2019-08-14 20:19:04 +00:00
&Task{},
2018-12-14 16:45:31 +00:00
&Team{},
&TeamMember{},
&TeamList{},
&TeamNamespace{},
&Namespace{},
&ListUser{},
&NamespaceUser{},
2019-08-14 20:19:04 +00:00
&TaskAssginee{},
2018-12-31 01:18:41 +00:00
&Label{},
&LabelTask{},
2019-05-25 07:33:57 +00:00
&TaskReminder{},
&LinkSharing{},
2019-09-25 18:44:41 +00:00
&TaskRelation{},
2019-10-16 20:52:29 +00:00
&TaskAttachment{},
2019-03-29 17:54:35 +00:00
}
2018-06-10 12:14:10 +00:00
}
2018-06-10 09:11:41 +00:00
// SetEngine sets the xorm.Engine
func SetEngine() (err error) {
2019-03-29 17:54:35 +00:00
x, err = db.CreateDBEngine()
2018-06-10 09:11:41 +00:00
if err != nil {
2019-07-20 18:12:10 +00:00
log.Criticalf("Could not connect to db: %v", err.Error())
2019-03-29 17:54:35 +00:00
return
2018-06-10 09:11:41 +00:00
}
// Cache
2019-10-16 20:52:29 +00:00
if config.CacheEnabled.GetBool() && config.CacheType.GetString() == "redis" {
db.RegisterTableStructsForCache(GetTables())
2018-09-13 17:53:03 +00:00
}
2018-06-10 09:11:41 +00:00
return nil
}
2018-11-09 10:30:17 +00:00
2019-10-23 21:11:40 +00:00
func getLimitFromPageIndex(page int, perPage int) (limit, start int) {
2018-11-09 10:30:17 +00:00
// Get everything when page index is -1
if page < 0 {
return 0, 0
}
2019-10-23 21:11:40 +00:00
limit = config.ServiceMaxItemsPerPage.GetInt()
if perPage > 0 {
limit = perPage
}
2018-11-09 10:30:17 +00:00
start = limit * (page - 1)
return
}
// GetTotalCount returns the total amount of something
func GetTotalCount(counting interface{}) (count int64, err error) {
return x.Count(counting)
}