1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package mail
import (
"dove/database"
"dove/models/mail"
)
var SystemFolders = []struct {
Name string
Slug string
Position int
}{
{Name: "Inbox", Slug: "inbox", Position: 0},
{Name: "Sent", Slug: "sent", Position: 1},
{Name: "Drafts", Slug: "drafts", Position: 2},
{Name: "Spam", Slug: "spam", Position: 3},
{Name: "Trash", Slug: "trash", Position: 4},
}
func SeedFoldersForMailbox(mailboxID uint) error {
for _, systemFolder := range SystemFolders {
folder := &mail.Folder{
Name: systemFolder.Name,
Slug: systemFolder.Slug,
MailboxID: mailboxID,
IsSystem: true,
Position: systemFolder.Position,
}
if createError := database.DB.Create(folder).Error; createError != nil {
return createError
}
}
return nil
}
func FindFoldersByMailboxID(mailboxID uint) []mail.Folder {
var folders []mail.Folder
database.DB.Where("mailbox_id = ?", mailboxID).Order("position ASC, name ASC").Find(&folders)
return folders
}
func FindFolderByID(folderID uint) *mail.Folder {
var folder mail.Folder
result := database.DB.First(&folder, folderID)
if result.Error != nil {
return nil
}
return &folder
}
func FindFolderBySlug(mailboxID uint, slug string) *mail.Folder {
var folder mail.Folder
result := database.DB.Where("mailbox_id = ? AND slug = ?", mailboxID, slug).First(&folder)
if result.Error != nil {
return nil
}
return &folder
}
func CreateFolder(folder *mail.Folder) error {
return database.DB.Create(folder).Error
}
func DeleteFolder(folder *mail.Folder) error {
return database.DB.Delete(folder).Error
}
func CountFoldersByMailboxID(mailboxID uint) int64 {
var count int64
database.DB.Model(&mail.Folder{}).Where("mailbox_id = ? AND is_system = ?", mailboxID, false).Count(&count)
return count
}
func MaxFolderPosition(mailboxID uint) int {
var maxPosition int
database.DB.Model(&mail.Folder{}).Where("mailbox_id = ?", mailboxID).Select("COALESCE(MAX(position), 0)").Scan(&maxPosition)
return maxPosition
}
|