Task Attachments #104
|
@ -44,6 +44,6 @@ func SetEngine() (err error) {
|
|||
// GetTables returns all structs which are also a table.
|
||||
func GetTables() []interface{} {
|
||||
return []interface{}{
|
||||
File{},
|
||||
&File{},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
- id:
|
|
@ -49,6 +49,7 @@ func GetTables() []interface{} {
|
|||
&TaskReminder{},
|
||||
&LinkSharing{},
|
||||
&TaskRelation{},
|
||||
&TaskAttachment{},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ type TaskAttachment struct {
|
|||
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
|
||||
CreatedBy *User `xorm:"-" json:"created_by"`
|
||||
|
||||
File *files.File `xorm:"extends" json:"file"`
|
||||
File *files.File `xorm:"-" json:"file"`
|
||||
|
||||
Created int64 `xorm:"created"`
|
||||
|
||||
|
@ -87,13 +87,28 @@ func (ta *TaskAttachment) ReadOne() (err error) {
|
|||
|
||||
// ReadAll returns a list with all attachments
|
||||
func (ta *TaskAttachment) ReadAll(s string, a web.Auth, page int) (interface{}, error) {
|
||||
result := []*TaskAttachment{}
|
||||
attachments := []*TaskAttachment{}
|
||||
|
||||
err := x.
|
||||
Limit(getLimitFromPageIndex(page)).
|
||||
Join("LEFT", "files", "task_attachments.file_id = files.id").
|
||||
Find(result)
|
||||
return result, err
|
||||
Find(attachments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fileIDs := make([]int64, 0, len(attachments))
|
||||
for _, r := range attachments {
|
||||
fileIDs = append(fileIDs, r.ID)
|
||||
}
|
||||
|
||||
fs := make(map[int64]*files.File)
|
||||
err = x.In("id", fileIDs).Find(&fs)
|
||||
|
||||
for _, r := range attachments {
|
||||
r.File = fs[r.FileID]
|
||||
}
|
||||
|
||||
return attachments, err
|
||||
}
|
||||
|
||||
// Delete removes an attachment
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2019 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 (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTaskAttachment_ReadOne(t *testing.T) {
|
||||
ta := &TaskAttachment{
|
||||
ID: 1,
|
||||
TaskID: 1,
|
||||
}
|
||||
err := ta.ReadOne()
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, ta.File)
|
||||
assert.True(t, ta.File.ID == ta.FileID && ta.FileID != 0)
|
||||
// TODO: check file content
|
||||
}
|
|
@ -19,6 +19,7 @@ package models
|
|||
import (
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
_ "code.vikunja.io/api/pkg/config" // To trigger its init() which initializes the config
|
||||
"code.vikunja.io/api/pkg/files"
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
"code.vikunja.io/api/pkg/mail"
|
||||
"fmt"
|
||||
|
@ -65,7 +66,7 @@ func createTestEngine(fixturesDir string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
err = x.Sync2(GetTables()...)
|
||||
err = initSchema(x)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -82,7 +83,8 @@ func createTestEngine(fixturesDir string) error {
|
|||
x.SetMapper(core.GonicMapper{})
|
||||
|
||||
// Sync dat shit
|
||||
if err := x.Sync2(GetTables()...); err != nil {
|
||||
err = initSchema(x)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sync database struct error: %v", err)
|
||||
}
|
||||
|
||||
|
@ -94,3 +96,10 @@ func createTestEngine(fixturesDir string) error {
|
|||
|
||||
return InitFixtures(fixturesHelper, fixturesDir)
|
||||
}
|
||||
|
||||
func initSchema(tx *xorm.Engine) error {
|
||||
return tx.Sync2(
|
||||
GetTables(),
|
||||
files.GetTables(),
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue