vikunja/pkg/models/favorites.go

84 lines
2.3 KiB
Go

// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 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 (
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"
"xorm.io/xorm"
)
type FavoriteKind int
const (
FavoriteKindUnknown FavoriteKind = iota
FavoriteKindTask = 1
FavoriteKindList = 2
)
type Favorite struct {
EntityID int64 `xorm:"bigint not null pk"`
UserID int64 `xorm:"bigint not null pk"`
Kind FavoriteKind `xorm:"int not null"`
}
func (t *Favorite) TableName() string {
return "favorites"
}
func addToFavorites(s *xorm.Session, entityID int64, a web.Auth, kind FavoriteKind) error {
u, err := user.GetFromAuth(a)
if err != nil {
// Only error GetFromAuth is if it's a link share and we want to ignore that
return nil
}
fav := &Favorite{
EntityID: entityID,
UserID: u.ID,
Kind: kind,
}
_, err = s.Insert(fav)
return err
}
func removeFromFavorite(s *xorm.Session, entityID int64, a web.Auth, kind FavoriteKind) error {
u, err := user.GetFromAuth(a)
if err != nil {
// Only error GetFromAuth is if it's a link share and we want to ignore that
return nil
}
_, err = s.
Where("task_id = ? AND user_id = ? AND kind = ?", entityID, u.ID, kind).
Delete(&Favorite{})
return err
}
func isFavorite(s *xorm.Session, entityID int64, a web.Auth, kind FavoriteKind) (is bool, err error) {
u, err := user.GetFromAuth(a)
if err != nil {
// Only error GetFromAuth is if it's a link share and we want to ignore that
return
}
return s.
Where("task_id = ? AND user_id = ? AND kind = ?", entityID, u.ID, kind).
Exist(&Favorite{})
}